1

I am pulling my hair out over this mouse picking thing. I do not know if the problem lies in my Ray calculation or my BoundingSpheres, anyway here's the code for my ray calculations:

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);
}

and for the intersection check:

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;
}

It's not like it isn't working at all but it seems to be very inaccurate... or something. The models are just spheres, very simple. Any help or insight would be greatly appreciated! Thanks!

4

1 回答 1

0

好吧,当我变换我的边界球时,我首先应用了世界矩阵,然后是骨骼变换。这似乎将边界球放在错误的位置。将其切换为首先应用骨骼变换然后世界矩阵做到了。

于 2011-01-23T04:16:41.083 回答