• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

# 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;
}
```