Jump to content

  • Log In with Google      Sign In   
  • Create Account


Simple hitTest with directions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 TheRealVZdemon   Members   -  Reputation: 103

Like
0Likes
Like

Posted 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;
}

thanks for reading :)

Sponsor:

#2 CurtisJC   Members   -  Reputation: 112

Like
0Likes
Like

Posted 17 May 2012 - 03:54 PM

It looks like you have it there without the complex if statements (I'm assuming A is the window and B is a box?).

if (left)
	return 1;
if (right)
	return 2;
if (up)
	return 3;
if (down)
	return 4;
else
	return 0;

Your booleans show whether one point is already pass another.
Curtis Clark
Software Engineer
Aerospace and Defence Industry

#3 TheRealVZdemon   Members   -  Reputation: 103

Like
0Likes
Like

Posted 19 May 2012 - 01:53 PM

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

Edited by TheRealVZdemon, 19 May 2012 - 01:55 PM.


#4 codyrigney92   Members   -  Reputation: 103

Like
0Likes
Like

Posted 22 May 2012 - 04:11 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS