Understanding Transforms...

Started by
3 comments, last by gimp 20 years, 11 months ago
I have an existing engine thats working out quite well. I''m usng ODE for physics and as such didn''t really need to learn too much about matricies. Now I want to add GOD mode to the engine, mostly as a prelude to creating an editor. I''ve implemented this as well but my mouse control of camera''s orientation is slightly wrong. Where is what I do: -Get User input from mouse. -Calculate Mouse Delta. -Apply the mouse delta to the camera''s orientation like this: Transform.SetYaw(m_Delta.x); Transform.SetPitch(m_Delta.y); -Go on with normal rendering. Here is an example of my SetPitch: (I stole this from Nate Millers code more or less)
  
void CMatrix::SetPitch(const float a_Degrees)
{
	CMatrix temp(CMatrix::IDENTITY);
	float Radians = Mathematics::Degrees2Radians(a_Degrees);
	float c = (float)cos(Radians);
	float s = (float)sin(Radians);

	temp.m[5]  = c;
	temp.m[6]  = s;
	temp.m[9]  = -s;
	temp.m[10] = c;

	*this = temp * *this;
}
void CMatrix::SetYaw(const float a_Degrees)
{
	CMatrix temp(CMatrix::IDENTITY);
	float Radians = Mathematics::Degrees2Radians(a_Degrees);
	float c = (float)cos(Radians);
	float s = (float)sin(Radians);

	temp.m[0] = c;   
	temp.m[2] = -s;
	temp.m[8] = s;  
	temp.m[10] = c;

	*this = temp * *this;
}
  
The odd thing that I''m getting is that when I wiggle the mouse around(say move the camera in a small circular motion) the camera slowly rolls clockwise and even though I never touch the roll it rolls anyway. Is there an error in my ocde or is this inherent in what I''m doing. If it''s inherent is there any way to lock the roll os ''up'' is always ''up''? Many thanks, Chris Brodie http:\\fourth.flipcode.com
Chris Brodie
Advertisement
Setting a pitch matrix and a yaw matrix are not quite enough. Although the code to do either is correct, you still need to multiply the 2 matrices together to get a resulting matrix.

EDIT: If you don't know how to multiply w matrices together, check out how to multiply matrices on google.

[edited by - RhoneRanger on April 28, 2003 7:37:20 AM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
??

This is the same matix the operations are being performed on. I just do one operation after another. Actually if you look inside nate''s function there I can see what your talking about, he appears to multiply these temporary matricies against the main one.

I still don''t understand why the camera slowly rolls when I wiggle the viewport around though.

Do I need to Orthonormalize?

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie
I was positively tearing my hair out on this one, I had the SAME thing.

You know what the problem is? A first person shooter does this wrong, deliberately, to create a specific camera effect.

Take a real life object, and pitch it up. Now yaw it. It has rolled! Arg!!

Anyway your camera is fine The way to yaw and pitch without rolling is to rotate around the local x axis but the world y axis. Theres a handy way to do this with a rotation matrix, but I promptly forget it hehe. I''m still having trouble picturing why it works... it seems like it would affect your position in a negative way (swinging you around the world) but it doesn''t seem to in my camera.
u can't *increment* the 2 rotations every frame unless u factor the current rotation into the calculation. as aargyle said, imagine looking down the z axis and being rotated up (or down) in the x axis - then rotate around the *local* y axis. what happens is in fact a local rotation around the y axis, but also *global* rotation around the z axis.

the way i do it is to keep seperate variables for the total rotation in x and y dimensions, and recreate the matrix if there has been mouse movement.

// Start with 0 rotation:
fltTotalXRot = 0.0f;
fltTotalYRot = 0.0f;

// Every frame:
if ( fltCurrentXDelta || fltCurrentYDelta )
{
fltTotalXRot += fltCurrentXDelta;
fltTotalYRot += fltCurrentYDelta;

// Do it yourself:
// Create matrix from Y rotation using fltTotalYRot
// Create matrix from X rotation using fltTotalXRot
// Multiply matrices together, and the result is the view matrix

// Or have opengl do it for you:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(fltTotalYRot, 0.0f, 1.0f, 0.0f);
glRotatef(fltTotalXRot, 1.0f, 0.0f, 0.0f);
}

[edited by - jorgander on May 5, 2003 7:47:35 AM]

This topic is closed to new replies.

Advertisement