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;
}
它不象它不是根本沒有工作,但似乎是非常不準確的......或某事。模型只是球體,非常簡單。任何幫助或見解將不勝感激! 謝謝!
你的cameraPosition,球體中心和半徑有什麼值?如果球體距離攝像機足夠遠(如十進制左邊的許多位置),那麼設置光線方向時的浮點精度可以發揮作用。如果你認爲這可能是適用的,試着用10作爲farSourse的Z值,以給予Ray一個更好的機會(如用長槍步槍而不是短手槍槍瞄準槍)。 – 2011-01-21 14:05:19