Implementing Quaternions

BlitzMax Modules Forums/MiniB3D/Implementing Quaternions

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

DAMN I finally got it working.

I parented one mesh to another and randomly spun the parent around on the screen. When I click the mouse, the child mesh gets parented to either the center mesh or the world. When I change the parenting, the mesh stays perfectly in place, no matter what crazy angle is happening. Here, see for yourself:
http://www.leadwerks.com/post/quat.zip

It will take some time to clean this up, but the hard part is done. I'm going to go drink now.

This extracts a quaternion from a matrix:
	Method ExtractQuat()
		fourWSquaredMinus1:Float = +grid[0,0]+grid[1,1]+grid[2,2]
		fourXSquaredMinus1:Float = +grid[0,0]-grid[1,1]-grid[2,2]
		fourYSquaredMinus1:Float = +grid[1,1]-grid[0,0]-grid[2,2]
		fourZSquaredMinus1:Float = +grid[2,2]-grid[0,0]-grid[1,1]

		biggestIndex=0
		fourBiggestSquaredMinus1:Float=fourWSquaredMinus1
		If fourXSquaredMinus1 > fourBiggestSquaredMinus1
			fourBiggestSquaredMinus1 = fourXSquaredMinus1 
			biggestIndex=1
		EndIf
		If fourYSquaredMinus1 > fourBiggestSquaredMinus1
			fourBiggestSquaredMinus1 = fourYSquaredMinus1 
			biggestIndex=2
		EndIf
		If fourZSquaredMinus1 > fourBiggestSquaredMinus1
			fourBiggestSquaredMinus1 = fourZSquaredMinus1 
			biggestIndex=3
		EndIf

		biggestValue:Float=Sqr(fourBiggestSquaredMinus1+1.0)*0.5
		mult:Float=0.25/biggestValue

		Select BiggestIndex
			Case 0
				w#=BiggestValue
				x#=(grid[1,2]-grid[2,1])*mult
				y#=-(grid[2,0]-grid[0,2])*mult
				z#=-(grid[0,1]-grid[1,0])*mult
			Case 1
				x#=-BiggestValue
				w#=-(grid[1,2]-grid[2,1])*mult
				y#=(grid[0,1]+grid(1,0))*mult
				z#=(grid[2,0]+grid[0,2])*mult
			Case 2
				y#=BiggestValue
				w#=-(grid[2,0]-grid[0,2])*mult
				x#=-(grid[0,1]+grid(1,0))*mult
				z#=(grid[1,2]+grid[2,1])*mult
			Case 3
				z#=-BiggestValue
				w#=(grid[0,1]-grid(1,0))*mult
				x#=(grid[2,0]+grid[0,2])*mult
				y#=-(grid[1,2]+grid[2,1])*mult
		EndSelect

		If w<-0.0
			x=-x
			y=-y
			z=-z
			w=-w
		EndIf

		VECTOR_X=z
		VECTOR_Y=x
		VECTOR_Z=y
		VECTOR_W=w		
	EndMethod