Quaternions Test

Blitz3D Forums/Blitz3D Programming/Quaternions Test

Bobysait(Posted 2006) [#1]
Here are some functions found on the forum, and modified for easier use.

But, I have a problem with the conversions, axes seem to be false.
As, i 'm not really understanding quaternions, i really think i made a mistake, but i can't point on this...
If anyone could help ^^



I have to use it to get the rotation of B3D files
=> getting w,x,y,z from the parse of a b3d, and then i build the scene using createpivot and createmesh command.
All works fine, but I can't turn entitys like it should.


Bobysait(Posted 2006) [#2]
Ok??? I think, the mistake is in the QUA_EulerToQuat function...

I replaced with

Function QUA_EulerToQuat2(Pitch#, Yaw#, Roll#)
	
	Pitch# = Pitch# / 2.0
	Yaw#   = Yaw#   / 2.0
	Roll#  = Roll#  / 2.0
	
	Cos_Pitch# = Cos(Pitch#)
	Cos_Yaw#   = Cos(Yaw#)
	Cos_Roll#  = Cos(Roll#);
	
	Sin_Pitch# = Sin(Pitch#)
	Sin_Yaw#   = Sin(Yaw#)
	Sin_Roll#  = Sin(Roll#);
	
	CpCy# = Cos_Pitch# * Cos_Yaw#
	SpSy# = Sin_Pitch# * Sin_Yaw#
	SpCy# = Sin_Pitch# * Cos_Yaw#
	CpSy# = Cos_Pitch# * Sin_Yaw#

	q.QUA_Quat=Last QUA_Quat
	If q=Null q=New QUA_Quat

	q\z = ((Sin_Roll# * CpCy#) - (Cos_Roll# * SpSy#))*-1
	q\x = ((Cos_Roll# * SpCy#) + (Sin_Roll# * CpSy#))*-1
	q\y = (Cos_Roll# * CpSy#) - (Sin_Roll# * SpCy#)
	q\w = (Cos_Roll# * CpCy#) + (Sin_Roll# * SpSy#)

End Function

and it seems to walk right

... Of course, it does not explain why 3 codes for getting Euler from Quat return 3 different values


Danny(Posted 2006) [#3]
Sorry can't give you a direct answer to this, but I do know there's several versions and variations of Quaternion function libraries in the code archives! These libraries all handle the same basic functions (Euler to Quats, Quats to Eulers, Slerps, etc) so I think it's safe to say that you should be able to find a solution in one of there. Either copy one of their functions or fix/adjust your own using theirs to compare...

Hope that helps,
Danny.


Bobysait(Posted 2006) [#4]
Thank you.

I'm just wondering if the exporter B3d pipeline store good quaternions values...
=> noone of the values contained in the chunk node seem to return the same values as using any one of the quaternions lib.
Would it be right to say, b3d pipeline lib don't store good values ? or i have to use an external lib to transform values into valid ones ?


b32(Posted 2006) [#5]
I've used Leadwerks conversion:

It works very nice. However, I found another problem:

Even though I turn the entity 270 degrees, the outcome is -90. The result looks the same, but for saving animations, this means the cube will rotate in the wrong direction. It will allways take the shortest route. Not sure if this helps, but I thought I post it anyway.


Bobysait(Posted 2006) [#6]
I understand the problem it occurs. and maybe that's why quaternions i use return Euler Angles >360 or <360 and maybe it's better.

Thanks for your code, i've tried it, and the same problem happen.
One way : I looked on the milkshape exporter for b3d, and maybe the problem is that blitz3d axis are not logical.

As we can see
QUA_eulerToQuat(X#,Y#,Z#)
	rz#=x#
	rx#=y#
	ry#=z#
	r#=rz
	p#=rx#
	y#=ry#
	sp#=Sin(p/2):cp=Cos(p/2)
	sy#=Sin(y/2):cy=Cos(y/2)
	sr#=Sin(r/2):cr=Cos(r/2)
	quat[0]=cr*cp*cy + sr*sp*sy;
	quat[1]=sr*cp*cy - cr*sp*sy;
	quat[2]=cr*sp*cy + sr*cp*sy;
	quat[3]=cr*cp*sy - sr*sp*cy;

	quat[3]=-quat[3];


Mark need to convert axis to get those of blitz3d.
Maybe quaternions libraries don't rectify this. ( not sure )
but as, i get quaternions form b3d file, they have to be in the good axis... I don't know how to explain all that, but i think rotations of b3d files are not as simple as "turnEntity Rx,Ry,Rz" using local coordinates from conversion of quaternions to Euler included in the b3d file.
I found the code from Pacemaker for import/export b3d files, but it does not attempt to build a scene using the b3d files.
It seems that noone has ever try it before ? maybe, I try to do something stupid :D


b32(Posted 2006) [#7]
After reading a few topics, I understand that Blitz quaternions are different from normal quaternions. I can't seem to find out what the difference is exactly. A while ago, I was writing a X-exporter. I noticed at one point that I was doing the order of xyzw/wxyz wrong somehow. Maybe it is something similair?


Bobysait(Posted 2006) [#8]
Maybe should Mark explain it...


Bobysait(Posted 2006) [#9]
after looking MiniB3D library, i found a way !

rotations have to be converted like this :
QuatToEUler(-w,x,y,-z)
rx-pitch
ry=+yaw
rz=+roll