Page 1 of 1

Collision Detection

Posted: October 27th, 2017, 9:53 pm
by immy

Code: Select all

bool Game::Collision(int playerX, int playerY, int playerWidth, int playerHeight, int objectX, int objectY, int objectWidth, int objectHeight)
{
	//draws images at the top left corner (0,0)
	int playerTop = playerX + playerWidth;
	int playerBottom = ;
	int playerLeft= playerY + playerHeight;
	int playerRight = ;

	int objectTop = objectX + objectWidth;
	int objectBottom = objectY;
	int objectLeft = objectY + objectHeight;
	int objectRight = ;

	/*if(playerWidth >= objectTop ||  <= objectBottom
		&& )*/
}
I am trying to find all sides of the two rectangles. I do not know how to calculate the bottom and right side of each rectangle...can somebody please assist me?

Thanks

Re: Collision Detection

Posted: October 27th, 2017, 11:04 pm
by Yumtard

Code: Select all

int playerTop = playerX + playerWidth;
this makes no sense. Player top should be just playerY
playerX + playerWidth should give you the players right side.

Code: Select all

int playerBottom = ;
y + height since y is the top

Code: Select all

  int playerLeft= playerY + playerHeight;
this also doesn't make sense to me. playerLeft should just be playerX
playerY + playerHeight should give you the players bottom like I metioned above

Code: Select all

int playerRight = ;
x + width since x is left.


Image

here's how the coordinate system looks like.
black arrow points to the position of the rect. top left corner since that's where it's being drawn from.
meaning:
x = left
x + width = right
y = top
y + height = bottom

Re: Collision Detection

Posted: October 28th, 2017, 5:11 am
by albinopapa
Or in code:
left = x;
top = y;
right = left + width;
bottom = top + height;

As Yumtard mentioned, the screen coordinates positive directions are right and down, so adding to X moves to the right and adding to Y moves downward.