I am working on the move tool in a 3D modeling software. I need to know how much to move when the user drags an axis of the gizmo (for example along the x axis).
I have the 2D vector describing how much the mouse moved on the screen
MouseDelta, and the vector corresponding to the selected axis
I would like to know the best way to compute the amount of extrusion I should apply.
Currently I try to project the
AxisVector in screen space, project
MouseDelta on it and get the ratio between the size of the
AxisVectorOnScreen and the computed projection. Then I have to clamp the result and multiply it by a factor. Here is a pseudo code:
Vector3 AxisVectorOnScreen = GetViewProjMatrix().TransformVector(AxisVector); Vector2 AxisVectorOnScreen2D(AxisVectorOnScreen .X * 0.5f * Screen.Width(), AxisVectorOnScreen .Y * 0.5f * Screen.Height()); Vector2 MouseDelta = EndMousePosition - BeginMousePosition; float Size = AxisVectorOnScreen2D.Length(); float ProjectionLength = MouseDelta | AxisVectorOnScreen2D.GetSafeNormal(); float Ratio = Size / ProjectionLength; float Amount = 10.f * FMath::Clamp(Ratio, -2.5f, 2.5f);
I think I could also project my mouse vector on a plane perpendicular to the camera and do the same maths on this plane. What is the best solution?
Note: I have the same problem to get the amount of extrusion I should apply with my extrude tool (along the normal of a face).