问题
how can i prevent to go down slopes or up hills that are too steep on heightmap terrain? I have a 3d camera that moves on a terrain, it now moves on any place even on big slopes and on hills that are too steep, what can I do?
回答1:
I would avoid rise/run calcs as they could produce a NaN if 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
}
回答2:
You should predict where you're going to end up if you attempt 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 }
}
来源:https://stackoverflow.com/questions/9944541/xna-prevent-to-go-down-slopes-or-up-hills-that-are-too-steep