Colision! How the fuq? [solved]

I have no idea of doing it!
in the begining of the tick method in the player i set xPrev and yPrev to x,y.
Then i do the moving… If intersects then i set x,y to xPrev and yPrev. I get stuck in one place pls help!


public void move() {
		xPrev = x;
		yPrev = y;
		if (Controler.up) {
			y -= Game.pSpeed;
			dir = 1;
			animUp.startAnimation();
		}
		if (Controler.down) {
			y += Game.pSpeed;
			dir = 0;
			animDown.startAnimation();
		}
		if (Controler.left) {
			x -= Game.pSpeed;
			dir = 3;
			animLeft.startAnimation();
		}
		if (Controler.right) {
			x += Game.pSpeed;
			dir = 2;
			animRight.startAnimation();
		}
		colDect();
	}

	private void colDect() {
		for (int i = 0; i < world.tiles.length; i++) {
			for (int j = 0; j < world.tiles[0].length; j++) {
				if (colBox.intersects(world.tiles[i][j]) && world.tiles[i][j].solid) {
					x =  xPrev;
					y = yPrev;
				}
			}
		}
	}


Edit:
What i did is that i moved the collision box first and checked if the is a collision if it was colliding i wouldn’t move! But i get it so that my player can’t move if i go left and up and there is collision up!For now it’s good i guess!

Hi

This is how I do collision detection. Hope this helps :slight_smile: Collisions.isCollision just checks if the player can be at their current location, and returns false if they can be. Your code [icode]colBox.intersects(world.tiles[i][j]) && world.tiles[i][j].solid[/icode] would be [icode]Collisions.isCollision()[/icode] in my code :slight_smile: [icode]player[/icode] is just a Vector2f for the user’s position.


   // Check for x
		if(!Collisions.isCollision()){
			// Move forward
			player.translate(x, 0.0f);
			if(Collisions.isCollision()){
                                // Move backward
				player.translate(-x, 0.0f);
			}
		} else {
                        // Move backward
			player.translate(-x, 0.0f);
			if(Collisions.isCollision()){
                                // Move forward
				player.translate(x, 0.0f);
			}
		}
		// Check for y
		if(!Collisions.isCollision()){
			// Move forward
			player.translate(0.0f, y);
			if(Collisions.isCollision()){
                                // Move backward
				player.translate(0.0f, -y);
			}
		} else {
                        // Move backward
			player.translate(0.0f, -y);
			if(Collisions.isCollision()){
                                // Move forward
				player.translate(0.0f, y);
			}
		}

CopyableCougar4

Isn’t this topic essentially the same as the one you opened yesterday and then marked as resolved?
We posted quite a lot of snippets in that topic, if you need help just check back in there.