Sign in to follow this  

How to translate mouse movement to a rotation gizmo?

This topic is 398 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

Hi,
 
I need to implement translation, scaling and rotation gizmos and I'm having trouble with the rotation gizmo. Basically I don't know how to translate the mouse movement to determine in which direction to rotate the object.
 
For the translation and scaling gizmos, which are represented as 3 axes, I do the following: I make a vector of the difference between the previous and current mouse position in screen space. Then multiply this vector by the inverse of the view and projection matrices, which gives me a vector of the mouse movement in world space. Then I take the dot product of the vector in world space with the  selected axis. This allows me determine if the mouse movement is in line with the axis or away from the axis. 
 
But I can't do the same thing for the rotation gizmo. The rotation gizmo is represented as 3 circles, each representing a rotation around an axis. Once a circle is clicked, I need to find some way to translate the mouse movement to determine which direction to rotate the object around the selected axis. I don't know how to do this. Please help.
 
It is important to note that I am using color picking and not raycasting to determine where the gizmo was clicked. This means that I don't have the point at which the ray hits the gizmo in world space to start from. I mention this because I've looked at examples that use raycasting like libgizmo and I don't think I can do the same thing.
 
Thanks in advance for any help.

Share this post


Link to post
Share on other sites
If you are not using Ray casting then is sounds like you are planning to translate, rotate, or scale by some amount that is proportional to the screen space distance that the user drags? This can work okay but you may find it is not ideal. For example, if the user is dragging a distant object then it should ideally move a long distance for a smaller screen space translation, whereas with a nearby object the same screen space movement should move it less far in world space. Getting things to be perspective-correct in this way becomes more crucial if you want to implement something like snap-to-vertex. In that case you really want the point on the 3d object which you initially clicked on to remain under the mouse cursor as you are dragging, which I don't think there is any way to do properly without Ray casting.

For rotating the perspective issue is not that important though I don't think, so the main question is just whether to rotate clockwise or counterclockwise around the axis you have selected. I don't have the exact algorithm in my head but I think the intuitive way to do this is that you imagine the user is interacting with the nearer side of the circle of the selected axis. If you project the rotation axis into a 2d screen space vector and then cross product with the drag vector, the sign of the z component will tell you which direction to rotate. For example, if the projected axis is pointing in positive y in screen space and you drag right then you would be grabbing the close side of the circle and spinning counterclockwise. In the case where the axis points directly towards or away from the camera, treat it more like a 2d rotation and cross the drag vector with the vector between the pivot and the mouse then look at the sign of the z component to figure out what whether it is clockwise or counterclockwise.

Share this post


Link to post
Share on other sites

Thanks for the reply uglycoyote.

 

Yes, I guess, I am planning on  translating, rotating, or scaling by some amount that is proportional to the screen space distance that the user drags.

 

I tried your calculations. I think they work, but I am not sure because I have some other problems rotating the object. I will keep working on it.

Share this post


Link to post
Share on other sites

you might try:

 

get mouse x,y when they click on a gizmo circle. this is the original x,y.

then go into a loop:

       get mouse x,y. this is the current x,y.

       rotate object based on delta between current and original x or y (use x or y, your choice, perhaps whatever aligns with the circle more).

       IE   yr  (or xr or zr)   = (Xcurrent - Xoriginal) * scaling_factor.  drag left and right to spin on vertical axis.

       until they release the mouse button.

 

the scaling factor controls how much it spins when they drag the mouse a specific amount.

Share this post


Link to post
Share on other sites

This topic is 398 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.

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