Jump to content

  • Log In with Google      Sign In   
  • Create Account


TheRealVZdemon

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

Posts I've Made

In Topic: Simple hitTest with directions

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

PARTNERS