Jump to content
• Advertisement

# Line vs Line Collission Response

This topic is 4526 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

I've been working on a small 2D physics engine where I store everything in lines. It seems to be pretty good so far(fast/accurate/stable), but I can't figure out how to do collissions in this system. If I could do a good line to line collission then I imagine I could use that for everything. Each of my lines consists of 2 points with position, velocity, and mass. Any ideas? http://www.alrecenk.cjb.net:81/Java/Show/physics/line/

#### Share this post

##### Share on other sites
Advertisement
Lines are in collision if they're 'crossed', i.e. one point of line 2 is on the other side of line 1 from the other point on line 2. If they're just line segments (yours are) you then have to check the collision point is on the piece of line 1 that actually exists.

To find out which side of l1 a point is (we had this question recently I think):
- Get the normal/constant representation of the line l1 (analogous to a plane definition in 3D): r.n = d. n is the line normal – normalise (y2-y1, x1-x2). Then calculate d by taking the dot product of either endpoint with n (they should be the same).
- For the point p, calculate (p.n)-d. (Call this value delta – the distance from the line.) If it's positive, it's above the line; negative then below. If it's exactly 0 then the point is on the line and you need to define whether that's a collision or not.

Compare which side of l1 both endpoints of l2 are. If they're opposite, then l2 crosses the extension of l1 at the least, however it may not be a collision as the lines could make a 'broken T' shape. We need to check that the intersection point on l1 is between its endpoints.

To do that, first find the intersection point. It is (-delta1 / (delta2 + delta1)) of the way along line 2, where delta1 is the delta value for the first endpoint of l2. That is, it is line2.endpoint1 + (-delta1 / (delta2 + delta1)) * (line2.endpoint2 - line2.endpoint1).

Now check if it is within the extent of l1. Take the dot product (intersectionPoint-line1.endpoint1).(line1.endpoint2-line1.endpoint1). If it's negative the intersection point is before the start of l1, and there is no collision. If it is greater than the length of l1 squared, it is beyond the end and there is no collision. Otherwise the two lines intersect.

If you want collision response then I will have to think some more :P.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013658
×

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