Jump to content
  • Advertisement

Archived

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

Xeee

A problem with 3rd Person View

This topic is 5988 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

currently, i'm workin' on a game where i need a 3rd person View for the Character you play with ... and i'm facing problems with some angles , i asked and i found out that it was something called Gimbal lock , so i spent about 2 days learning about quaternion (which i was told that it is the only solution) , and after i made a small library for Quat's , and i changed the code in the Render() , i faced the same problem , so please could anybody tell me what's wrong with my code ? also i want to know , is this the proper way to make a 3rd person view?? anyway , look at the code and give me your comments .
    
void Render()
{
	Quaternion q;
	Matrix m;
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
	
	glTranslatef(0, -2.0, -7.0);

        Character->Draw();

	glPushMatrix();
		glLoadIdentity();
		glTranslatef(0, -2.0, -7.0);
		
		ResetQuaternion(&q);
		q = CreateQuaternionFromEuler(- world.xAngle, - world.yAngle, - world.zAngle);
		q = NormalizeQuaternion(&q);
		ConvertQuaternionToMatrix(&q, m);
		glMultMatrixf(m);
		
		/*
		glRotatef(- world.xAngle, 1, 0, 0);
		glRotatef(- world.yAngle, 0, 1, 0);
		glRotatef(- world.zAngle, 0, 0, 1);
		*/		
		//int error = glGetError();

		glTranslatef(- world.position.x, - world.position.y, - world.position.z);
		level->Render();
	glPopMatrix();
}
    
xee.. [edited by - xeee on May 29, 2002 9:07:16 PM] [edited by - xeee on May 29, 2002 9:08:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
Converting a euler to a quaternion to a matrix is the same as converting a euler to a matrix (what you were doing before, presumably).

The point of quats is to multiply them by each other. Store a quat for your camera''s rotation, then multiply it by a quat representing the desired rotation, then convert that to a matrix with each frame.


____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
i think that's what CreateQuaternionFromEuler() does , here's it's code , please take a look and tell me what to modify

      
Quaternion CreateQuaternionFromEuler3(const float xAngle, const float yAngle, const float zAngle)
{
Quaternion q, qx, qy, qz, qzqy;

float halfThetax = toradian(xAngle/2);
float halfThetay = toradian(yAngle/2);
float halfThetaz = toradian(zAngle/2);

SETQUATVALUES(qx, tCos(halfThetax), tSin(halfThetax), 0, 0);
SETQUATVALUES(qy, tCos(halfThetay), 0, tSin(halfThetay), 0);
SETQUATVALUES(qz, tCos(halfThetaz), 0, 0, tSin(halfThetaz));

qzqy = MultiplyQuaternions(&qz, &qy);
q = MultiplyQuaternions(&qzqy, &qx);

return q;
}


Actually that's not the exact function i'm using , the one i'm using is really faster and it's taken from an article about Quaternions in gamastura , but both have the same effect and the same problem , i've tried , anyway this function looks alot more organized .
Thanks for Paying Attention

[edited by - xeee on May 29, 2002 10:21:11 PM]

Share this post


Link to post
Share on other sites
It''s not the same. The euler to quat function looks right, however you need to change the way you''re using quats. Let me reclarify:

Converting a euler to a quat and then the quat to a matrix, as you''re doing, is exactly the same as converting a euler to a matrix (except it''s slower). You''re not really using quaternions at all.

To use quats for a purpose, you have to multiply them by each other. A quat represents a rotation.

For example:

Your camera''s rotation is stored in a quaternion. With each frame, you multiply it by a rotation change quaternion (for example, 10 degrees about the camera''s up vector). The rotated camera quaternion can then be converted to a rotation matrix, which you can multiply your translated modelview matrix by.

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
i got ya , but I have a couple of questions .
first , what's the identity quat which means no rotation (is it all zero except for w is 1)
second , how to form/create that other quat that'll b multiplied by the camera's quat each frame (can i use CreateQuaternionFromAngleAxis() ?)


[edited by - xeee on May 29, 2002 10:27:31 PM]

Share this post


Link to post
Share on other sites
Only one more Question , for Creating the Quat that'll b multiplied by the Camera's Quat , can i use CreateQuaternionFromEuler() that i've used b4 ???
also , should i Load the identity Quat for the camera every frame b4 doing anything?



[edited by - xeee on May 29, 2002 11:03:49 PM]

Share this post


Link to post
Share on other sites
Don't load the identity quat for the camera. The point is to store the camera's rotation in the quaternion.

Yes, you can use create quat from euler.

BTW, your createquatfromeuler function has a problem: You're returning an object from it which will be removed from the stack when the function returns. I suspect you're doing the same with some of your other functions. What you should be doing is passing a pointer or reference to the calculated quaternion to the function.

ie

void createQuatFromEuler(const float xAngle, const float yAngle, const float zAngle, Quaternion * quatOut)


you won't need the * if Quaternion is a typedef for an array of floats.

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on May 30, 2002 9:29:49 AM]

Share this post


Link to post
Share on other sites

  • 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!