Sign in to follow this  

Simple hitTest with directions

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

[CODE]
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;
}
[/CODE]

thanks for reading :)

Share this post


Link to post
Share on other sites
It looks like you have it there without the complex if statements (I'm assuming A is the window and B is a box?).

[CODE]
if (left)
return 1;
if (right)
return 2;
if (up)
return 3;
if (down)
return 4;
else
return 0;
[/CODE]

Your booleans show whether one point is already pass another.

Share this post


Link to post
Share on other sites
ok i did what you said and modified my code a bit but still, only left collision works.
can you show me how to fix my booleans

btw a is supposed to be the player and b is supposed to be a wall.

here is my current code
[CODE]
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(isColliding(a, b)==true){
if(left)
return 2;

if(right)
return 1;

if(top)
return 3;

if(bottom)
return 4;
}

return 0;
}
bool isColliding(SDL_Rect a, SDL_Rect b) {
int left1 = a.x;
int right1 = a.x + a.w;
int top1 = a.y;
int bottom1 = a.y+a.h;

int left2 = b.x;
int right2 = b.x + b.w;
int top2 = b.y;
int bottom2 = b.y+b.h;
if( bottom1 < top2 ) {
return false;
}
if( top1 > bottom2 ) {
return false;
}
if( right1 < left2 ) {
return false;
}
if( left1 > right2 ) {
return false;
}

return true;
}
[/CODE] Edited by TheRealVZdemon

Share this post


Link to post
Share on other sites
It looks to me like since left is your first one, that is why it is the only one working. If another one is true but left is also true, it will never get to the other ones. You will probably have to fix the logic

Share this post


Link to post
Share on other sites
Sign in to follow this