pitch, roll n yawn !

Started by
2 comments, last by sipickles 18 years, 10 months ago
Hi, I am getting bogged down in my moving object manipulation in my app. My camera class is working well, so i'd thought i'd extend a version of that for my moving objects, eg plane. Here's the code i am using, I am storing the direction the plane is pointing in.
void Cobject::pitch(float rads){

	//Pitch about right vector.
	D3DXMATRIX R;
	D3DXMatrixRotationAxis(&R, &mRight, rads);
	D3DXVec3TransformCoord(&mLook, &mLook, &R);
	D3DXVec3TransformCoord(&mUp, &mUp, &R);

	//update object direction
	m_fPitch += rads;
}

void Cobject::yaw(float rads){

	//Yaw about up vector.
	D3DXMATRIX R;
	D3DXMatrixRotationAxis(&R, &mUp, rads );
	D3DXVec3TransformCoord(&mLook, &mLook, &R);
	D3DXVec3TransformCoord(&mRight, &mRight, &R);

	//update object direction
	m_fYaw += rads;
}

void Cobject::roll(float rads){

	//Roll about look vector.
	D3DXMATRIX R;
	D3DXMatrixRotationAxis(&R, &mLook, rads );
	D3DXVec3TransformCoord(&mRight, &mRight, &R);
	D3DXVec3TransformCoord(&mUp, &mUp, &R);

	//update object direction
	m_fRoll += rads;
}


Then I am just using this code to render the mutha:
void Cobject::render()
{
	D3DXMATRIX matObjSpin;
	D3DXMATRIX matObjTrans;
	D3DXMATRIX matObj;

	D3DXMatrixRotationYawPitchRoll( &matObjSpin, m_fYaw, m_fPitch, m_fRoll );
	D3DXMatrixTranslation( &matObjTrans, mPos.x, mPos.y, mPos.z );
	D3DXMatrixMultiply( &matObj, &matObjSpin, &matObjTrans );

	m_lpDevice->SetTransform( D3DTS_WORLD, &matObj );
			
	Mobj->render();

}

My problem is that once the model has moved out of the normal XYZ alignment, further rotations are still occuring around the XYZ axes and not the models look, up and right axes. This leads to odd movement to say the least. Am I failing to update them correctly or something? Thanks in advance Simon
Advertisement
Im not sure if this is 'it' but give this a try.

    D3DXMATRIX old;    D3DXMATRIX matWorld;            d3d_device->GetTransform(D3DTS_WORLD, &old);                  //translate and rotate matWorld        d3d_device->SetTransform( D3DTS_WORLD, &matWorld );        //Render the trans/rot'ed object        d3d_device->SetTransform(D3DTS_WORLD, &old);      //Restore the old transform.


When I did a similar thing I did it a very different way. First I did it using euler angles(basicaly what your doing) only I didnt use the RollPitchYaw function. I used matrixmultiply. Then I went on to try quaternions for rotation,
I found this to be the most useful and straightforward approach.

if you want to see the code I used ill show it to ya...
What you need to do after you rotate the model is re-make the look at matrix. So you first build a rotation matrix based on yaw-pitch-roll, then you multiply it by your current LookAt, then you rebuild the look at.

Here is the source to do that, I explain how it works in this post clicky
	// Rebuild the look at	math::Vector3 xAxis;	math::Vector3 yAxis;	math::Vector3 zAxis( m_local._31, m_local._32, m_local._33 );	xAxis = math::Vector3CrossProduct( math::Vector3(0,1,0), zAxis );	math::Vector3Normalize( xAxis, xAxis );	yAxis = math::Vector3CrossProduct( zAxis, xAxis );	math::Vector3Normalize( yAxis, yAxis );	// Set the x	m_local._11 = xAxis.x;	m_local._12 = xAxis.y;	m_local._13 = xAxis.z;	// Set the y	m_local._21 = yAxis.x;	m_local._22 = yAxis.y;	m_local._23 = yAxis.z;	// Set the z	m_local._31 = zAxis.x;	m_local._32 = zAxis.y;	m_local._33 = zAxis.z;
I realise upon close inspection that the model is moving correctly, however it is not rotating as it turns, leading to confusing visual effect.

So I need to improve the line in my code which sets the mesh rotation matrix:

D3DXMatrixRotationYawPitchRoll( &matObjSpin, m_fYaw, m_fPitch, m_fRoll );


Can I do:

LookAt - Position

then use the result to provide the angles for the yawpitchroll D3DX func?

Problem is lookat and position are vec3's and YPR is matrix!

errr....

This topic is closed to new replies.

Advertisement