• Create Account

We're offering banner ads on our site from just \$5!

# AlexWalters22

Member Since 31 Mar 2012
Offline Last Active Yesterday, 10:28 PM

### AABB Collision with SAT it works but I don't know how

01 July 2013 - 07:01 PM

So, I have been trying to familiarize myself with the Separating Axis Theorem by doing a very basic collision test AABB vs. AABB. In this one I am only testing against the x and y axes. Now this code works, I tested it from every direction, but I don't know how. I messed up the naming or did some math wrong and it worked right, which is fine, except that I would like to know how to apply this to OBB vs. OBB and Triangle vs. OBB etc. Any help would be greatly appreciated. I will try and comment on things for clarification.

This function is much slower than it should be, but I created it to be very readable(Or so I thought)

//The functions returns an int because eventually I want it to return the penetration distance

int BoundingBox::IsAABBColliding(BoundingBox * i_Box) //The i prefix is for distinguishing that it is an input variable
{
// c1                c2
//  ----------------
// |                  |
// |                  |
// |                  |
//  ----------------
//c4                 c3
//
// c = corner

//The corners and centers of the rectangles
Vector2D c1A, c1B;
Vector2D c2A, c2B;
Vector2D c3A, c3B;
Vector2D c4A, c4B;
Vector2D centerA, centerB; //No reason to calculate these(until OBB)

//Center of first
centerA.X = xCenter, centerA.Y = yCenter;

//Center of second
centerB.X = i_Box->xCenter, centerB.Y = i_Box->yCenter;

//w is width, h is height
//Corners of first
c1A.X = x, c1A.Y = y;
c2A.X = x + w, c2A.Y = y;
c3A.X = x + w, c3A.Y = y + h;
c4A.X = x, c4A.Y = y + h;

//Corners of second
c1B.X = i_Box->x, c1B.Y = i_Box->y;
c2B.X = i_Box->x + i_Box->w, c2B.Y = i_Box->y;
c3B.X = i_Box->x + i_Box->w,  c3B.Y = i_Box->y + i_Box->h;
c4B.X = i_Box->x,  c4B.Y = i_Box->y + i_Box->h;

//Left normals of top and right
//These are the two axes(in this case) that I need to check against
Vector2D rightNormalA = c2A - c1A;
rightNormalA.Y *= -1;

float tempX = rightNormalA.X;
float tempY = rightNormalA.Y;

rightNormalA.X = tempY;
rightNormalA.Y = tempX;

Vector2D topNormalA = c3A - c2A;
topNormalA.Y *= -1;

//Do the left normal
tempX = topNormalA.X;
tempY = topNormalA.Y;

topNormalA.X = tempY;
topNormalA.Y = tempX;

//  ^
//  |
//  |
//  | P topNormalA
//  |
//  |
//  |               Q rightNormalA
//  |--------------------------------->

//At this part I though I had them right at first. It only stopped the box when it was completely inside of it. So i messed around with the order of //the corners and it worked from every angle( I don't know why). Also the numbers represent which box the projection is for.

min = minimum

max = maximum

proj = projection

//The minimum and maximum projections of box 1
float q1_min_proj = Vector2D::DotProduct(c2A, rightNormalA); //x axis
float q1_max_proj = Vector2D::DotProduct(c3A, rightNormalA); //x axis
float p1_min_proj = Vector2D::DotProduct(c3A, topNormalA); //y axis
float p1_max_proj = Vector2D::DotProduct(c4A, topNormalA); //y axis

//The minimum and maximum projections of box 2
float q2_min_proj = Vector2D::DotProduct(c2B, rightNormalA); //x axis
float q2_max_proj = Vector2D::DotProduct(c3B, rightNormalA); //x axis
float p2_min_proj = Vector2D::DotProduct(c3B, topNormalA); //y axis
float p2_max_proj = Vector2D::DotProduct(c4B, topNormalA); //y axis

bool separate_p = p1_max_proj < p2_min_proj || p2_max_proj < p1_min_proj;

if(separate_p)
{
return 0;
}

bool separate_q = q1_max_proj < q2_min_proj || q2_max_proj < q1_min_proj;

if(separate_q)
{
return 0;
}

return 1;
}

### Events help

31 March 2012 - 04:44 PM

I have been working on a simple space shoot em up and I have been having some trouble with the movement of my ship. It moves fine when you use the directional keys except that when your first hit them the ship jumps then begins moving fluidly. Another question: whenever I have a direction key presssed down and then hit any other keys, then my ship ceases movement and no longer picks up the key being pressed. I would greatly appreciate help and thanks in advance.