• Create Account

We need 7 developers from Canada and 18 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a \$50 Amazon gift card. Click here to get started!

# TheRealVZdemon

Member Since 15 May 2012
Offline Last Active May 21 2012 03:25 PM

### Simple hitTest with directions

15 May 2012 - 02:52 PM

Hello people, it's my first time here so i not sure this is the right forum for the topic but aways, i trying to make a box collision function that returns what direction the box has collided (1 for left, 2 for right, 3 for up, 4 for down) and returns 0 in no collision, but i have econtered various issues in what seems to me like functional logic. here's my funcion i using SDL for C btw. I'd be happy if someone could point out the problem with my code or provide a better solution (in code) it would help very much.

```int TestCollision(SDL_Rect a, SDL_Rect b){
int Aleft = a.x;
int Aright = a.x + a.w;
int Atop = a.y;
int Abottom = a.y+a.h;

int Bleft = b.x;
int Bright = b.x + b.w;
int Btop = b.y;
int Bbottom = b.y+b.h;
bool bottom = Abottom < Bbottom;
bool top = Atop > Btop;
bool left = Aright > Bleft;
bool right = Aleft < Bright;
if((left && top) || (left && bottom))
return 2;

if((right && top) || (right && bottom))
return 1;

if((top && left) || (top && right))
return 3;

if((bottom && left) || (bottom && right))
return 4;

return 0;
}
```