Advertisement Jump to content
Sign in to follow this  

Dragging 3d object based on ray-plane intersection

This topic is 1192 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,
                  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);

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

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?



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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!