# Collision detection in a 3D heightmap-based world

I'm creating a simple engine to increase my knowledge, and have a Minecraft like world, but the height is only one block.

How would you suggest I detect for collision? AABB would be good, but I have no clue. My map is stored in a 2 dimensional byte array, and I have the current location of the "player"/camera in X, Y and Z as well as the yaw (0-360 degrees).

I'm using LWJGL if that makes a difference, any ideas?

-

You world is a 2 dimensional byte array storing what? Height? Tile type? You won't be able to have caves if it's only 2 dimensional, only hills. But anyway, the general idea would be something like:

Vertical Collision (Floor)

Divide the player's (X,Z) by the block's size to know which block he's currently on top of. Find the height of the floor in that block (by checking your array at that position). If it's lower than the player's Y apply gravity, in order to make the player fall. Otherwise, set the player's Y to be the height at that spot, in order to ground him.

Horizontal Collision (Walls)

When moving the player, check the block he's on before and after the movement. If he crossed a block boundary by moving (i.e. the current block changed) check the height of the new block. If it's higher than his current height, cancel the movement because he must have hit a wall (or clamp his position against the edge of the block).

-
This is really a 2d map rendered in a 3d world. Sorry I should have made this clearer. As for horizontal collision, I did do this, but I need some sort of algorithm to find the block in front of where the player is going, if you understand that. – pychris Dec 16 '11 at 14:52
I gave you the algorithm. Which would be, 1) Moving the player forward but store his old position in a variable 2) Finding out the new current block 3) Moving the player back to the old position in case of collision. – David Gouveia Dec 16 '11 at 16:03
Ah, I think I may have found the problem! The camera should be in the center of my player (in the "eye socket" location not stuck right out). At the moment the camera is right at the edge of the player! If that makes any sense at all! – pychris Dec 16 '11 at 16:23
Yes you should be using the origin of the player for this. But it makes more sense to use the player.position directly for all collision and movement, and simply update the camera to match the player at the end of your update loop, instead of doing the collisions on the camera position. – David Gouveia Dec 16 '11 at 16:45
Always good to have some experienced people help along the way! Would you mind answering some more simple questions about this like how I should store Entity locations, in a Vector2f, two floats or two integers? – pychris Dec 16 '11 at 17:28