Jump to content
  • Advertisement
Sign in to follow this  
BlackJoker

Dragging 3d object based on ray-plane intersection

This topic is 1012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!