3
votes

I am working on a simple mesh viewer implementation in C++ with basic functionality such as translation, rotation, scaling. I'm stuck with with implementing the rotation of the object along z-axis using the mouse. What I want to implement is the following:

  1. Click and drag the mouse vertically (almost vertical will do, as I use a simple threshold to filter slight deviations along the horizontal axis) to rotate the object along y-axis (this part is done).
  2. Click and drag the mouse horizontally just as described above to rotate the object along x-axis (this part is done too).
  3. For z-axis rotation, I want to detect a circular (or along an arc) mouse movement. I'm stuck with this part, and don't know how to implement this.

For the above two, i just use atan2() to determine the angle of movement. But how do I detect circular movements?

6
How will you differentiate between "almost vertical" and "slight circular", when they are basically the same? You won't tell them apart if a movement is ~3-4 px in length.Lyth
Agreed. That's the part where I'm confused. But I've seen this sort of an implementation in a sample mesh viewer, and that's why I wanted to try and implement it.user1737647
Is that a publicly available software? If so, perhaps there's a video of it in youtube.Aki Suihkonen

6 Answers

0
votes

Perhaps, you should define a screen region (e.g. at window boundaries), which, when was clicked, will initiate arc movement - or use some other modifier, a button or whatever.

Then at a mouse click you capture the coordinates and center of rotation (mesh axis) in 2D screen space. This gets you a vector (mesh center, button down pos)

On every mouse move you calculate a new vector (mesh center, mouse pos) and the angle between the two vectors is the angle of rotation.

0
votes

The only way to deal with this is to have a delay between the user starting to make the motion and the object rotating:

When user clicks and begins to move the mouse you need to determine if its going to become a straight line movement, or a circular one. This will require a certain amount of data to be collected before that judgement can be made.

The most extreme case would be requiring the user to make one complete circle first, then the rotation begins (in reality you could do much better than this). Just how small you are able to cut this period down to will depend on a) how precise you dictate your users actions must be, and b) how good you are with pattern recognition algorithms.

To get you started heres an outline of an extremely poor algorithm:

On user click store the x and y coordinates. Every 1/10 of a second store the new coordinates and process_for_pattern.

in process_for_pattern you're looking for:

A period where the x coordinates and the y coordinates regularly both increase, both decrease, or one increases and one decreases. Over time if this pattern changes such that either the x or the y begins to reverse whilst the other continues as it was, then at that moment you can be fairly sure you've got a circle.

This algorithm would require the user to draw a quarter circle before it was detected, and it does not account for size, direction, or largely irregular movements.

If you really want to continue with this method you can get a much better algorithm, but you might want to reconsider your control method.

0
votes

I don't think it works like that...

You could convert mouse wheel rotation to z-axis, or use quaternion camera orientation, which is able to rotate along every axis almost intuitively...

The opposite is true for quarternion camera: if one tries to rotate the mesh along a straight line, the mesh appears to rotate slightly around some other weird axis -- and to compensate that, one intuitively tries to follow some slightly curved trajectory.

0
votes

It's not exactly what you want, but should come close enough.

Choose a circular region within which your movements numbered 1 and 2 work as described (in the picture this would be some region that is smaller than the red circle. However, when the user clicks outside the circular region, you save the initial click position (shown in green). This defines a point which has a certain angle relative to the x-axis of your screen (you can find this easily with some trig), and it also defines the radius of the circle on which the user is working (in red). The release of the mouse adds a second point (blue). You then find the angle this point has relative to the center of the screen and the x-axis (just like before). You then project that angle onto your circle with the radius determined by the first click. The dark red arc defines the amount of rotation of the model.

enter image description here

This should be enough to get you started.

0
votes

That will not be a good input method, I think. Because you will always need some travel distance to discriminate between a line and a curve, which means some input delay. Here is an alternative:

Only vertical mouse having their line crossing the center of the screen are considered vertical. Same for horizontal. In other cases it's considered a rotation, and to calculate its amplitude, calculate the angle between the last mouse location and the current location relatively to the center of the screen.

Alternatively you could use the center of the selected mesh if your application works like that.

0
votes

You can't detect the "circular, along an arc" mouse movement with anywhere near the precision needed for 3d model viewing. What you want is something like this: http://thetechartist.com/?p=80

You nominate an axis (x, y, or z) using either keyboard shortcuts or on-screen axis indicators that you can grab with the mouse.

This will be much more precise than trying to detect an "arc" gesture. Any "arc" recognition would necessarily involve a delay while you accumulate enough mouse samples to decide whether an arc gesture has begun or not. Gesture recognition like this is non-trivial (I've done some gesture work with the Wii-mote). Similarly, even your simple "vertical" and "horizontal" mouse movement detection will require a delay for the same reason. Any "simple threshold to filter slight deviations" will make it feel dampened and weird.

For 3d viewing you want 1:1 mouse responsiveness, and that means just explicitly nominating an axis with a shortcut key or UI etc. For x-axis rotation, just restrict it to mouse x, y-axis to mouse y if you like. For z you could similarly restrict to x or y mouse input, or just take the total 2d mouse distance travelled. It depends what feels nicest to you.

As an alternative, you could try coding up support for a 3D mouse like the 3dConnexion SpaceExplorer.