12,951,732 members (62,816 online)
Rate this:
See more:
In XNA 4.0 3D. I want to Drag and Drop one model 3D.So,I must check mouse in a Model or not. My problem is I don't know change position and rate this Model from 3D to 2D. It's related Matrix View and Matrix Projection of camera??
This is my code:
my code
Posted 8-Nov-12 22:35pm
Updated 8-Nov-12 22:37pm
v2
ely_bob 9-Nov-12 8:33am

Are you having problems identifying the model or getting it to move at the right speed?
haitrieu749 9-Nov-12 11:28am

Oh, my problem is getting it to move. And I have found the answer for it. This is solution: http://msdn.microsoft.com/en-us/library/bb203905.aspx
haitrieu749 9-Nov-12 11:29am

I want to check mouse in one Model or not.

Rate this:

v2
Rate this:

## Solution 2

In this code example:
Get a Ray in 3D:
```Ray GetPickRay()
{
MouseState mouseState = Mouse.GetState();

int mouseX = mouseState.X;
int mouseY = mouseState.Y;

Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f);

Matrix world = Matrix.CreateTranslation(0, 0, 0);

Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearsource,
camera.Projection, camera.View, world);

Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farsource,
camera.Projection, camera.View, world);

// Create a ray from the near clip plane to the far clip plane.
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
Ray pickRay = new Ray(nearPoint, direction);

return pickRay;
}```

And use that Ray to check collision:
```public void CheckMouse()
{
MouseState mouseState = Mouse.GetState();
float deltaX = (float)mouseState.X - (float)lastMouseState2.X;
float deltaY = -(float)mouseState.Y + (float)lastMouseState2.Y;
if (mouseState.LeftButton==ButtonState.Pressed)
{
Ray ray = GetPickRay();
float selectedDistance = float.MaxValue;

Nullable<float> result = ray.Intersects(models[0].BoudingSphere);
if (result.HasValue==true)
{
if (result.Value < selectedDistance)
{

models[0].Positon += new Vector3(deltaX, deltaY, 0);
}
}

}
lastMouseState2 = mouseState;

}</pre>
```

And you can drag and drop one model by mouse.

Top Experts
Last 24hrsThis month
 OriginalGriff 490 CPallini 135 Maciej Los 130 CHill60 110 F-ES Sitecore 105
 OriginalGriff 6,239 CHill60 3,490 Maciej Los 3,083 Jochen Arndt 1,975 ppolymorphe 1,880