To check which sprites are in the vicinity, you can just use the distance formula.
distance = sqrt( (x1-x2)^2 + (y1-y2)^2 );
But if you want to check collision for only the sprites in the vicinity, then its not a good choice. Anyways, you'd have to calculate the distance for all the sprites; instead you can check collision itself.
To check the side from where your player has collided, its better you have a function as
checkCollision(playerBound, targetBound, playerVelocity)
We won't check if a collision has occurred. Instead, we will check if it is about to occur it the player makes its next move.
Lets have a playerBound
to the player, the rectangle that determines the player position and size. Similarly for targetBound
playerVelocity
would be velocity vector of the player per time span (method call).
We can have a function that checks if a rectangle collides with another
bool checkXCollision(bound1, bound2)
{
if((bound1.left <= bound2.right && bound1.left >= bound2.left) || (bound1.right <= bound2.right && bound1.right >= bound2.left)) //Check for x-axis
return true; //Collision is there
return false;
}
bool checkYCollision(bound1, bound2)
{
if((bound1.bottom <= bound2.top && bound1.bottom >= bound2.bottom) || (bound1.top <= bound2.top && bound1.top >= bound2.bottom)) //Check for y-axis
return true; //Collision is there
return false;
}
bool checkCollision(bound1, bound2)
{
if(checkXCollision(bound1, bound2) && checkYCollision(bound1, bound2))
return true;
return false;
}
Notation for top, bottom, left and right
top __________
| |
| |
| |
| o |
| |
| |
bottom |__________|
left right
Case 1: Colliding at y-axis
__________
| |
| |
| |
| |
| |\ _________
| | \ | |
|__________| \| |
| |
|_________|
If after one more step, they collide, then collision has occured from the left.
Case 2: Colliding at x-axis
__________
| |
| |
| |
| |
| |
| |
|__________|
\
_\______
| |
| |
| |
|________|
If after one more step, they collide, then collision has occured from the top.
Case 3: Not colliding at any axis
__________
| |
| |
| |
| |
| |
| |
|__________|
\
\ ________
\ | |
\| |
| |
|________|
But after making one step they will collide, so we are not sure whether its from left or right.
Direction checkCollision(playerBound, targetBound, playerVelocity)
{
bool xCollided = false;
bool yCollided = false;
bool isCollided = false;
if(checkXCollision(playerBound, targetBound))
xCollided = true;
else if(checkYCollision(playerBound, targetBound))
yCollided = true;
playerBound.x += playerVelocity.x;
playerBound.y += playerVelocity.y;
if(checkCollision(playerBound, targetBound))
isCollided = true;
if(isCollided)
{
if(xCollided) //Case 1
//Collision has occured either from left or right depending on playerVelocity.x
else if(yCollided) //Case 2
//Collision has occured either from top or bottom depending on playerVelocity.y
}
}
For case 3, we can increment the position of the player by small amount unless it collides at exactly 1 axis. Of if still it collides at both axis, we can say that their corners (not sides) are collided.
Alternatively, we can compare the slope of the line made by o-p and the velocity vector. From there, we can know whether its going more towards x
or y
.
__________
| |
| |
| |
| |
| o____|__
| | | |
|_____|____p |
| |
|_______|