Relative object movement in 3d space

Started by
6 comments, last by BlackJoker 8 years, 6 months ago

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?

Advertisement
Do you have any code you could post?
My current game project Platform RPG

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);
            }
         }
      }

so you're doing a raypick, which gives you the moveto location in the world, from which you get the displacement vector from current position in the world to the moveto location in the world ?

i don't really see where camera up and right vectors come into play...

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

They come into play here:


EntityWorld.SelectedEntity.MoveRelXY(
                           cameraController.UserControlledCamera.ViewMatrix.Right,
                           cameraController.UserControlledCamera.ViewMatrix.Up,
                           res);

Hey ya.

You need a offset from the 3d object centre you clicked and the pick point. and apply that with the new mouse to 3dworld location.

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

One issue left - how to deal with scaling? Seems that model with scaling is not correctly handle by the ray.intersect() method.

This topic is closed to new replies.

Advertisement