Click here to Skip to main content
12,951,732 members (62,816 online)
Rate this:
Please Sign up or sign in to vote.
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
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:
haitrieu749 9-Nov-12 11:29am
I want to check mouse in one Model or not.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

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;
            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;


And you can drag and drop one model by mouse.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100