Jump to content
  • Advertisement
Sign in to follow this  
scottrick49

Help with quaternion rotation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!