Jump to content
  • Advertisement
Sign in to follow this  
cokeface666

2D terrain collision detection

This topic is 2518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have some 2D terrain that is made from connected line segments (see attached)

[attachment=6840:terrain.png]

My questions are:

  1. Given a point above the terrain how can I tell which edge the point is closest to?
  2. How can I check for a collision with an edge? (I'm thinking particle collisions but I guess it isn't important)
  3. If the terrain is very large then is there a clever way to only check edges within close proximity of a given point for better performance?


Many thanks!

Share this post


Link to post
Share on other sites
Advertisement
well, if you define the terrain as a parametric series of rays with endpoints of T0 and Tn (their start and stop locations), you could check to see if a given point is along the edge, and thus find if something collides... however that doesnt help if your a priori and a postori times are before and after the ray... but checking the two times against eachother as another ray, you can set both ray equations equal to eachother and solve them to see if an intersection should have occured...

Basically, you've moved from point A to point B, there should be a corresponding ray between those points, additionally, each line segement is equivalent to a ray with a specified begining and end. check those two rays against eachother, and you should be able to detect if you've crossed one of the lines... you can even project where you "will be" and see if you'll cross the line in your next pass and therefore can take pre-emtive action.

proximity depends if your 2D terrain is sidescroller or top-down... given your picture, i would assume sidescroller... so you could just offset with a location and do some sort of lookup that way perhaps by checking your x-coord against other lines that start/end near it. kinda like a bi-tree (quadtree minus one dimension). even a linear grid would work.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!