Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XNA4.0
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 23:35pm
Edited 8-Nov-12 23:37pm
v2
Comments
ely_bob at 9-Nov-12 8:33am
   
Are you having problems identifying the model or getting it to move at the right speed?
haitrieu749 at 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 at 9-Nov-12 11:29am
   
I want to check mouse in one Model or not.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Rate this: bad
good
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;
            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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2014
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