# Line vs Line Collission Response

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

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 12
• 9
• 11
• 15