Plane Collision Detection
Hello!
Ok, it's simple to check a collision between planes in 2d :)
But I too need the distance the planes should move to stop colliding. I need the coordinates of these targetpoints.
See this image: http://www.christianchabek.at/stuff/cd.jpg
Can anybody help me? I'm no expert in math :)
First, I thought of Phytagoras (of course): Just adding the heights of the planes to get a triangle and the desired length of my vector. But this only works with one direction :)
This must be a simple task. Thanks in advance!
If I underestand correctly you want to find the collision response of two rectangles in 2d. Here is an easy solution.
suppse rectangles have x,y (center) and w, h.
//if there is collision in x direction
if (abs (rect1.x-rect2.x)<(rect1.w+rect2.w)/2.0)
{
colliosnX=(rect1.x+rect2.x)/2.0; //find X of collsion point
rect1.x=colliosnX + sign(rect1.x-colliosnX)* rect1.w/2.0 ;
rect2.x=colliosnX + sign(rect2.x-colliosnX)* rect2.w/2.0 ;
}
I used sign to make sure recttangles stay at the same side that they were.
You should do the same by replacing X with Y and W with H.
suppse rectangles have x,y (center) and w, h.
//if there is collision in x direction
if (abs (rect1.x-rect2.x)<(rect1.w+rect2.w)/2.0)
{
colliosnX=(rect1.x+rect2.x)/2.0; //find X of collsion point
rect1.x=colliosnX + sign(rect1.x-colliosnX)* rect1.w/2.0 ;
rect2.x=colliosnX + sign(rect2.x-colliosnX)* rect2.w/2.0 ;
}
I used sign to make sure recttangles stay at the same side that they were.
You should do the same by replacing X with Y and W with H.
Hi,
Thanks for your reply.
What does "sign" do?
I just tried your code, but it doesn't work.
I'm not sure, if you understood me right. Yes, it's about collision response (I know the word now :)). But it is possible, that two planes e.g. overlap on the y-axis, but still don't collide. Imagine two planes positioned beneath each other, with slightly different y-values and enough distance on the x-axis.
Your code would detect a collision?
I still think that this must be some vector-operation. Maybe projecting the vector between the two planes on the two vectors that form the collision rectangle...
Thanks for your reply.
What does "sign" do?
I just tried your code, but it doesn't work.
I'm not sure, if you understood me right. Yes, it's about collision response (I know the word now :)). But it is possible, that two planes e.g. overlap on the y-axis, but still don't collide. Imagine two planes positioned beneath each other, with slightly different y-values and enough distance on the x-axis.
Your code would detect a collision?
I still think that this must be some vector-operation. Maybe projecting the vector between the two planes on the two vectors that form the collision rectangle...
Quote:Original post by grisu
What does "sign" do?
It takes a scalar and returns a scalar. Usually, it's defined
though sometimes people just do,
or
All three definitions are the same everywhere except exactly at zero...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement