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!!! :)