Sign in to follow this  
BlackJoker

Dragging 3d object based on ray-plane intersection

Recommended Posts

BlackJoker    1329

I am trying to implement dragging 3d objects by mouse and I am using for this ray-plane intersection, but results I get is not what I expect them to be.

 

When I start moving object, it begin to jump from one position to another and back, so seems that there are 2 objects instead of one.

Trying to find out why, I now move object only when I release mouse button and my results are  very strange:

 

I assume that object must be always under the mouse, but in that it goes at least twice far from the cursor. At the same time intersection coordinates and position of the object is completely the same. Distance to object is correct too.

 

I dont know how that could be, but I know that this method should keep object exactly under the mouse.

 

Here is the code for intersection:

if (MouseManager.RelativeCoordinates - startCursorPosition != Vector2.Zero)
               {
                  startCursorPosition = MouseManager.RelativeCoordinates;
                  p = new Plane(EntityWorld.SelectedEntity.GetRelativePosition(), cameraController.UserControlledCamera.ViewMatrix.Backward);
                  
                  var ray2 = CalculateRay(MouseManager.RelativeCoordinates, cameraController.UserControlledCamera,
                     EntityWorld.SelectedEntity.GetActualMatrix());
                  var intersects = ray2.Intersects(ref p, out coordinates);
                  ray2.Intersects(ref p, out distance);
                  if (intersects)
                  {
                     position = cameraController.UserControlledCamera.Offset + coordinates;
                     position.Z = startPoint.Z;
                     var location = ray2.Position + (ray2.Direction*distance);
                     Debug.WriteLine("location " + location);
                     EntityWorld.SelectedEntity.SetAbsolutePosition(position);
                   }
               }

I attached screenshot where green point shows real cursor position and you can see that the final object position quite far from the cursor.

Also you can see that intersection coordinates, which point green point and current object position is completely the same, but cursor position and object position are not equal. 

 

Could anyone point me what I am doing wrong here?

Share this post


Link to post
Share on other sites
BlackJoker    1329

After reading this topic: http://www.gamedev.net/topic/548132-solved-move-3d-object-with-2d-mouse/page-2

I suddenly realized, that I should pass to the ray calculation method not the \world matrix of the object, but Identity World matrix and when I check my suggestion, I saw, that now object is rendering strictly under cursor.

Thanks to donlucacorleone and Buckeye.

Share this post


Link to post
Share on other sites
BlackJoker    1329

Ok, Now I faced with the issue that intersection is not working correctly when I rotate camera and look to the other side of the object. Seems plane is not correct because intersection test return completely wrong coordinates. Could anyone point me to the right direction?

 

EDIT

Problem solved. I just forgot fact that plae is already relative to camera, so I need just to apply new coordinates.

Edited by BlackJoker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this