Horizontal collisions

Hello guys I don’t know how to check horizontal collisions on a tile map :’( I can check vertical collisions and they works perfectly but I don’t know how to check horizontal ways. My problem is if the player is near the “tile” horizontal collision works but the player is teleported to the top of the tile. :S It’s a platform game and I hope you understand what I mean

Sounds like the problem is in the code that gets run when a collision is detected. What is the behavior you would like to have? I assume you want the player to stop horizontal motion or bounce backwards. Post the code that is run when a collision is triggered and it will make it easier to see what the problem is.

EDIT: Another question. Does your game code know when a horizontal collision has occurred vs. a vertical one?

This might help:

Sorry for my explanation. :slight_smile: Really what I want to do is “separating both collisions”. I wanna say, don’t mix the vertical collision with the horizontal collision. Actually they’re mixed and it’s very strange because every time I’m near a wall the code says “eh you’re together a block then you must be on the top of this block”. I don’t want that the player go to the top of the tile when the player is together a wall :S
I hope you understand better this explanation.
My collision code:

public class Collision {
	
	public Collision () {
		
	}
	
	public void Update(Camera camera, Player player, Level level) {
		for (int i = 0; i < level.getMap().length; i++) {
			for (int j = 0; j < level.getMap()[i].length; j++) {
				if (level.getMap()[i][j] != 0) 
					Check(i, j, camera, player, level);
			}
		}
	}
	
	private void Check(int i, int j, Camera camera, Player player, Level level) {
		if (player.getPosition().getX() - camera.getX() > i * level.getBlockSize() + level.getBlockSize() - camera.getX() ||
				player.getPosition().getY() - camera.getY() > j * level.getBlockSize() + level.getBlockSize() - camera.getY() ||
				player.getPosition().getX() - camera.getX() + player.getPosition().getWidth() * player.getScale() < i * level.getBlockSize() - camera.getX() ||
				player.getPosition().getY() - camera.getY() + player.getPosition().getHeight() * player.getScale() < j * level.getBlockSize() - camera.getY()) {
		} else {
			player.onFloor(j * level.getBlockSize(), camera);
		}
	}
}

OnFloor(player functon)

public void onFloor(float j, Camera camera) {
		jump = ((position.getY() + position.getHeight() * scale) - camera.getY() >= j - camera.getY());
		if (jump) {
			position.setY((j-camera.getY())  - (position.getHeight() * scale - camera.getY()));
		}
	}

Thanks you very much @relminator for the link but I saw your post before I asked this and I didn’t understand it so well :S Anyway thanks you :smiley:

From this StackExchange answer simply check collisions one axis at a time.

In your case, it may make sense to first check for a vertical collision then a horizontal.

Thanks you very much :slight_smile: It’s solved