Управление 3D-моделью с помощью мыши XNA 4.0 C # - PullRequest
1 голос
/ 01 февраля 2012

Я создаю 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);

    }

}

Любая помощь в наведении курсора на ракетку была бы очень признательна.Спасибо!!!:)

...