I'm creating a lengthy program, that has a player, and an arraylist of solid objects. For each run through the program, the program checks if there is a collision between the player, and any of the object in the arraylist. If there is a collision, the program then determines the orientation of the collision from: to left, to right, to up, or to down. I only want the program to recognize one orientation of collision at a time. However, I cant seem to figure out a geometric way to prevent the program from realizing more than one collision, or the wrong type of collision.
//I use these variables to assume a bounding box around objects/player. private int xPosLeft = x; private int xPosRight = x+Constants.width; private int xPosUp = y; private int xPosDown = y+Constants.height; //A bounding box is made for every object, and the player in this fashion.
I don't know how to explain the problem.. other than that a collision can't occur unless the player is already overlapping an object. When that happens, I simply fix up the position of the player. But it results in a 'twitchy' animation (as long as the key is pressed.) Also, if you take a minute to realize the variables, a collision to the left can also be a collision to the right... the variables may not all be parallel in structure.. because I keep changing them to figure out why I can't get it to work.
//Object is to the left of the player. public boolean CollisiontoLeft(Player Greenman) { if(Greenman.xPosLeft > xPosRight) { CollisiontoLeft = true; Greenman.xPosLeft = xPosRight; Greenman.xPosRight = Greenman.xPosLeft + Constants.xSize; Greenman.gamexPos = xPosLeft+Constants.xSize/2; Greenman.canRight=false; return true; } else return false; } //Object is to the right of the player. public boolean CollisiontoRight(Player Greenman) { if(Greenman.xPosRight < xPosLeft) { CollisiontoRight = true; Greenman.xPosRight = xPosLeft; Greenman.xPosLeft = Greenman.xPosRight - Constants.xSize; Greenman.gamexPos = xPosLeft+Constants.xSize/2; Greenman.canRight=false; return true; } else Greenman.canRight=true; return false; } //Object is above the player. public boolean CollisiontoUp(Player Greenman) { if(Greenman.yPosDown > yPosDown) { CollisiontoUp = true; Greenman.yPosUp = yPosDown; Greenman.yPosDown = Greenman.yPosUp + Constants.ySize; Greenman.gameyPos = yPosUp+Constants.ySize/2; return true; } else return false; } //Object is below the player. public boolean CollisiontoDown(Player Greenman) { if(Greenman.yPosDown < yPosUp) { CollisiontoDown = true; Greenman.yPosDown = yPosUp; Greenman.yPosUp = Greenman.yPosDown - Constants.ySize; Greenman.gameyPos = yPosUp+Constants.ySize/2; return true; } else return false;
Anyways.. tell me if you know how to fix this.