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

rotations

Started by cjtho6 May 22, 2003 at 12:56 AM 5 replies 2.2k views
Original Post
cjtho6
cjtho6
hello, I am trying to get my mesh rotations working but my following code doesn''t work as I expected. It works fine if I change just the yaw pitch or roll, but not if I change more than one at a time. If I cange more than the one the mesh moves erratically even when I stop changing the values. In my render function I set the world matric to &m_MTransform, but I think I might be multiplying my marices wrong or something. Sny help is appreciated
  
	D3DXVec3Normalize( &m_LookAt, &m_LookAt );
	D3DXVec3Cross( &m_Right, &m_Up, &m_LookAt );
	D3DXVec3Normalize( &m_Right, &m_Right );
	D3DXVec3Cross( &m_Up, &m_LookAt, &m_Right );
	D3DXVec3Normalize( &m_Up, &m_Up );
	
	D3DXMATRIX mPitch, mRoll, mYaw;
	// Set up the y-axis rotation

	D3DXMatrixRotationAxis( &mYaw, &m_Up, m_Yaw );
	D3DXVec3TransformCoord( &m_LookAt, &m_LookAt, &mYaw );
	D3DXVec3TransformCoord( &m_Right, &m_Right, &mYaw );

	// Set up the x-axis rotation

	D3DXMatrixRotationAxis( &mPitch, &m_Right, m_Pitch );
	D3DXVec3TransformCoord( &m_LookAt, &m_LookAt, &mPitch );
	D3DXVec3TransformCoord( &m_Up, &m_Up, &mPitch );

	// Set up the z-axis rotation

	D3DXMatrixRotationAxis( &mRoll, &m_LookAt, m_Roll );
	D3DXVec3TransformCoord( &m_Right, &m_Right, &mRoll );
	D3DXVec3TransformCoord( &m_Up, &m_Up, &mRoll );
	m_Velocity = m_LookAt * m_Speed;
	m_Position += m_Velocity;

	D3DXMatrixMultiply(&m_MTransform, &mRoll, &mPitch);
	D3DXMatrixMultiply(&m_MTransform, &m_MTransform, &mYaw);
  
cjtho6
cjtho6
bump
cjtho6
cjtho6
Hi, I still can''t get my rotations to work. I tried using quaternions instead but I still get the problem of after rotating more than 1 axis for a while the mesh seems to jump around crazily. Even after I stop inputting it still does this.

here is the code I use, please help


  
D3DXQUATERNION pQy, pQp, pQr, pQ;
D3DXQuaternionIdentity(&pQ);
D3DXQuaternionRotationAxis( &pQy, &m_Up , m_Yaw );
D3DXQuaternionRotationAxis( &pQp, &m_Right, m_Pitch );
D3DXQuaternionRotationAxis( &pQr, &m_LookAt , m_Roll );

pQ = pQy * pQp * pQr;

/
// Quaternion to Matrix

D3DXVECTOR3 pAxis;
float pAngle;
D3DXQuaternionToAxisAngle( &pQ, &pAxis, &pAngle );
D3DXMatrixRotationAxis( &m_MTransform, &pAxis, pAngle );

m_MTransform._41 = m_Position.x;
m_MTransform._42 = m_Position.y;
m_MTransform._43 = m_Position.z;


// Now extract the direction out of the matrix

m_LookAt.x = m_MTransform._31;
m_LookAt.y = m_MTransform._32;
m_LookAt.z = m_MTransform._33;

// Right vector

m_Right.x = m_MTransform._11;
m_Right.y = m_MTransform._12;
m_Right.z = m_MTransform._13;

// Up vector

m_Up.x = m_MTransform._21;
m_Up.y = m_MTransform._22;
m_Up.z = m_MTransform._23;

// Normalize and Regenerate the Look, Right, and Up Vectors

D3DXVec3Normalize( &m_LookAt, &m_LookAt );
D3DXVec3Cross( &m_Right, &m_Up, &m_LookAt );
D3DXVec3Normalize( &m_Right, &m_Right );
D3DXVec3Cross( &m_Up, &m_LookAt, &m_Right );
D3DXVec3Normalize( &m_Up, &m_Up );

g_pDevice->SetTransform( D3DTS_WORLD, &m_MTransform );
The Grappler
The Grappler
you will probably find that creating three seperate Quats for a
rotation will be the same as D3DXQuaternionRotationYawPitchRoll
anyway

so instead of regenerating your quat each time you change an
axis rotation just multiply your existing quat by that amount.

Ie. your first call will probably like..
D3DXQuaternionRotationYawPitchRoll( &m_qOrientation, 0.0f, 0.0f, 0.0f );

then each rotation you do, something like

VOID AssClass::Rotation( float fYaw, float fPitch, float fRoll )
{
D3DXQUATERNION qNewRot;
D3DXQuaternionRotationYawPitchRoll( &qNewRot, fYaw, fPitch, fRoll );
D3DXQuaternionMultiply( &m_qOrientation, &m_qOrientation, &qNewRot );
}
^ - just a note about that, you really should normalize all your Quaternion calculations, quats should always be unit length


to get the rotation matrix simply

D3DXMatrixRotationQuaternion( &mDirection, &m_qOrientation );

then multiply this matrix by your position...

D3DXMatrixTranslation( &mTranslation, &-m_Position.x, &-m_Position.y, &-m_Position.z );

D3DXMatrixMultiply( &mView, &mTranslation, &mDirection );

then mView should be what you want.


if you want to get the Up, Right and Forward vectors then
do use the Direction matrix, transpose it and do a Vec3Transform using unit length direction vectors, ie (0.0f, 0.0f, 1.0f),(0.0f, 1.0f, 0.0f) and (1.0f,0.0f,0.0f)

is this what you wanted?

(also, this is probably stupid of me but just checking, you
are using Radians as yaw,pitch,roll rotations?)
Some of you are not real.
cjtho6
cjtho6
quote:
Original post by The Grappler
you will probably find that creating three seperate Quats for a
rotation will be the same as D3DXQuaternionRotationYawPitchRoll
anyway

so instead of regenerating your quat each time you change an
axis rotation just multiply your existing quat by that amount.


won''t this rotate around the world axis and not the local axis?

Ie. your first call will probably like..

quote:

^ - just a note about that, you really should normalize all your Quaternion calculations, quats should always be unit length


I will try normalizeing my quaternions

I use a float for my angle and increment it by .001 each time you push a key. Do I need to wrap these around when it reaches 2 PI ?

thanks for your help
The Grappler
The Grappler
> won''t this rotate around the world axis and not the local axis?

no. this is the local orientation.
if you multiply the quats in the correct order, it is like
rotating the current orientation, not spinning around the
world axis - thats why you must keep an ongoing quat not keep creating it from X,Y,Z rotations.


the only "incorrect" effect of using quats like this is that
you appear to get a z-roll after rotating x and y repetatively.
however - i have read many articles about this and although this happens and looks wrong - supposedly it is the correct way it should work - if you are making a first person shooter however - z-rolls will not trouble you as usually you will want an Up(0.0f,1.0f,0.0f) view perspective anyway.
Some of you are not real.
cjtho6
cjtho6
thanks, I got it working now, works perfectly.

Topic Locked

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

Sign in to reply to this topic.