# Help with quaternion rotation

This topic is 4010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to use quaternions to represent orientation, and i'm pretty close to having it working, but something is not quite right. If my program draws a model on the screen and I only rotate the model, all seems to be well. But, the moment I start to move the model so it bounces back and forth while its turning, the rotation doesn't seem to happen evenly anymore. It will be faster when the model is moving in one direction and slower in the other. And then after a while of the program running, the model seems to gradually get distorted somehow and then gradually get better. Here is the code where the models rotation and position are updated:
void TestObject::Refresh(long CurrentTime)
{
VECTOR3 *pos = &(m_pNode->m_PosQuat.pos);
Quat *quat = &(m_pNode->m_PosQuat.quat);

//update the position with some crappy bouncing thing
if (bXUp)
{
if (pos->x <= 2.0f)
{
pos->x += 0.005f;
}
else
{
bXUp = !bXUp;
}
}
else
{
if (pos->x >= -2.0f)
{
pos->x -= 0.005f;
}
else
{
bXUp = !bXUp;
}
}

//rotate by small amount around the y axis every time
Quat rotation;
rotation.CreateFromRotation(0.003f, 0.0f, 1.0f, 0.0f);
*quat = *quat * rotation;
}

The CreateFromRotation function:
void Quat::CreateFromRotation(float angle, float xAxis, float yAxis, float zAxis)
{
w = cos(angle / 2.0f);
x = xAxis * sin(angle / 2.0f);
y = yAxis * sin(angle / 2.0f);
z = zAxis * sin(angle / 2.0f);
}

The objects render function:
        glPushMatrix();
glColor4f(m_vColor.x, m_vColor.y, m_vColor.z, m_vColor.w);

//translate to the correct position
glTranslatef(m_PosQuat.pos.x, m_PosQuat.pos.y, m_PosQuat.pos.z);

//rotate to correct orientation
glMultMatrixf(m_PosQuat.quat.ToMatrix().m);

//scale to correct size
glScalef(m_vScale.x, m_vScale.y, m_vScale.z);

//render
glutWireTeapot(1.0);

//render any children

glPopMatrix();

Thanks!

##### Share on other sites
one of the issues could be that you are repeatedly multiplying your orientation quaternion here so may be introducing precision problems.

where you do this:

rotation.CreateFromRotation(0.003f, 0.0f, 1.0f, 0.0f);

you could try something like this:

static float angle = 0.0f;
angle += 0.003f;
rotation.CreateFromRotation(angle, 0.0f, 1.0f, 0.0f);
*quat = rotation;

##### Share on other sites
I still get the same thing when I do it that way. Thanks, though.

##### Share on other sites
Just to check, what happens if

1. you fix this at a non-zero position
glTranslatef(m_PosQuat.pos.x, m_PosQuat.pos.y, m_PosQuat.pos.z);

2. you comment this line out
glMultMatrixf(m_PosQuat.quat.ToMatrix().m);

##### Share on other sites
I will look into it tonight and post back when I see what happens. Thanks.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009811
• ### Who's Online (See full list)

There are no registered users currently online

×