XNA将鼠标位置转换为3d坐标

时间:2014-07-04 21:06:15

标签: c# xna

我使用数组绘制了一个3D网格,并希望能够使用鼠标选择单个图块,但似乎无法正确获取3D位置。 我从2个三角形绘制网格的每个方格,从0,0,0开始但是当光标放在左下角时,我得到的数据如X 0.823,Y 0.676 我用于鼠标坐标的代码如下

        MouseState ms = Mouse.GetState();

        Vector3 nearScreenPoint = new Vector3(ms.X, ms.Y, 0f);
        Vector3 farScreenPoint = new Vector3(ms.X, ms.Y, 1f);
        Vector3 nearWorldPoint = device.Viewport.Unproject(nearScreenPoint, projection, view, world);
        Vector3 farWorldPoint = device.Viewport.Unproject(farScreenPoint, projection, view, world);

        Vector3 direction = farWorldPoint - nearWorldPoint;
        direction.Normalize();

        Ray r = new Ray(nearWorldPoint, direction);

        Plane p = new Plane(0, 1, 0, 0f);

        // calculate distance of intersection point from r.origin
        float denominator = Vector3.Dot(p.Normal, r.Direction);
        float numerator = Vector3.Dot(p.Normal, r.Position) + p.D;
        float t = -(numerator / denominator);

        Vector3 pickedPosition = nearWorldPoint + direction * t;

        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
        spriteBatch.DrawString(font, "" + fps + "    " + pickedPosition.X + "    -    " + pickedPosition.Z, new Vector2(100, 100), Color.Black);
        spriteBatch.End();

在网格上绘制单个方格的代码

        Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 1000.0f);
        Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
        Matrix view = Matrix.CreateLookAt(new Vector3(xPos, yPos, zHeight), new Vector3(xPos, yPos + zRotation, 0), new Vector3(0, 5, 0));


        foreach (EffectPass pass in grassEffect.CurrentTechnique.Passes)
        {
            pass.Apply();

            VertexPositionTexture[] vertices = createCubeTexture(0, 0, 1, 0f);
            device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
        }

最后是方形创作代码

    private VertexPositionTexture[] createCubeTexture(float xlocation, float ylocation, float size, float zlocation)
    {
        VertexPositionTexture[] vertices;

        vertices = new VertexPositionTexture[6];

        vertices[0].Position = new Vector3(xlocation - size, ylocation - size, zlocation);
        vertices[0].TextureCoordinate.X = 0;
        vertices[0].TextureCoordinate.Y = 1;
        vertices[1].Position = new Vector3(xlocation - size, ylocation + size, zlocation);
        vertices[1].TextureCoordinate.X = 0;
        vertices[1].TextureCoordinate.Y = 0;
        vertices[2].Position = new Vector3(xlocation + size, ylocation - size, zlocation);
        vertices[2].TextureCoordinate.X = 1;
        vertices[2].TextureCoordinate.Y = 1;
        vertices[3].Position = new Vector3(xlocation - size, ylocation + size, zlocation);
        vertices[3].TextureCoordinate.X = 0;
        vertices[3].TextureCoordinate.Y = 0;
        vertices[4].Position = new Vector3(xlocation + size, ylocation + size, zlocation);
        vertices[4].TextureCoordinate.X = 1;
        vertices[4].TextureCoordinate.Y = 0;
        vertices[5].Position = new Vector3(xlocation + size, ylocation - size, zlocation);
        vertices[5].TextureCoordinate.X = 1;
        vertices[5].TextureCoordinate.Y = 1;

        return vertices;
    }

0 个答案:

没有答案