Public Group

# 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.

## 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 on other sites
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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633714
• Total Posts
3013491
×