This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Game Development and the fly likes Vector Collision Detection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Vector Collision Detection" Watch "Vector Collision Detection" New topic
Author

Vector Collision Detection

Wes McClintock
Greenhorn

Joined: Jul 19, 2011
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.

Thanks in advance, you guys are always so helpful.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3578
    
  14

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

Joined: Jul 19, 2011
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.
 
jQuery in Action, 2nd edition
 
subject: Vector Collision Detection
 
Similar Threads
OO design question?
Collision detection
Area
Java Newbie needs guidance
Questions emerged while working on my console board game.