Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XNA鼠标拾取问题_Xna_Xna 4.0 - Fatal编程技术网

XNA鼠标拾取问题

XNA鼠标拾取问题,xna,xna-4.0,Xna,Xna 4.0,我正在为这件捡老鼠的事发毛。我不知道问题是出在光线计算还是出在边界球上,总之,下面是光线计算的代码: public Ray CalculateRay(InputManager input) { Vector3 nearSource = new Vector3(input.CurrentMousePosition, 0f); Vector3 farSource = new Vector3(input.CurrentMousePosition, 1f);

我正在为这件捡老鼠的事发毛。我不知道问题是出在光线计算还是出在边界球上,总之,下面是光线计算的代码:

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;
}
这并不是说它根本不起作用,但它似乎是非常不准确的。。。或者别的什么。模型只是球体,非常简单。任何帮助或见解都将不胜感激!
谢谢

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

摄影机位置、球体中心和半径的值是多少?如果球体距离摄影机足够远(就像小数点左边的许多地方),则设置光线方向时精度的浮点值可能起作用。如果你认为这可能是适用的,试着用10作为辐射源的Z值,给射线一个更好的机会(比如用长枪管而不是短枪管瞄准枪)。尽管上面的代码根本没有反映这一事实。我认为上面代码中的问题是我根本没有应用骨骼变换。