Player wall collision sliding problem [Need Help]

Hey Everyone,

I’m having problems with my collisions. Basically when i colliding against a wall i cant slide if i press another key…For example. if i press The left arrow key and i am on the wall, if i continue to hold this key and try to press the up arrow my player wont move upwards. I hope you know what i mean. I know its probably a really easy fix but i need help.

Here is my player update method:

	public void update() {

		movePlayer();
		// Check Collision;

		int curCol = tileMap.getColTile((int) x);
		int curRow = tileMap.getRowTile((int) y);

		double xDest = x += dx;
		double yDest = y += dy;

		double tempX = x;
		double tempY = y;

		collision(x, yDest);
		if (dy < 0) {
			if (topLeft || topRight) {
				dy = 0;
				tempY = curRow * tileMap.getTileSize() + height / 2;
			} else {
				tempY += dy;
			}
		}
		if (dy > 0) {
			if (bottomLeft || bottomRight) {
				dy = 0;
				tempY = (curRow + 1) * tileMap.getTileSize() - height / 2;
			} else {
				tempY += dy;
			}
		}
		dy = 0;
		collision(xDest, y);
		if (dx < 0) {
			if (topLeft || bottomLeft) {
				dx = 0;
				tempX = curCol * tileMap.getTileSize() + width / 2;
			} else {
				tempX += dx;
			}
		}
		if (dx > 0) {
			if (topRight || bottomRight) {
				dx = 0;
				tempX = (curCol + 1) * tileMap.getTileSize() - width / 2;
			} else {
				tempX += dx;
			}
		}
		dx = 0;

		x = tempX;
		y = tempY;

		tileMap.setX((int) (Game.WIDTH * Game.SCALE / 2 - x));
		tileMap.setY((int) (Game.HEIGHT * Game.SCALE / 2 - y));
	}

	public void movePlayer() {
		if (left) {
			dx -= moveSpeed;
		}
		if (right) {
			dx += moveSpeed;
		}
		if (up) {
			dy -= moveSpeed;
		}
		if (down) {
			dy += moveSpeed;
		}
	}

I might have an idea on how to do it. i think you could use something like

if(dx != 0 && dy != 0){
//Something in here
            }

Please any help is much appreciated.
Thanks

  • GlennBrann

I’m not sure I understand completely because some of the code isn’t there (collision() for example).

However, I think you are doing things a little backwards, you’re moving then verifying that move. An easier way is to verify the move and only move if it is a legal move.

If you can give more information I can help further :).

I belive that this is a common problem. First you check if there´s a collision in the y-axis and stops velocity if intersection returns true, then you do the same in the x-axis. The problem is that when intersection is true in the x-axis, it will also be true in the y-axis since for a collision to be true you have to be “in” a collision. H3ckboys solution is one, another one is the one I posted in your last topic about collisions here http://www.java-gaming.org/topics/corner-collision-detection-need-help/29399/msg/269818/view.html#msg269818: Check for intersection. If it returns true you check how much the objects overlap and then push back one object the amout of overlap. This way you´ll always be just outside of collision.

Hey sorry for not being clear enough. Basically if i try to slid along a wall i just stop moving. i cant slide. here is my collision code :

	public void collision(double x, double y) {
		int leftTile = tileMap.getColTile((int) (x - width / 2));
		int rightTile = tileMap.getColTile((int) (x + width / 2) - 1);
		int topTile = tileMap.getRowTile((int) (y - height / 2));
		int bottomTile = tileMap.getRowTile((int) (y + height / 2) - 1);
		topLeft = tileMap.getTile(topTile, leftTile) == 1;
		topRight = tileMap.getTile(topTile, rightTile) == 1;
		bottomLeft = tileMap.getTile(bottomTile, leftTile) == 1;
		bottomRight = tileMap.getTile(bottomTile, rightTile) == 1;
		
	}

also, could you maybe show me the best way in which to move the player. what should i change ?

Did you read my reply?

Yes i have read your post i meant to say thank you. Ill need to try that out i just didn’t see it in time. Sorry about that. ill try it now :slight_smile:
-GLenn

Hi

I already solved a similar problem in TUER. Let me know if you still need some help.

Yeah, id love some help please. every little helps. Thanks :slight_smile:

Ok. Actually, I would have loved to show you the source code but some people will say that this is a GPL trap and if someone reuses a non trivial part of my code, I will be the bad guy reminding that derivatives must be under GPL too… I can still explain to you what I did:

  • I compute the theorical distance between the previous position and the new position (like there was no collision)
  • I compute a step count which depends on the size of the bounding volume and this distance
  • I compute a separate step count per dimension (x, z)
  • I loop while no collision is found and while my counter is less than the step count
    • I compute the temporary position of the player at this step
    • I look at several candidates around this position by considering the player can modify x and z
    • if there is a collision, I try to do the same but by considering the player can’t modify x
    • if there is a collision, I try to do the same but by considering the player can’t modify z

Where did z axis come in? I thought this is a 2d game

My game is in 3D but as I can’t jump, the collisions are handled in 2D.