1
\$\begingroup\$

I'm trying to make my own 3D model editor to better learn 3D programming. I have my translate manipulators and I'm trying to calculate the Vector2 that the mouse would have to move in order to move an object down a specific axis. This will obviously change based on the position of the camera, position of the object and the axis. I have the majority of it working, I'm just not sure on how to finish it. If the camera is completely horizontal to the object then the values come out correct, but once I start to move the camera above or below the object the values start to get less accurate. I figure I need to incorporate the camera's up vector into it, but I'm not sure how.

The code snippet is for the X axis.

Vector3 meshCameraDistance = meshPosition - cameraPosition;
Vector3 angleAxis = new Vector3(1, 0, 0);
float meshCameraAngle = Vector3.Angle(meshCameraDistance, angleAxis);
Vector2 mouseDragDirection = new Vector2(Mathf.Sin(Mathf.Deg2Rad * meshCameraAngle), Mathf.Cos(Mathf.Deg2Rad * meshCameraAngle));

if (meshCameraDistance.z < 0)
{
    mouseDragDirection = new Vector3(-mouseDragDirection.x, mouseDragDirection.y);
}

speed = (manipulatorSpeed.x * mouseDragDirection.x) + (manipulatorSpeed.y * mouseDragDirection.y);
\$\endgroup\$
2
\$\begingroup\$

Usually this is handled a bit differently.

When manipulator is picked, you choose a movement plane (XY, YZ or XZ). Then you cast ray from cursor on to that plane and detect the hit point and move the manipulator (and object) to that location.

Special cases you need to work around for are when the plane is parallel to the view and ray from cursor can reach infinity. For such cases you need to clip your movement plane to some reasonable size and discard the movement outside of it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.