Implementing Quaternions

BlitzMax Modules Forums/MiniB3D/Implementing Quaternions

Okay, it seems to work when you find every instance of -rx or -z and don't flip them. The only thing I am not sure how to do is retrieve the global quaternion.

I've got it pretty close here, which shows how to extract a quat from a mat, but the signs get flipped randomly:
Global VECTOR_X#
Global VECTOR_Y#
Global VECTOR_Z#
Global VECTOR_W#

Local grid#[3,3]

SeedRnd MilliSecs()

x#=Rnd(-180,180)
y#=Rnd(-180,180)
z#=Rnd(-180,180)

eulerasquat x,y,z

qx#=VECTOR_x
qy#=VECTOR_Y
qz#=VECTOR_Z
qw#=VECTOR_W

grid[0,0]=(1.0-2.0*qx*qx-2.0*qz*qz)
grid[1,0]=-(2.0*qz*qy-2.0*qw*qx)
grid[2,0]=-(2.0*qx*qy+2.0*qw*qz)

grid[0,1]=-(2.0*qz*qy+2.0*qw*qx)
grid[1,1]=(1.0-2.0*qy*qy-2.0*qx*qx)
grid[2,1]=-(2.0*qw*qy-2.0*qx*qz)

grid[0,2]=2.0*qw*qz-2.0*qx*qy
grid[1,2]=2.0*qx*qz+2.0*qw*qy
grid[2,2]=1.0-2.0*qz*qz-2.0*qy*qy

VECTOR_Y#=Sqr(grid[0,0]-grid[1,1]-grid[2,2]+1.0)/2.0
VECTOR_Z#=Sqr(-grid[0,0]+grid[1,1]-grid[2,2]+1.0)/2.0
VECTOR_X#=Sqr(-grid[0,0]-grid[1,1]+grid[2,2]+1.0)/2.0
VECTOR_W#=Sqr(grid[0,0]+grid[1,1]+grid[2,2]+1.0)/2.0

Print qx+", "+qy+", "+qz+", "+qw
Print VECTOR_x+", "+VECTOR_y+", "+VECTOR_z+", "+VECTOR_w



Function EulerAsQuat(pitch#,yaw#,roll#)
	cr#=Cos(-roll#/2.0)
	cp#=Cos(pitch#/2.0)
	cy#=Cos(yaw#/2.0)
	sr#=Sin(-roll#/2.0)
	sp#=Sin(pitch#/2.0)
	sy#=Sin(yaw#/2.0)
	cpcy#=cp#*cy#
	spsy#=sp#*sy#
	spcy#=sp#*cy#
	cpsy#=cp#*sy#
	VECTOR_w#=cr#*cpcy#+sr#*spsy#
	VECTOR_x#=sr#*cpcy#-cr#*spsy#
	VECTOR_y#=cr#*spcy#+sr#*cpsy#
	VECTOR_z#=cr#*cpsy#-sr#*spcy#
End Function