2011-01-21 73 views
1

我拉着我的頭髮在這個鼠標採摘的東西。我不知道問題出在我的雷計算或我BoundingSpheres,反正這裏是我的射線計算代碼:XNA鼠標挑選問題

public Ray CalculateRay(InputManager input) 
{ 
    Vector3 nearSource = new Vector3(input.CurrentMousePosition, 0f); 
    Vector3 farSource = new Vector3(input.CurrentMousePosition, 1f); 

    Vector3 nearPoint = Engine.Device.Viewport.Unproject(nearSource, _projectionMatrix, 
    _viewMatrix, Matrix.Identity); 

    Vector3 farPoint = Engine.Device.Viewport.Unproject(farSource, 
    _projectionMatrix, _viewMatrix, Matrix.Identity); 

    Vector3 direction = farPoint - nearPoint; 
    direction.Normalize(); 

    return new Ray(nearPoint, direction); 
} 

,並在路口檢查:

public bool RayIntersectsModel() 
{   
    foreach (ModelMesh mesh in _model.Meshes) 
    { 
     BoundingSphere sphere = mesh.BoundingSphere; 

     sphere.Center = _position; 

     Ray ray = Engine.Camera.CalculateRay(Engine.Input); 

     if (sphere.Intersects(ray) != null) 
     { 
      return true; 
     } 
    } 
    return false; 
} 

它不象它不是根本沒有工作,但似乎是非常不準確的......或某事。模型只是球體,非常簡單。任何幫助或見解將不勝感激! 謝謝!

+0

你的cameraPosition,球體中心和半徑有什麼值?如果球體距離攝像機足夠遠(如十進制左邊的許多位置),那麼設置光線方向時的浮點精度可以發揮作用。如果你認爲這可能是適用的,試着用10作爲farSourse的Z值,以給予Ray一個更好的機會(如用長槍步槍而不是短手槍槍瞄準槍)。 – 2011-01-21 14:05:19

回答

0

那麼,當我改變我的邊界球時,我首先應用了世界矩陣,然後進行骨骼變換。這似乎把邊界放在了錯誤的地方。將其切換爲首先應用骨骼變換,然後由世界矩陣完成。

+0

雖然上面的代碼根本不反映這個事實。我認爲我上面的代碼中的問題是,我根本沒有應用骨骼轉換。 – 2011-01-23 04:25:45