Search code examples
collision-detectiongame-physics

How to achieve collision precision


Okay, so in my attempts to recreate the Slime environment, I've run into a problem: when the magnitude of the velocity of my ball is high enough, it does not "collide" with objects until it is already well-embedded in them, as shown below.

Collision overlap

I'm wondering what the best approach would be for more precise collision detection. I can think of two things that might work.

The first would involve increasing the framerate so that velocity could be lowered, but it seems that solution wouldn't scale at higher speeds.

My second thought is this: before changing the ball's location, I could find it's path and check if that line intersects with any other objects (shown below). If it does, then I could set the position of the ball to be nearly adjacent (overlapping by 1 pixel) to the object.

Line intersection

So, any thoughts about the best course to take? Details of implementation are welcomed.


Solution

  • Usually, the solution to this problem is exactly your second suggestion.

    The reason is that in order to accurately calculate collision detection, you should base your calculations on the most accurate thing you can, which is the location of the objects and math functions.

    If instead you go with your first approach, you'll be sacrificing accuracy, since not only is the graphical representation of the object an approximation, but it also might change in different environments.