• Advertisement
Sign in to follow this  

Correct implementation of Rotate tool for 3D

This topic is 673 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 am trying to implement rotation tool for my engine and currently faced with one issue, which, I hope, could be resolved:

 

I am creating a plane with camera Z axis as Normal to align plane with the screen and then use intersection coordinates to determine the angle for rotation. Simplistically it looks like this:

var p = new Plane(coordinates, camera.ZAxis);
var ray = Collisions.CalculateRay(MouseManager.RelativeCoordinates, camera,
               Matrix.Identity);
var intersects = ray.Intersects(ref p, out resultCoordinates);
var center = currentTool.GetCenterRelative(); 
Vector3 start = center - startPoint; 
Vector3 end = center - resultCoordinates; 
start.Normalize(); 
end.Normalize(); 
var angle = (float)Math.Atan2(Vector3.Dot(Vector3.Cross(start, end), p.Normal), Vector3.Dot(end, start));
This should give me signed angle for 360 degrees. And actually it does, but only if I am in front of the object. If I place my camera behind the object, rotation sign will be inversed. I suppose because Normal of the plane also inverse as I am creating it from me camera Z axis.

 

My aim to make model rotate to the same side, where I move my cursor without inversions like now (regardless from the camera position).

 

Does this could be reached?

Edited by BlackJoker

Share this post


Link to post
Share on other sites
Advertisement

In that case, I would use mouse coordinate movement to rotate the object: If you click and drag the mouse to the right, the object rotates counter clockwise (when viewed from above) and reverse for left.

 

Be sure to use quaternion rotation if you are wanting to rotate the object from any camera direction. Quaternions rotate more accurately than Euler rotation and without the singularity at the polls.

Share this post


Link to post
Share on other sites

Specifically, what are you wanting to achieve?

 

What language are you using?

 

Rotating objects using quaternion math IS elegant......

 

EDIT: do you have a video or image showing what you want?

Edited by Hawkblood

Share this post


Link to post
Share on other sites

You misunderstood me. Quaternions has nothing to do with my problem, because I am already rotating with Quaternions.

My problem is that object rotation direction is not the same when manipulating it with the help of my rotation tool. 

For example if I look along +Z, then when I grab Z rotation tool and move it down, my object rotates around Z axes down (the same direction as my mouse goes), but when I rotate my camera 180 degrees and will look at the object along -Z and try to do the same as described before, my object will rotate in opposite direction to my mouse movements.

 

Hope I described my issue clear enough.

Share this post


Link to post
Share on other sites

You need to calculate the world-space point on the transform widget where the user has clicked, as well as the world-space vector along which a motion will produce a rotation relative to that point. Then, you transform the point/vector to screen space using your model/view/projection matrices, convert to 2D mouse coordinates.

 

Given this, information you have the screen-space vector along which a mouse movement would produce the corresponding rotation (or translation/scale). When the user starts a gesture, you store this point/vector. As the user continues the gesture you take the vector to the current mouse position in pixels from the starting point (where the mouse was first pressed). The dot product of this vector with the previously-computed screen-space rotation vector will give you the amount that the object should rotate (multiply by a scale factor to get the units right).

 

I haven't implemented any of this yet (about to do it) but I arrived at this approach by studying how the transform tool in unity works.

Edited by Aressera

Share this post


Link to post
Share on other sites

Are your tools fixed to each axis and don't move? That is to say, do you want to rotate the object ONLY in one axis at a time AND ONLY on that axis?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement