• Create Account

## Rectangle Line collision problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Sam Sandeep  Members

123
Like
0Likes
Like

Posted 08 January 2013 - 11:58 PM

Hi,

I'm working on 2d top down car racing game.  Could anyone tell me the simplest way of the detecting the collision between the car (rectangle) and the track edges(lines).  The logic I'm currently using is not working as I expected.  I tried to check for line to line collision between the car's rectangle (4 lines) and the track lines.  Also if it not vector based it would be great, so I could use it right away in my  project.

### #2l0k0  Members

278
Like
2Likes
Like

Posted 09 January 2013 - 12:52 AM

It would be quite the feat if you could do this without vectors so not sure what you mean by "not vector based"

To keep this simple, albeit not the most efficient, I'd first make a system of equations between the line segment and the line in normal form (for each edge of the rectangle).  Ensure the t value is within 0 to 1 for the segment.  Then, find the scalar projection onto the edge of the rectangle.  It is possible that the segment resides completely in the rectangle, which means we need a point inside rectangle test.  If either point is inside the rectangle it is a positive intersection. It would look something like this:

const Vector2 RightPerp(const Vector2& v) {
return Vector2(y, -x);
}

bool32 TestLineSegmentLineSegment(const Vector2& a0, const Vector2& a1, const Vector2& b0, const Vector2& b1) {
const Vector2 a = a1 - a0;
const Vector2 b = b1 - b0;
const Vector2 bPerp = RightPerp(b);
const Vector2 bPerpN = Normalize(bPerp);
const float bPerpNDist = -Dot(bPerpN, b0);
// dot(pn, x) = -pd
// pt = a0 + a * t
// dot(pn, a0 + a * t) = -pd
// t = (-pd - dot(pn, a0)) / dot(pn, a)
// intersects a if t is in zero to one range
const float t = (-bPerpNDist - Dot(bPerpN, a0)) / (Dot(bPerpN, a));
if (0.0f <= t && t <= 1.0f) {
// this means the segment a is being intersected between a0 and a1
const Vector2 ptOnA = a0 + a * t; // this is the point on a of the intersection
// find the scalar projection of the point onto b and if it is in range they intersect
const float bt = Dot(ptOnA - b0, b) / (Dot(b, b));
if (0.0f <= bt && bt <= 1.0f) {
return 1;
}
}
return 0;
}

bool32 TestRectanglePoint(const Vector2& min, const Vector2& max, const Vector2& pt) {
return (pt.x >= min.x && pt.y >= min.y && pt.x <= max.x && pt.y <= max.y);
}

bool32 TestRectangleLineSegment(const Vector2& min, const Vector2& max, const Vector2& a, const Vector2& b) {

if (TestRectanglePoint(min, max, a) || TestRectanglePoint(min, max, b)) return 1;
const Vector2 maxXMinY = Vector2(max.x, min.y);
const Vector2 minXMaxY = Vector2(min.x, max.y);
return (TestLineSegmentLineSegment(min, maxXMinY, a, b) || TestLineSegmentLineSegment(maxXMinY, max, a, b) || TestLineSegmentLineSegment(max, minXMaxY, a, b) || TestLineSegmentLineSegment(minXMaxY, min, a, b));
} 

Give this a try and let me know if you have any issues.

Edited by l0k0, 10 January 2013 - 12:40 AM.

<shameless blog plug>
A Floating Point
</shameless blog plug>

### #3de_mattT  Members

308
Like
0Likes
Like

Posted 09 January 2013 - 04:04 PM

Nice code example l0k0. Think I'll store a link to this for future reference.

I'm not brilliant with this stuff but was wondering if
const float bt = Dot(ptOnA - b0) / (Dot(b, b));
should be
const float bt = Dot(ptOnA - b0, b) / Dot(b,b);
Thanks for sharing.

Matt

Edited by de_mattT, 09 January 2013 - 04:05 PM.

### #4l0k0  Members

278
Like
0Likes
Like

Posted 10 January 2013 - 12:39 AM

That is correct.  Edited the original mishap.

<shameless blog plug>
A Floating Point
</shameless blog plug>

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.