0

我最近能够在屏幕上创建模型并添加边界框碰撞检测。我现在要做的是当点击模型时发生某些事情并且有人知道这方面的任何好的教程吗?

4

1 回答 1

3

您需要从您的相机和鼠标坐标投射一条射线,并测试它是否与您的边界框相交。您可以通过将相机的视图和投影矩阵传递给此函数来创建射线:

 public Ray CalculateCursorRay(Matrix projectionMatrix, Matrix viewMatrix)
    {
        //Position is your mouse position
        Vector3 nearSource = new Vector3(Position, 0f);
        Vector3 farSource = new Vector3(Position, 1f);

        Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearSource,
            projectionMatrix, viewMatrix, Matrix.Identity);

        Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farSource,
            projectionMatrix, viewMatrix, Matrix.Identity);

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

        return new Ray(nearPoint, direction);
    }

然后您可以调用yourBox.Intersects(yourRay) ,如果没有交集,它将返回null 。
整个代码取自此 MSDN 示例

于 2013-06-08T16:38:45.873 回答