Xna prevent descending slopes or hills that are too steep

How can I prevent going down slopes or hills that are too steep at terrain altitude? I have a 3D camera that moves around the terrain, now it moves anywhere even on large slopes and hills that are too steep, what can I do?


source to share

2 answers

I would avoid scaling / running computations as they might produce NaN if the terrain is vertical.

//currentPosition & targetPosition should be known to you

Vector3 potentialMove = Vector3.Normalize(targetPosition - currentPosition);

float steepness = Vector3.Dot(Vector3.Up, potentialMove);
if( steepness < 0.85f && steepness > -0.85f) // set to taste. 1 is vertically up, 0 is flat, -1 is vert down
  //allow move
  currentPosition = targetPosition




You have to predict where you are going to end up if you try to move in a direction, and then figure out if the slope between your current point and your future point is too steep:

if(forward key pressed) {
    get location I'll end up at
    get the Z of that location
    calculate slope using rise/run formula
    if(slope is too steep) {
        don't move
    else { move to the future location }




All Articles