Sign in to follow this  
BlackJoker

Relative object movement in 3d space

Recommended Posts

HI. I am trying to implement drag object with mouse by finding intersection between the ray and a plane. And seems coords is OK, but I cannot move object correctly. I want it to move relative to the camera X and Y axis (Right and Up) but instead of this I receive opposite direction movement.

 

Only one case when it moving correctly - when camera is along Z axis. In all other cases, object moves incorrect.

To move relatively I multiply camera relative axis (Rigth or Up) to the delta vector (difference between current and new object positions) given by the intersection test and when add them to the current position.

 

Could someone point me what I am doing wrong and why cannot drag object correctly on the all axis?

Maybe I am missing something very basic?

Edited by BlackJoker

Share this post


Link to post
Share on other sites

Sure.

Here is my intersection test

p = new Plane(EntityWorld.SelectedEntity.GetRelativePosition(), cameraController.UserControlledCamera.ViewMatrix.Forward);
                  var ray = CalculateRay(MouseManager.RelativeCoordinates, cameraController.UserControlledCamera,
                     Matrix.Identity);

                  var intersects = ray.Intersects(ref p, out coordinates);
                  if (intersects)
                  {
                     var relative = EntityWorld.SelectedEntity.GetRelativePosition();
                     var res = coordinates - relative;
                     EntityWorld.SelectedEntity.MoveRelXY(
                           cameraController.UserControlledCamera.ViewMatrix.Right,
                           cameraController.UserControlledCamera.ViewMatrix.Up,
                           res);
                  }

And here is details for MoveRelXY:

public static void MoveRelXY(this Entity inEntity, Vector3 relX, Vector3 relY, Vector3 delta)
      {
         stack.Clear();
         stack.Push(inEntity);
         while (stack.Count > 0)
         {
            Entity current = stack.Pop();

            var position = current.GetComponent<PositionComponent>();

            if (position != null)
            {
               var pos1 = Vector3.Multiply(relX, delta);
               var pos2 = Vector3.Multiply(relY, delta);
               var result = pos1 + pos2;
               position.AbsolutePosition += result;
            }

            foreach (Entity child in current.Dependencies)
            {
               stack.Push(child);
            }
         }
      }

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