Click here to Skip to main content
15,883,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 8-Nov-12 22:37pm
v2
Comments
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.

 
Share this answer
 
v2
In this code example:
Get a Ray in 3D:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900