Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am creating a 3D table tennis game and the racquet (created in Maya 2012) is controlled by the 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!!! :)

share|improve this question

closed as off-topic by Josh Petrie Aug 1 at 15:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

    
I haven't looked through your code, but I have encountered problems like this before. Check that everything is in the proper type, eg make sure you are using floats instead of ints; Check that the viewport size you are using is the Actual viewport size, (might be slightly lower) - check the graphics device class for this; and if you are embedding your program in an external GUI, make sure the window/panel handle you pass to your input system is aligned with your graphics rendering window –  Aralox Feb 8 '12 at 6:04

Browse other questions tagged or ask your own question.