|
Title: Finding the quaternion from two vectors? Post by: TheSnidr on November 05, 2011, 07:28:44 AM Hello!
I'm trying to get quaternions to work in my game. I need to be able to quickly switch from quaternions to vectors and back to quaternions, but I'm kinda stuck on that last part. Finding the up- and to-vector of the local coordinate system is done through these equations: Code: xto=sqr(rw)+sqr(rx)-sqr(ry)-sqr(rz) Where the quaternion is [rw,rx,ry,rz]. However, how would I reverse these equations to return the quaternion with the two vectors as input?yto=(rx*ry+rw*rz)*2 zto=(rx*rz-rw*ry)*2 xup=(rz*rx+rw*ry)*2 yup=(rz*ry-rw*rx)*2 zup=sqr(rw)-sqr(rx)-sqr(ry)+sqr(rz) I've tried churning them through wxMaxima, but it doesn't seem to be able to do it. I currently use a system that works, but I'd rather want to use quaternions. Here's an old video (http://www.youtube.com/watch?v=J5pBVCJgC_o) And here's a new one (watch from 2:50) (http://www.youtube.com/watch?v=bkNq3wKKafU) Title: Re: Finding the quaternion from two vectors? Post by: Kayamon on November 05, 2011, 10:00:11 AM The easiest way is to make a 3x3 matrix, where column0=to, column1=cross(to,up), and , column2=up.
Then just run that through some standard matrix->quaternion code. You might find you have to invert the result due to convention differences. Title: Re: Finding the quaternion from two vectors? Post by: TheSnidr on November 06, 2011, 02:54:28 AM Thanks a lot for the tip! I'm no mathematician, but searching around lead me to this page, where it's described pretty well:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm The game maker script now looks like this: Code: //matrix_to_quat(xup,yup,zup,xto,yto,zto) //Returns the quaternion of the given matrix representation //Only needs up and to vectors rw=sqrt((argument3+1)*(1+argument2)-argument0*argument5)/2 rx=(argument1*(argument3+1)-argument0*argument4)/(4*rw) ry=(argument5-argument0)/(4*rw) rz=(argument1*argument5-argument4*(argument2+1))/(4*rw) |