How do I calculate a position in front of a quaternion given the initial position and the quaternion?

7.4k Views Asked by At

Well I want to determine the position in front of, lets say 'an object'.

'An objects' has a position (pX,pY,pZ) and a quaternion rotation (qX,qY,qZ,qW) (where qW seems always to be 0 - because this is from a game I want to modify, I never saw it become 1)

Now I would like to determine the position at n units in front of the position of the object according to the quaternion rotation. How would I do that?

Extra Info:

I know the quaternion combinations produce (when starting to transform from (1,0,0,0) if that matters):

(1,0,0,0) left side facing north

(-1,0,0,0) right side facing north

(0,-1,0,0) back facing north

(0,1,0,0) front facing north

(0,0,1,0) front facing the sky

(0,0,-1,0) front facing the ground

1

There are 1 best solutions below

0
On BEST ANSWER

You can convert the quaternion into a rotation matrix, using the formula on this page.

The rows of this matrix are the vectors to which the original coordinate axes are transformed by the rotation.

So, let's suppose your "object" is a rocket, and, in its original position, its nose points along the $z$-axis. Then, after transformation by the quaternion $(w,x,y,z)$, the formula on the wikipedia page tells us that its nose points in the direction of the vector $v = (2xz -2yw, 2yz+2xw, 1 - 2x^2 - 2y^2)$. Then, a position $n$ units in front of the rocket is $p + n*v$, where $p = (pX, pY, pZ)$.

The GeometricTools site has good code for working with quaternions and rotations. Documentation is here, and code is here.