Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Vector Collision Detection

Wes McClintock
Greenhorn
Posts: 25
So I've been working on a tile based platformer (actually a Jump n Bumb rip). I have vectors, delta time, and all that stuff working, the only problem is I'm not sure how to do the collision. All tiles have a solid boolean value, and I can check if the player is actually IN the tile just fine. The problem is I don't know how to make the player stop just on top of the terrain, or stop them from moving straight into/through the terrain when it lags. I can do this fine when its just 4 direction movement, but the vectors are just messing all my attempts up.

Stephan van Hulst
Bartender
Posts: 6479
83
• 1
When the program lags, your position may skip well beyond the point where a collision could have taken place. So what you need to do is think of the path you travel between two loop iterations as a line segment and then see if that line segment intersects with any surface (which are also line segments). For line segments within a two dimensional plane this means solving a relatively simple equation.

When your path intersects with a surface, you can then relocate your object to the intersection point, instead of to the end of its path.

Here's an article that details how to solve the equation for two lines within a two dimensional plane: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

Wes McClintock
Greenhorn
Posts: 25
Stephan van Hulst wrote:When the program lags, your position may skip well beyond the point where a collision could have taken place. So what you need to do is think of the path you travel between two loop iterations as a line segment and then see if that line segment intersects with any surface (which are also line segments). For line segments within a two dimensional plane this means solving a relatively simple equation.

When your path intersects with a surface, you can then relocate your object to the intersection point, instead of to the end of its path.

Here's an article that details how to solve the equation for two lines within a two dimensional plane: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

Thanks! I think I can get it working now.