Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quaternion Interpolation

Started by yanuart Dec 27, 2001 at 1:43 AM 3 replies 600+ views
Original Post
yanuart
yanuart
Can somebody give me the basic of what quaternion and the interpolation process so that it can make a smooth path ?? Plzzz... I''m not very good at math so any basic things will do and will be appreciated very much
Ride the thrill of your life
Play Motorama
v71
v71
sure here it comes
  
void __forceinline QuatLerp( const qVect p, qVect q, float t, qVect qt )
{
int i;
float omega, cosom, sinom, sclp, sclq;

// decide if one of the quaternions is backwards

float a = 0;
float b = 0;
for (i = 0; i < 4; i++) {
a += (p[i]-q[i])*(p[i]-q[i]);
b += (p[i]+q[i])*(p[i]+q[i]);
}
if (a > b) {
for (i = 0; i < 4; i++) {
q[i] = -q[i];
}
}

cosom = p[0]*q[0] + p[1]*q[1] + p[2]*q[2] + p[3]*q[3];

// very small value


if ((1.0 + cosom) > 0.00000001f) {
if ((1.0 - cosom) > 0.00000001f) {
omega = acos( cosom );
sinom = sin( omega );
sclp = sin( (1.0f - t)*omega) / sinom;
sclq = sin( t*omega ) / sinom;
}
else {
sclp = 1.0f - t;
sclq = t;
}
for (i = 0; i < 4; i++) {
qt[i] = sclp * p[i] + sclq * q[i];
}
}
else {
qt[0] = -p[1];
qt[1] = p[0];
qt[2] = -p[3];
qt[3] = p[2];
sclp = sin( (1.0f - t) * 0.5f * __PI);
sclq = sin( t * 0.5f * __PI);
for ( i = 0 ; i < 3 ; i++ ) {
qt[i] = sclp * p[i] + sclq * qt[i];
}
}
}


[edit: added source tags]


Edited by - Magmai Kai Holmlor on December 29, 2001 2:37:17 PM
v71
v71
Sorry i''ve posted too quickly , perhaps, the qVect actually is
simple a float a[4] array , and __PI is the venerable PI this is easy , if you have something more to ask, just do it.


yanuart
yanuart
well what are those parameters means anyway ??
what about the quartenion for the rotation ??
Ride the thrill of your life
Play Motorama
v71
v71
  
void __forceinline AngleQuaternion( fVect angles, qVect quaternion )
{
float angle;
float sr, sp, sy, cr, cp, cy;

angle = angles[2] * 0.5f;
sy = sin(angle);
cy = cos(angle);
angle = angles[1] * 0.5f;
sp = sin(angle);
cp = cos(angle);
angle = angles[0] * 0.5f;
sr = sin(angle);
cr = cos(angle);

quaternion[0] = sr*cp*cy-cr*sp*sy; // X

quaternion[1] = cr*sp*cy+sr*cp*sy; // Y

quaternion[2] = cr*cp*sy-sr*sp*cy; // Z

quaternion[3] = cr*cp*cy+sr*sp*sy; // W

}

void __forceinline QuaternionMatrix( qVect quaternion, fMatrix matrix )
{
matrix[0][0] = 1.0f - 2.0f * quaternion[1] * quaternion[1] - 2.0f * quaternion[2] * quaternion[2];
matrix[1][0] = 2.0f * quaternion[0] * quaternion[1] + 2.0f * quaternion[3] * quaternion[2];
matrix[2][0] = 2.0f * quaternion[0] * quaternion[2] - 2.0f * quaternion[3] * quaternion[1];

matrix[0][1] = 2.0f * quaternion[0] * quaternion[1] - 2.0f * quaternion[3] * quaternion[2];
matrix[1][1] = 1.0f - 2.0f * quaternion[0] * quaternion[0] - 2.0f * quaternion[2] * quaternion[2];
matrix[2][1] = 2.0f * quaternion[1] * quaternion[2] + 2.0f * quaternion[3] * quaternion[0];

matrix[0][2] = 2.0f * quaternion[0] * quaternion[2] + 2.0f * quaternion[3] * quaternion[1];
matrix[1][2] = 2.0f * quaternion[1] * quaternion[2] - 2.0f * quaternion[3] * quaternion[0];
matrix[2][2] = 1.0f - 2.0f * quaternion[0] * quaternion[0] - 2.0f * quaternion[1] * quaternion[1];
}

....please don't make me expalin the theory behind quaternions
it is too long for a post....:-(((((
i hope this function will be usefull for you


[edit: added source tags]

Edited by - Magmai Kai Holmlor on December 29, 2001 2:39:16 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.