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

时间:2012-02-01 12:42:19

标签: c# xna picking

我正在创建一个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);

    }

}

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

0 个答案:

没有答案