用鼠标控制3D模型 XNA 4.0 C#

发布于 2025-01-01 13:23:24 字数 3563 浏览 0 评论 0原文

我正在创建一个 3D 乒乓球游戏,球拍(在 Maya 2012 创建)由鼠标控制。我用拨球的方法来控制球拍,但是光标不在球拍上,距离球拍大约10厘米。

球拍类如下所示,move() 方法执行拾取操作。

class Racquet
{

    Model racquet;
    Camera camera;
    GraphicsDeviceManager graphics;


    //Originial model position -7.0f, 9.0f, 20.0f
    Vector3 modelPosition = Vector3.Zero; //(-7.0f, 9.0f, -20.0f);


    //Consturctor
    public Racquet(Model racquet, GraphicsDeviceManager g){

        this.racquet = racquet;
        this.graphics = g;
        camera = new Camera(this.graphics.GraphicsDevice.Viewport);
        camera.LookAt = new Vector3(0.0f, 5.0f, 6.0f);
        camera.CameraPosition = new Vector3(0.0f, 17.5f, 30.0f);
    }


    /* Method allows to draw the racquet onto the screen */
    public void RacquetDraw()
    {
        // Copy any parent transforms.
        Matrix[] transforms = new Matrix[racquet.Bones.Count];
        racquet.CopyAbsoluteBoneTransformsTo(transforms);

        // Draw the model. A model can have multiple meshes, so loop.
        foreach (ModelMesh mesh in racquet.Meshes)
        {


            // This is where the mesh orientation is set, as well 
            // as our camera and projection.
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();


                effect.World = transforms[mesh.ParentBone.Index]
                    * Matrix.CreateTranslation(modelPosition);
                effect.View = this.camera.ViewMatrix;
                effect.Projection = this.camera.ProjectionMatrix;

            }

            // Draw the mesh, using the effects set above.
            camera.Update();
            mesh.Draw();
        }
    }

    //Take x and y coordinates of mouse as parameters and then
    //converts them from 2D to 3D
    public void Move(int mousePositionX, int mousePositionY)
    {

        // Clamp mouse coordinates to viewport
        if (mousePositionX < 0) mousePositionX = 0;
        if (mousePositionY < 0) mousePositionY = 0;
        if (mousePositionX > graphics.GraphicsDevice.Viewport.Width) mousePositionX = (short)graphics.GraphicsDevice.Viewport.Width;
        if (mousePositionY > graphics.GraphicsDevice.Viewport.Height) mousePositionY = (short)graphics.GraphicsDevice.Viewport.Height;

        //bottom left corener to top right corner
        Vector3 nearSource = new Vector3((float)mousePositionX, (float)mousePositionY, 0.0f);
        Vector3 farSource = new Vector3((float)mousePositionX, (float)mousePositionY, 1.0f);



        Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearSource, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);
        Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farSource, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);

        //Determine the direction vector by subtracting maxPointSource from minPointSource
        Ray ray = new Ray(nearPoint, Vector3.Normalize(farPoint - nearPoint));

        //Define a plane with a slope of -8 with respect to the xz-axis 
        Plane plane = new Plane(Vector3.Up, -8.5f);

        //modelPosition = ray.Position;

        float denominator = Vector3.Dot(plane.Normal, ray.Direction);
        float numerator = Vector3.Dot(plane.Normal, ray.Position) + plane.D;
        float t = -(numerator / denominator);

        modelPosition = (nearPoint + ray.Direction * t);

        System.Diagnostics.Debug.WriteLine("M is " + modelPosition + "Ray is " + ray.Position);

    }

}

任何将光标放在球拍上的帮助都将非常感激。谢谢!!! :)

I am creating a 3D table tennis game and the racquet (created in Maya 2012) is controlled by mouse. I have used picking method to control the racquet however the cursor is not on the racquet, it is about 10 cm away from the racquet.

The racquet class is shown below and the move() method does the picking.

class Racquet
{

    Model racquet;
    Camera camera;
    GraphicsDeviceManager graphics;


    //Originial model position -7.0f, 9.0f, 20.0f
    Vector3 modelPosition = Vector3.Zero; //(-7.0f, 9.0f, -20.0f);


    //Consturctor
    public Racquet(Model racquet, GraphicsDeviceManager g){

        this.racquet = racquet;
        this.graphics = g;
        camera = new Camera(this.graphics.GraphicsDevice.Viewport);
        camera.LookAt = new Vector3(0.0f, 5.0f, 6.0f);
        camera.CameraPosition = new Vector3(0.0f, 17.5f, 30.0f);
    }


    /* Method allows to draw the racquet onto the screen */
    public void RacquetDraw()
    {
        // Copy any parent transforms.
        Matrix[] transforms = new Matrix[racquet.Bones.Count];
        racquet.CopyAbsoluteBoneTransformsTo(transforms);

        // Draw the model. A model can have multiple meshes, so loop.
        foreach (ModelMesh mesh in racquet.Meshes)
        {


            // This is where the mesh orientation is set, as well 
            // as our camera and projection.
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();


                effect.World = transforms[mesh.ParentBone.Index]
                    * Matrix.CreateTranslation(modelPosition);
                effect.View = this.camera.ViewMatrix;
                effect.Projection = this.camera.ProjectionMatrix;

            }

            // Draw the mesh, using the effects set above.
            camera.Update();
            mesh.Draw();
        }
    }

    //Take x and y coordinates of mouse as parameters and then
    //converts them from 2D to 3D
    public void Move(int mousePositionX, int mousePositionY)
    {

        // Clamp mouse coordinates to viewport
        if (mousePositionX < 0) mousePositionX = 0;
        if (mousePositionY < 0) mousePositionY = 0;
        if (mousePositionX > graphics.GraphicsDevice.Viewport.Width) mousePositionX = (short)graphics.GraphicsDevice.Viewport.Width;
        if (mousePositionY > graphics.GraphicsDevice.Viewport.Height) mousePositionY = (short)graphics.GraphicsDevice.Viewport.Height;

        //bottom left corener to top right corner
        Vector3 nearSource = new Vector3((float)mousePositionX, (float)mousePositionY, 0.0f);
        Vector3 farSource = new Vector3((float)mousePositionX, (float)mousePositionY, 1.0f);



        Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearSource, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);
        Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farSource, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);

        //Determine the direction vector by subtracting maxPointSource from minPointSource
        Ray ray = new Ray(nearPoint, Vector3.Normalize(farPoint - nearPoint));

        //Define a plane with a slope of -8 with respect to the xz-axis 
        Plane plane = new Plane(Vector3.Up, -8.5f);

        //modelPosition = ray.Position;

        float denominator = Vector3.Dot(plane.Normal, ray.Direction);
        float numerator = Vector3.Dot(plane.Normal, ray.Position) + plane.D;
        float t = -(numerator / denominator);

        modelPosition = (nearPoint + ray.Direction * t);

        System.Diagnostics.Debug.WriteLine("M is " + modelPosition + "Ray is " + ray.Position);

    }

}

Any help in getting the cursor on the racquet would be really appreciated. Thanks!!! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文