#### Archived

This topic is now archived and is closed to further replies.

# Check if a Rectangle and a Line collide

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

## Recommended Posts

Hi I wonder if it exist an easy way to check if a line and a rectangle collide and get the collisionpoint(s). Or do I have to make lines of the rectangle? (I hope not ) Thanks in forward /pb

##### Share on other sites
http://www.gametutorials.com/

========================
Leyder Dylan (dylan.leyder@slug-production.be.tf
http://users.skynet.be/fa550206/Slug-Production/Index.htm/

##### Share on other sites
Are the lines straight?

##### Share on other sites
i cant remember the forumale, but you can work out if two lines intersect, so basically u check all 5 lines.

you get the equation of the line, and do something with it or so, use google or someting

its fairly simple, and probably the clearest way,

and it wont matter if the lines are straight or not

edit
oh and to work out the collision point would be

eg
3y=2x+5 ...(1)
y=3x-3 ...(2)

then

5= 3y - 2x ...(3)
3 =y-3x ...(4)

then
...(4) * 3
12 = 3y - 9x ...(5)

then
(3)-(5)

5= 3y - 2x ...(3)
12 = 3y - 9x ...(5)
-7=5x
x=-7/5

SO now u have found the x pos
then

sub x=-7/5 into (2)

y= 3*(-7/5)-3

so
x=-7/5
y= 3*(-7/5)-3

im 2 lazy to work out the numbers, but its pretty simple,

[edited by - johnnyBravo on November 16, 2003 9:21:58 AM]

##### Share on other sites
quote:
Original post by Leyder Dylan
http://www.gametutorials.com/

========================
Leyder Dylan (dylan.leyder@slug-production.be.tf
http://users.skynet.be/fa550206/Slug-Production/Index.htm/

WOW, you have been around these forums for three years but you still don''t know how to use hyperlinks! Or maybe you don''t want to use them?!

##### Share on other sites
if p1 and p2 give you the line:

vNormal.x = -(p1 - p2).y;vNormal.y =  (p1 - p2).x;float d;float center;for (i = 0; i < numVertices; i++)     objectCenter += vertices[i];objectCenter /= numVertices;center = DotProd(p1 - objectCenter, vNormal);for (i = 0; i < numVertices; i++){    d = DotProd(p1 - vertices[i], vNormal);    if (d * center < 0) BANG Collision!    }

[edited by - Ilici on November 16, 2003 9:27:00 AM]

##### Share on other sites
Excellent. This is exactly what I was going to ask!

Ilici: I tried your method, but, unless I am doing something wrong, it seems HIGHLY inaccurate. I created a fast and short C# demo (1.0 Framework). It's pretty much the code you posted but instead of testing between a line and a polygon, I modified the code to test between a line and a rectangle. I would appreciate if you or anyone else could take a look and verify it.

C# test

gametutorials.com doesn't have very much on it and nothing as far as this is concerned. Or at least I couldn't find anything.

johnnyBravo: I'll try your method next. It seems the most promising if Ilici's method doesn't work that well.

[edited by - JasonA on November 16, 2003 7:04:14 PM]

##### Share on other sites
quote:
Original post by JasonA
Excellent. This is exactly what I was going to ask!

Ilici: I tried your method, but, unless I am doing something wrong, it seems HIGHLY inaccurate. I created a fast and short C# demo (1.0 Framework). It''s pretty much the code you posted but instead of testing between a line and a polygon, I modified the code to test between a line and a rectangle. I would appreciate if you or anyone else could take a look and verify it.

C# test

well, i''ve used it and it was very good. basiclly it tests if each of the vertices are on the same side of the line. the dot product is > 0 if they 2 points are on the same side and < 0 if the points are on different sides.

##### Share on other sites
thats fine if your testing for collision between a line and a plane, but not a rectangle..

##### Share on other sites
its a nice first step, but you need the point of intersection to check if this point is even part of the rectangle side.

your life will be a lot easier if the rectangles are axis aligned, maybe even so much easier that i would think about transforming line and rectangle to make it axis aligned.

after that for the left side:
if (side.y-p0.y)*(side.y-p1.y) is positive both points (start and end point of your line) are on the same side of the side (the same as above with the dot product, just simpler). in that case, go to the next side.

if its negative or 0 you line equation looks like this:
p0 + t*(p1-p0). you know it will intersect, so:
p0.y + t*(p1.y-p0.y) = side.y

solving this shouldnt be hard and you get a t.

short:
t = (side.y - p0.y) / (p1.y - p0.y)

if this t is smaller than 0 or larger than 1 they would intersect, but not in the interval between p0 and p1 (though here they will, we already did a quick and dirty test to figure that out). but the equation for our simplified 2d case is easy enough so forget about the quick and dirty test ,-)

next, plug this t into the equation to get x at the intersection.
x = p0.x + t * (p1.x-p0.x)

this x has to be between the two points of the rectangle side:
if xside.maxx forget it and go to the next side.

also, you might get up to 4 intersections but the one with the smallest t will happen first.

doing the same without the rectangle being axis aligned isnt much different except you would have line equations on both sides and solving this will be a little more work.

p0 + t*(p1-p0) = side0 + s*(side1-side0)

basically twice, for x and y coord. solve one for s and then plug the whole ugly thing into the other instead of s, then solve for t. chances are that a lot of sites will already present you with the formula ,-)

in the end both, s and t will have to be between 0 and 1 for the segments to intersect, if you need the point of intersection just plug t or s into its equation.

[edited by - Trienco on November 18, 2003 12:52:30 PM]

• 21
• 10
• 19
• 14
• 20