Turning a matrix locally

BlitzMax Forums/BlitzMax Programming/Turning a matrix locally

JoshK(Posted 2008) [#1]
I am having a hard time getting a matrix to turn locally. I have tried a lot of combinations of quat and matrix multiplications, but am having no luck. Can anyone help?

The global turning is easy. But I am trying to make the matrix turn on its own axes:

	Method Turn(r:TVec3,glob=0)
		If glob
			quaternion=quaternion.times(r.toquat())
			rotation=quaternion.toeuler()
		Else
			Local q:TQuat
			q=r.toquat()
			q=q.times(mat.inverse().rotation())
			'qmat=qmat.times(mat)
			'q=qmat.rotation()
		'	Local q:TQuat=r.toquat()
			'q=qmat.rotation()'.Times(q)
		'	'qmat=qmat.times(mat)
			quaternion=quaternion.times(q)
			rotation=quaternion.toeuler()
			'rotation=rotation.plus(r)
			'quaternion=rotation.toquat()
		EndIf
		UpdateMatrix()
	EndMethod



JoshK(Posted 2008) [#2]
Well, I figured it out:
	Method Turn(r:TVec3,glob=0)
		If glob
			quaternion=quaternion.times(r.toquat())
			rotation=quaternion.toeuler()
		Else
			quaternion=r.toquat().inverse().times(mat.rotation()).normalize()
			rotation=quaternion.toeuler()
		EndIf
		UpdateMatrix()
	EndMethod


See my Mat4->Quat code in the brl.matrix thread. It is very disturbing that declaring those locals in the middle of the function totally changes the results.

Normalizing the quaternion is very important, or you get a kind of degrading accuracy that causes errors.


Damien Sturdy(Posted 2008) [#3]
Darn you! finding simpler solutions to problems I solved months ago. You should see my 25 line solution! haha.

(Naw, It's not that big anymore, but it was for a long time.) Glad you resolved the issue.

Normalising is required because without it the matrix slowly ruins itself as you stated- you'll see strange stretching/scaling of the object it is applied to as vector lengths slowly grow above/below 1 due to floating point innaccuracy.

Beware that even if you are normalising you will still get some innacuracy (a 90 degree turn may not precisely be 90 degrees, so the inaccuracy grows the more you turn it.)


JoshK(Posted 2008) [#4]
Mark's vector and matrix code he posted is pretty good. After working with it a while, I realized complicated transformations can be solved in just a couple lines of code.

The problem I had was that Mark's quaternion extraction code was wrong. I replaced it with my own I figured out from a math book.