2012-03-30 38 views
0

我有一個fps相機和跟隨她的槍的模型,我有一個地形,相機在地形上移動就好,但我有一個問題,如果相機移動到高處(如果它試圖移動到懸崖或另一個高處我想停止移動到很高的地方的這個選項),我想停止移動我並不意味着要阻止移動到高處我意味着只有在斜率非常高的時候,希望你能理解並能夠幫助!在地形上移動 - 防止移動到高處xna

+1

無需連續發佈同樣的問題兩次:http://stackoverflow.com/questions/9944541/xna-prevent-to-go-down-slopes-or-up-hills-這,是太陡 – Denzil 2012-03-30 17:56:19

回答

3

如果您能夠從您所行走的地形獲取信息,還可以獲取有關地形角度的信息。

地形由於不同的三角形而存在,因爲它是一個網格。每個三角形有3個頂點,但也有一個所謂的:normal

面部的法線是指向上的方向。使用簡單的角度計算,您可以檢查角度是否太陡峭。

// in pseudo code: 
public bool TooSteep(Vector3 position, float maxAngle) 
{ 
    // get your information from the terrain 
    // there is probably some function, or you have to write it, 
    // that returns the normal from the terrain 
    Vector3 normal = myTerrain.GetNormal(position); 

    // then we calculate the angle between the 'up'-vector and our normal vector 
    if (Vector3.Angle(normal, Vector3.up) > maxAngle) 
     return true; 
    else return false; 
} 

因此,假設我們的最大角度是45度,我們有一個非常陡峭的正常。向上矢量和法線之間的角度將很大。大於我們的maxAngle,因此將返回:是的,它太陡。

enter image description here