Doug Judy
Doug Judy

Reputation: 49

How to calculate the angle given a quaternion rotation and a specified axis?

I'm trying to rotate an object around only one axis (any arbitrary unit vector, not necessarily x, y or z), based on the quaternion rotation component along the same axis of a specified quaternion rotation.

public void Rotate(Quaternion rotation, Vector3 axis, Vector3 pointToRotateAround)
{
     float angle = ?
     gameObject.transform.RotateAround(pointToRotateAround, axis, angle);
}

I don't know how to obtain the angle of my quaternion's rotation that is only along the specified axis. I could do it when the axis is y, for example:

public void Rotate(Quaternion rotation, Vector3 pointToRotateAround)
{
     gameObject.transform.RotateAround(pointToRotateAround, Vector3.up, rotation.eulerAngles.y);
}

I want to replicate the results of the above, but for any given axis.

I've dug into google trying to find the answer to this but I haven't found a solution. Any help is appreciated.

Upvotes: 0

Views: 2558

Answers (3)

J Decker
J Decker

Reputation: 586

Then angle of rotation from a unit-quaternion (w,x,y,z) also written w+xi+yj+zk is arccos(w)*2.

If the angle is > 0, the direction is (x,y,z)/sin( arccos(w)/2 ) otherwise there is no specific direction, and the rotation is identity around any axis; 0 angle is no rotation

Upvotes: 0

Doug Judy
Doug Judy

Reputation: 49

Here is the code I wanted:

public void Rotate(Quaternion rotation, Vector3 axis, Vector3 pointToRotateAround)
{
     float angle;
     Vector3 rotAxis;

     rotation.ToAngleAxis(out angle, out rotAxis);

     float angleAroundAxis = Vector3.Dot(rotAxis, axis);

     gameObject.transform.RotateAround(pointToRotateAround, axis, angleAroundAxis);
}

Upvotes: 0

KoreanwGlasses
KoreanwGlasses

Reputation: 284

The way I am interpreting your question is that if you have a set of rotating points, by what angle would they move around some other axis.

The only issue with this is that the angle from a different axis actually depends on which point you are looking at.

Rotation

However, something you could try is to represent your rotation as an axis angle (ω, θ), then take the dot product with your axis v to get a new angle of θ scaled by w.v. This might not be what you want , but if you add details with more details on what you are trying to achieve, we might be able to help you better.

Upvotes: 1

Related Questions