# Rectangle collision in 2d

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

## Recommended Posts

Ive been writing a game for a while now using rectangles for the collision, but I wanted to add the ability to rotate them. My old collision assumed that the rectangles were made up of horizontal and vertical lines. Now im pretty stuck on how to test for intersection. I read a few things on the web but im still in 10th grade algebra2 and it looked like calculus or something to me !O! Really i think i can get it just by checking if 2 lines intersect and a few more safety checks but im still stuck on this one. What i know is Create a line mirroring your line Find the cross product of that //im pretty sure cross product is x2*y1 - x1*y2 //but what does this value mean?? Get the dot product of the lines x and the mirrored lines y and add the calculated cross product then do the same with the lines y and its mirrored x and then see if the value is the same sign as your lines x, y // Ok now im really confused ive learned dotproduct on the web but what does it mean and does anyone relize what im doing ill put the math up but i think theres prolly an easier sollution coordinates for the mirrored line???? a = line1.v.y - line1.u.y; b = line1.u.x - line1.v.x; c = (line1.v.x * line1.u.y) - (line1.u.x * line1.v.y); is this in the form 0 = ax + by + c? what is rx?? rx = (a * line2.u.x) + (b * line2.u.y) + c; ry = (a * line2.v.x) + (b * line2.v.y) + c; if ( ( rx != 0 ) && (ry != 0 ) ) if ( rx & ry have the same sign ) return false; // don't intersect anyways this code doesnt even work :P but can anyone gimme some newb advice ?

##### Share on other sites
a simpler solution is to rotate the ray with the opposite of the angle of your rectangle, and do the test with axis aligned boxes like you used to do.

Else you can see here
http://uk.geocities.com/olivier_rebellion/Polycolly.zip

look into the docs section, segment - polygon intersection.

code is there too.

##### Share on other sites
Thanks for the help but really i think my big problem is that i havent taken a trig class before and i dont really understand what the dot product and cross product are doing... Does anyone think they can explain the process that i can use to see if 2 lines intersect using 10th grade math :P sorry this prolly should be in the newbie forum.

##### Share on other sites
sorry, can't do. You've got to know at least the dot prouct and vector basics, or else, it's a neverending discussion. Dynamics, Collision Detection, gemoetry and intersections discussion should start with that as the minimum of knowledge required.

http://www.gamedev.net/reference/articles/article1832.asp

dot product gives you nothing more than the cos of the angle between two unit vectors.

A dot B = A.Length() * B.Length() * cos(Angle(A, B)) = a.x*b.x + a.y*b.y;

=> Angle(A, B) = [+, -] acos(A dot B / (A.Length() * B.Length()));

cross product is the same for the sin of the angle (plus, it can be negative, so it's got a 'direction' as well).

ect...

##### Share on other sites
not sure, if this is understandable for you, but it might be interesting:

it starts with vectors.

http://www.flipcode.com/articles/gprimer1_issue01.shtml

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015539
×