# How to prevent a character from going up a steep hill with a height map?

I've been trying to make my character unable to go up steep hills, but I just can't manage to make it work. I'm using a simple height map which stores the height for every (x, z) point within the map. I've tried to do this(using C# and XNA):

``````float height1 = TerrainHeightAt(cam.Position);
float height2 = TerrainHeightAt(cam.Position + cam.GetVelocity());
//If the difference between the two is bigger than a certain constant, don't move.
``````

Basically I find the current height and the future height of the camera. Then, I check if the character is attempting to go up a too steep hill, and if so, I prevent the character from moving. This is not working and I can't see why it isn't.

So my question is: What am I doing wrong? I would appreciate any link(specially using XNA, but general articles would be great too) showing how to achieve this.

Thank you.

-
There is too little info here. "I find the current height and the future height of the camera." How? "Then, I check if the character is attempting to go up a too steep hill". How? "and if so, I prevent the character from moving." How? Why are you checking the future position of the camera rather than of the character? – Felixyz Dec 11 '10 at 6:19
The data of the terrain is held in a multi-dimensional array (float[][], which takes the x and z component and holds its height). So I basically get the height for the current camera position and its future one(which is its current position + its velocity in my case) by putting the positions' x and z components in that array and I get the height. As I said, I check if it's going up a steep hill by checking the difference between both heights. I prevent the character from moving by not adding its velocity to its position. And in my case the camera IS the character. – Jesse Emond Dec 12 '10 at 1:46

Well if you can calculate the normal of the terrain at your desired future point, you can see if the angle of that is less than some value, and if is then allow the player to move along that.

-

The problem with height difference is that it's dependent on forward velocity. So as long as a the player slows down enough they could climb almost anything.

• If you have a 45° slope and you move forward 1 unit/update you'll travel up 1 unit/update.
• If you have a 80° slope and you move forward .17 unit/update you'll travel up 1 unit/update

As Tetrad said, you need the normal of the ground under the player and compare that to the direction they are trying to move. If the angle difference is over a certain threshold don't allow the movement.

-

check the diffrence between the two heights to give you something of a steepness mesure.

``````if(height1 < height2) {
// Its uphill
float diffrence = height2 - height1;
if(diffrence > somepredefinedmax)
StopMove();
}
``````
-