Jump to content


- - - - -

Quaternion Rotations


3 replies to this topic

#1 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 13 November 2008 - 02:30 AM

I tried quaternion rotations a long time ago, and never got it working. I'm trying it again, from scratch, but seem to be having exactly the same issue: not-so-circular orbits. As a test, I'm incrementing an angle theta each frame and then each frame, using quaternions, rotate a start point by that angle and set a mesh's position as that point. What I want to happen is to see the mesh loop around in circles, depending on which axis i rotate about. I figure this way, I can't possibly have problems with imprecisions accumulating. The thing is, my orbits look more like >'s or weird double loops. I've spent days days checking and rechecking, and am quite frustrated. If you aren't familiar with Objective-C, the notation is [object method(well, message really): parameters].

Here is my relevant code:

//Rotation, done each frame
	theta = theta+0.05;
	ZQuaternion* newRotation = [[ZQuaternion alloc] initWithValues: cos(theta/2.0): sin(theta/2.0): 0.0: 0.0];
	//ZQuaternion* newRotation = [[ZQuaternion alloc] initWithValues: cos(theta/2.0): 0.0: sin(theta/2.0): 0.0];
	//ZQuaternion* newRotation = [[ZQuaternion alloc] initWithValues: cos(theta/2.0): 0.0: 0.0: sin(theta/2.0)];
	
	//point to rotate
	ZQuaternion* qposition = [[ZQuaternion alloc] initWithValues: 0.0: 0.3: 0.1: 0.6]; 
	ZLeaf* mesh0 = [rootNode childAt: 0];
	ZVector3* position = [mesh0 localPosition];
	ZQuaternion* newQLocation = [newRotation multiply: [qposition multiply: [newRotation conjugate]]];
	position.x = newQLocation.x;
	position.y = newQLocation.y;
	position.z = newQLocation.z;	

//Quaternion Class:
#import "zquaternion.h"

@implementation ZQuaternion
@synthesize w;
@synthesize x;
@synthesize y;
@synthesize z;

-(id) initWithValues: (float) a: (float) b: (float) c: (float) d {
	if ((self = [super init])) {
		w = a;
		x = b;
		y = c;
		z = d;
    }
    return self;
}

-(float*) getCArray	{
	float* cArray = malloc(4*sizeof(float));
	*(cArray+0) = w;
	*(cArray+1) = x;
	*(cArray+2) = y;
	*(cArray+3) = z;
	return cArray;
}

-(ZQuaternion*) conjugate	{
	return [[ZQuaternion alloc] initWithValues: w: -x: -y: -z];
}

-(void) unitize	{
	float magnitude = sqrt( (w*w) + (x*x) + (y*y) + (z*z) );
	w /= magnitude;
	x /= magnitude;
	y /= magnitude;
	z /= magnitude;
}

-(ZQuaternion*) multiply: (ZQuaternion*) q	{
	float a = (w*q.w) - (x+q.x) - (y*q.y) - (z*q.z);
	float b = (w*q.x) + (x*q.w) + (y*q.z) - (z*q.y);
	float c = (w*q.y) - (x*q.z) + (y*q.w) + (z*q.x);
	float d = (w*q.z) + (x*q.y) - (y*q.x) + (z*q.w);
	return [[ZQuaternion alloc] initWithValues: a: b: c: d];
}
@end

Thanks for your help,
-Tobey

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 13 November 2008 - 03:05 AM

You've got a typo in your multiply routine:

float a = (w*q.w) - (x+q.x) - (y*q.y) - (z*q.z);

should have a * in the second term, not a +.

Besides that, all your code looks good to me.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 13 November 2008 - 03:35 AM

Reedbeta,
You are awesome, and much less lexdistic than I. I can't freaking believe I didn't catch that! It works perfectly now.

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 13 November 2008 - 04:24 AM

Glad to hear. Sometimes you just need a second pair of eyes; spend too much time looking at a piece of code and you start seeing what you think is there instead of what's actually there. Happens to everyone. ;)
reedbeta.com - developer blog, OpenGL demos, and other projects





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users