Page 1 of 1

Irrlicht NOT Using Quaternions?

Posted: Thu Jul 29, 2004 11:46 am
by Robomaniac
I just noticed something. Even though niko added Quaternions to the engine, from what i've seen, he's not using them for rotation. At least not for nodes and stuff. If he used them for orientation, things like moving a node relative to its curent position is a breeze :)

Just a thought!

Posted: Thu Jul 29, 2004 12:42 pm
by Electron
Yeah, I probably agree. I find matrices easier to understand then quaternions, but quaternions are definitely better then matrices for rotation (no nasty gimbal lock, which is definitely a downside of working with matrices)
One point though, OpenGL has got internal support for matrices but I believe not quaternions (dunno about dx) thoguh I haven't looked in the innards of the engine enough to know if niko's even using OGL for any transforms, so that migh not be an issue

Posted: Thu Jul 29, 2004 9:08 pm
by AssiDragon
Depends. While there is gimbal lock in matrices, quaternations is something that I don't think I will ever understand deeper than "they are something that avoids gimbal lock and uses 4x4 matrix" :D
"Seeing" in 4 dimensions is just not my style. :/

Posted: Fri Jul 30, 2004 8:34 am
by zola
You don't have to understand the quaternions. All You have to know is the methods the quaternion class provides and what they are used for, and some basic stuff:

1) normalised Quaternions can be used for rotations around an arbitrary axis

2) Quaternions can be converted to and from rotation matrices

3) Quaternions can be converted to and from euler angles

4) rotating a vector with a Quaternion : vec' = Quat * vec

5) concatenating 2 rotations: Quat = Quat2 * Quat1
Quat is the result of applying first the rotation Quat1 and then the rotation Quat2!

Now comes the fun part
:) the quaternion class in irrlicht is nearly useless because it doesn't provide the needed functions to use quaternions in a meaningful way.

I miss these functions the most:

void Quaternion.FromAngleAxis(float angle, Vector3 axis)

Vector3 Quaternion.toEuler()

Vector3 operator*(const Vector3& v)

But here comes the remedy:

Code: Select all

void quaternion::FromAngleAxis (const f32& angle, const vector3df& axis)
	{
		// assert:  axis is unit length
		//
		// The quaternion representing the rotation is
		//   q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)

		f32 fHalfAngle = 0.5*angle;
		f32 fSin = sin(fHalfAngle);
		w = cos(fHalfAngle);
		x = fSin*axis.X;
		y = fSin*axis.Y;
		z = fSin*axis.Z;
	}

Code: Select all

void quaternion::ToEuler(vector3df& euler){
		double sqw = W*W;    
		double sqx = X*X;    
		double sqy = Y*Y;    
		double sqz = Z*Z; 
		// heading == rotaton about z-axis
		euler.Z = (f32) (atan2(2.0 * (X*Y +Z*W),(sqx - sqy - sqz + sqw)));
		// bank == rotation about x-axis
		euler.X = (f32) (atan2(2.0 * (Y*Z +X*W),(-sqx - sqy + sqz + sqw)));  
		// attitude == rotation about y-axis
		euler.Y = (f32) (asin(-2.0 * (X*Z - Y*W)));
	}

Code: Select all

vector3df quaternion::operator* (const vector3df& v) const
	{
		// nVidia SDK implementation
		vector3df uv, uuv; 
		vector3df qvec(X, Y, Z);
		uv = qvec.crossProduct(v); 
		uuv = qvec.crossProduct(uv); 
		uv *= (2.0f * W); 
		uuv *= 2.0f; 

		return v + uv + uuv;

	}
good day good sirs

Posted: Fri Jul 30, 2004 12:50 pm
by Electron
thanks for those functions

Posted: Sat Jul 31, 2004 8:33 am
by niko
Thanks for the methods, maybe I'll add them if its ok for you.
If anybody wants a scene node which uses quaternions for rotation instead of matrices, just override ISceneNode and implement it. :)

Posted: Sat Sep 04, 2004 1:46 am
by saigumi
Looks like I may be adding it myself soon enough.

Been a boid demo and want to update each boid based on its rotation and speed and everything out there says "use the orientation quaternion"

Posted: Sat Sep 04, 2004 2:05 am
by Not Oz
euler angles = gimbal lock

Yes we want this please niko! Were too stupid to do it ourselves. :)