Archived

This topic is now archived and is closed to further replies.

Nibbles

Euler to Quat, then rotate

Recommended Posts

Hi, K, I''m trying to implement quaternion rotations, and I''m starting out with eulers. I can convert my eulers to quat''s using the following code, but i''m not sure what i have to do before/after it. (code is from gamasutra)
  
EulerToQuat(float roll, float pitch, float yaw, QUAT * quat)
{
	float cr, cp, cy, sr, sp, sy, cpcy, spsy;


        // calculate trig identities

        cr = cos(roll/2);

	cp = cos(pitch/2);
	cy = cos(yaw/2);


	sr = sin(roll/2);
	sp = sin(pitch/2);
	sy = sin(yaw/2);
	
	cpcy = cp * cy;
	spsy = sp * sy;


	quat->w = cr * cpcy + sr * spsy;
	quat->x = sr * cpcy - cr * spsy;
	quat->y = cr * sp * cy + sr * cp * sy;
	quat->z = cr * cp * sy - sr * sp * cy;
}

  
Thanks, Scott Email Website
"If you try and don''t succeed, destroy all evidence that you tried."

Share this post


Link to post
Share on other sites
Ok, so you have your axis quaternion (the one that EulerToQuat gave you). Next, you need a point, and an angle. The point you choose is obviously the point the is going to be rotated about the axis, and the angle is the amount of rotation that is going to take place.

Now, if I''m get this wrong, I''m sorry, It''s been a while.

First, I have to define some way of multiplying quaternions

  
void QuaternionMultiply(QUAT * q, QUAT * p, QUAT * h)
{
h->w = ((q->w * p->w) - (q->x * p->x) - (q->y * p->y) - (q->z * p->z))
h->x = ((q->w * p->x) + (q->x * p->y) + (q->y * p->z) - (q->z * p->w))
h->y = ((q->w * p->y) - (q->x * p->z) + (q->y * p->w) + (q->z * p->x))
h->z = ((q->w * p->z) + (q->x * p->w) - (q->y * p->x) + (q->z * p->y))

return h;
}


Now we''ll need another function to turn a quaternion into it''s inverse

  
void QuaternionInverse(QUAT * q)
{
q->x = -q->x;
q->y = -q->y;
q->z = -q->z;

/* Not needed if the quaternion is already normalized
float norm = sqrt((q->w * q->w) + (q->x * q->x) + (q->y * q->y) + (q->z * q->z));

q->w /= norm;
q->x /= norm;
q->y /= norm;
q->z /= norm;
*/

}

Now a function to rotate a point about the rotate quaternion

  
void QuaternionRotate(QUAT p, QUAT a, QUAT * out)
{
QUAT mult;

QuaternionMultiply(&a, &p, &mult);
QuaternionInverse(&a);
QuaternionMultiply(&mult, &a, out);
}

To get p and a, use this:

  
p.w = 0;
p.x = point.x;
p.y = point.y;
p.z = point.z;

a.w = sin(angle / 2);
a.x = rot.x * cos(angle / 2);
a.y = rot.y * cos(angle / 2);
a.z = rot.z * cos(angle / 2);


Where point is the vector of the point to be rotated, rot quaternion returned with EulerToQuat, and angle is the amount of rotation in radians.

To get your final point, use out.x, out.y, and out.z

I hope that''s corret =P

NickW

Share this post


Link to post
Share on other sites
wow, a reply! thank god i''m patient

thanks, I finally got this working!!

now, how difficult is it to add movement?

Thanks again!
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."

Share this post


Link to post
Share on other sites