Got some collision code problems

Hi :slight_smile: I made this collision code, but its a little buggy. My player cant move either right or left and up or down when standing next to a wall. Iโ€™m pretty sure it has to do with detecting which rectangle is close to the player. I also want to optimise it so that it only checks the rectangles closest to the player :slight_smile: Any help would be nice :slight_smile:

public void collision(){
        	
        	//Collision
            //Save old position
            float oldX = getX();
            float oldY = getY();
            boolean collisionX = false, collisionY = false;
            
            rectPlayer.setSize(rectPlayer.getWidth() / world.getUnitScale(), rectPlayer.getWidth() / world.getUnitScale());

            // move on x
            setX(getX() + speedX / world.getUnitScale());

            if(speedX < 0) { // going left
                    for(int i = 0; i < world.getCollisionBoxes().size(); i++){
                    	if(getX() > world.getCollisionBoxes().get(i).x){
                    		if(rectPlayer.overlaps(world.getCollisionBoxes().get(i))){
                    			collisionX = true;
                    			
                    			System.out.println("Left");
                    		}
                    	}
                    }
            }else if(speedX > 0) { // going right
            	for(int i = 0; i < world.getCollisionBoxes().size(); i++){
                	if(getX() < world.getCollisionBoxes().get(i).x){
                		if(rectPlayer.overlaps(world.getCollisionBoxes().get(i))){
                			collisionX = true;
                			
                			System.out.println("Right");
                		}
                		
                	}
                }
            }
            // react to x collision
            if(collisionX) {
                    setX(oldX);
                    speedX = 0;
            }

            // move on y
            setY(getY() + speedY / world.getUnitScale());

            if(speedY < 0) { // going down
            	for(int i = 0; i < world.getCollisionBoxes().size(); i++){
                	if(getY() > world.getCollisionBoxes().get(i).y){
                		if(rectPlayer.overlaps(world.getCollisionBoxes().get(i))){
                			collisionY = true;
                			
                			System.out.println("Down");
                		}
                		
                	}
                }
            }else if(speedY > 0) { // going up
            	for(int i = 0; i < world.getCollisionBoxes().size(); i++){
                	if(getY() < world.getCollisionBoxes().get(i).y){
                		if(rectPlayer.overlaps(world.getCollisionBoxes().get(i))){
                			collisionY = true;
                			
                			System.out.println("Up");
                		}
                		
                	}
                } 
            }
            
            // react to y collision
            if(collisionY) {
                    setY(oldY);
                    speedY = 0;
            }

            rectPlayer.setSize(rectPlayer.getWidth() * world.getUnitScale(), rectPlayer.getHeight() * world.getUnitScale());
            
        }

All of this code seems, unnessecaryโ€ฆ

The way I move things, is pretty clean, and standard in most code:


	public boolean move(float x, float y, float delta) {
		// Whether or not we moved.
		boolean moved = false;
		// Hypothetical x and y after move.
		float hx = this.x + x * delta, hy = this.y + y * delta;

		// Is it a valid move on the x axis?
		if (/* validLocation, meaning "is this point colliding with anything?" */validLocation(hx, this.y)) {
			this.x = hx;
			moved = true;
		}

		// Is it a valid move on the y axis?
		if (/* validLocation, meaning "is this point colliding with anything?" */validLocation(this.x, hy)) {
			this.y = hy;
			moved = true;
		}

		return moved;
	}

Hope this helps! :wink:

int xa = 0, ya = 0;

if(KEY_W.down) ya -= 1;
if(KEY_S.down) ya += 1;
if(KEY_A.down) xa -= 1;
if(KEY_D.down) xa += 1;

float speed = 2;

float xspeed = xa * speed;
float yspeed = ya * speed;

int xs = (int) xspeed;
int ys = (int) yspeed;

if(xs > 0) xs += 1;
if(xs < 0) xs -= 1;
if(ys > 0) ys += 1;
if(ys < 0) ys -= 1;

int checkx = (this.xpos + xs) / level.getTileSize();
int checky = (this.ypos + ys) / level.getTileSize();

if(level.getTile(checkx, ypos).canMove()) {
	xpos += xspeed;
}

if(level.getTile(xpos, checky).canMove()) {
	ypos += yspeed;
}