carpy-breakout/pkg/globject/quaternions.md

1.1 KiB

How to rotate using Quaternions

https://math.stackexchange.com/a/535223

To answer the question simply, given:

 P  = [0, p1, p2, p3]  <-- point vector
 R  = [w,  x,  y,  z]  <-- rotation
 R' = [w, -x, -y, -z]

For the example in the question, these are:

 P  = [0, 1, 0, 0]
 R  = [0.707, 0.0,  0.707, 0.0]
 R' = [0.707, 0.0, -0.707, 0.0]

You can calculate the resulting vector using the Hamilton product H(a, b) by:

 P' = RPR'
 P' = H(H(R, P), R')

Performing the calculations:

        H(R, P)      = [0.0, 0.707, 0.0, -0.707]
 P' = H(H(R, P), R') = [0.0, 0.0,   0.0, -1.0  ]

Thus, the example above illustrates a rotation of 90 degrees about the y-axis for the point (1, 0, 0). The result is (0, 0, -1). (Note that the first element of P' will always be 0 and can therefore be discarded.)

For those unfamiliar with quaternions, it's worth noting that the quaternion R may be determined using the formula:

a = angle to rotate
[x, y, z] = axis to rotate around (unit vector)

R = [cos(a/2), sin(a/2)*x, sin(a/2)*y, sin(a/2)*z]

See here for further reference. http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation