Hey JGO.
I was implementing a collision detection system today for my 2D game’s entity system, and I came across a issue regarding If and Else statements and how they’re executed.
The issue I’m having:
How my collision movement is handled.
Since Java executes methods in a code block by the order they were written in, my method is choosing to favor 1 or the other of the If and Else statements that are used to handle my players movement.
Result on game play:
If the statement for handling left/right collision is put before handling up/down collision, the player when colliding with a entity’s top or bottom is not able to press left or right and be able to slide along the entity’s X axis.
This is vice versa for putting the up/down collision if and else statement before the left/right if and else collision statement, the player cannot slide up/down along the players Y axis.
(The entity in this case is a rock)
Here are the 2 If and Else statements:
/* Statement Number 1 handles moving Left and Right */
if (mapMovingLeft) {
if (!rightCollision) {
setMap1X(getMap1X() + map1VelX);
}
leftCollision = downCollision = upCollision = false;
} else if (mapMovingRight) {
if (!leftCollision) {
setMap1X(getMap1X() - map1VelX);
}
rightCollision = downCollision = upCollision = false;
}
/* Statement Number 2 handles moving Up and Down */
if (mapMovingUp) {
if (!downCollision) {
setMap1Y(getMap1Y() + map1VelY);
}
leftCollision = rightCollision = upCollision = false;
} else if (mapMovingDown) {
if (!upCollision) {
setMap1Y(getMap1Y() - map1VelY);
}
leftCollision = rightCollision = downCollision = false;
}
If anyone is thinking the issue I’m having is related to how I wrote my collision, here is 1 / 4 methods implemented in Paste Bin:
http://pastebin.com/yYBrvtnq
Hope somebody can help / understand thanks.