object rotations

Started by
3 comments, last by Sc4Freak 17 years, 2 months ago
Hello, At first... sorry for my english... Well... I'm trying to develop a directx application to move a simple object (like a square) in the world space... This movement consist of all kind of rotation (yaw, pitch and roll) and the translation. I created an object class which contains some methods, variables and four versores (vPos, vUp, vRight, vLook) to store transformations object's informations... When user pushes the left and right keys I want to execute yaw rotation around the square's local Y-axis... I associated left key with this method:
void CObject::YawLeft(void)
{
	yaw -= rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vUp, -rotationSpeed);
	// rotate another axis around Y-axis
	D3DXVec3TransformCoord(&vRight, &vRight, &m);
	D3DXVec3TransformCoord(&vLook, &vLook, &m);

}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
Similarly for right key:
void CObject::YawRight(void)
{
	yaw += rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vUp, rotationSpeed);
	// rotate another axis around Y-axis
	D3DXVec3TransformCoord(&vRight, &vRight, &m);
	D3DXVec3TransformCoord(&vLook, &vLook, &m);
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
When user pushes the 'A' and 'D' keys I want to execute roll rotation around the square's local Z-axis... for A key:
void CObject::RollLeft(void)
{
	roll += rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vLook, rotationSpeed);
	// rotate another axis around Z-axis
	D3DXVec3TransformCoord(&vUp, &vUp, &m);
	D3DXVec3TransformCoord(&vRight, &vRight, &m);
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
and for D key:
void CObject::RollRight(void)
{
	roll -= rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vLook, -rotationSpeed);
	// rotate another axis around Z-axis
	D3DXVec3TransformCoord(&vUp, &vUp, &m);
	D3DXVec3TransformCoord(&vRight, &vRight, &m);
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
For pitch:
void CObject::PitchUp(void)
{
	pitch -= rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vRight, -rotationSpeed);
	// rotate another axis around X-axis
	D3DXVec3TransformCoord(&vUp, &vUp, &m);
	D3DXVec3TransformCoord(&vLook, &vLook, &m);

	rota=2;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

void CObject::PitchDown(void)
{
	pitch += rotationSpeed;
	D3DXMATRIX m;
	D3DXMatrixRotationAxis(&m, &vRight, rotationSpeed);
	// rotate another axis around X-axis
	D3DXVec3TransformCoord(&vUp, &vUp, &m);
	D3DXVec3TransformCoord(&vLook, &vLook, &m);

	rota=2;
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
For movements I tried this:
void CObject::MoveRight(void)
{
	vPos += vRight * movementSpeed;  
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

void CObject::MoveLeft(void)
{
	vPos -= vRight * movementSpeed; 
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

void CObject::MoveForward(void)
{
	vPos += vLook * movementSpeed;  
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

void CObject::MoveBackward(void)
{
	vPos -= vLook * movementSpeed; 
}
//°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
Finally this is the code in the render function:
......
D3DXMATRIX world;
D3DXMATRIX trans;
D3DXMATRIX rot,rot2,rot3;

D3DXMatrixTranslation(&trans, vPos.x, vPos.y, vPos.z);
D3DXMatrixRotationAxis(&rot, &vUp, yaw);
D3DXMatrixRotationAxis(&rot2, &vRight, pitch);	
D3DXMatrixRotationAxis(&rot3, &vLook, roll);

world = rot * rot2 * rot3 * trans;

pD3DDevice->SetTransform(D3DTS_WORLD, &world);
......
Well... this code works fine for one rotation... but for other rotations the code doesn't work as I want!!! Please, how can I fix this problem? thx.
Advertisement
Two questions:

1) What is the second object doing? If the first one works, try commenting that one out and see if the second works, if so, then I would make sure your not sharing values or parenting one's world to another.

2) Why not just use a single matrix for the object... your transformation matrix, which contains all the rotations, scale, and translations. It seems like this way your doing a lot more calculations than you have to. Plus, even if the object doesn't move, on render your recalculating the world matrix.

Look at functions like:
D3DXMatrixRotationY
D3DXMatrixRotationX
D3DXMatrixRotationZ
etc...

Jeff.
Thx for your reply...

1) In my project there is only one object... for this object I need to implement all kind of rotations and translations... my problem is that I managed to make only one rotation at time (ex. yaw), if I try to add another kind of rotation for the same object (ex. roll and pitch) the actions together don't work.
Perhaps is it because I call this statements for every frame:
......
D3DXMATRIX world;
D3DXMATRIX trans;
D3DXMATRIX rot,rot2,rot3;

D3DXMatrixTranslation(&trans, vPos.x, vPos.y, vPos.z);
D3DXMatrixRotationAxis(&rot, &vUp, yaw);
D3DXMatrixRotationAxis(&rot2, &vRight, pitch);
D3DXMatrixRotationAxis(&rot3, &vLook, roll);

world = rot * rot2 * rot3 * trans;

pD3DDevice->SetTransform(D3DTS_WORLD, &world);
......

2)I don't know how to do it using only one matrix...

those functions:
D3DXMatrixRotationY
D3DXMatrixRotationX
D3DXMatrixRotationZ

I think they allow the rotations concerning the same local axis of the object... instead if I have to make rotation around Y-axis, the other 2 axis must rotate in operation of the first one... like the airplane's movements.

I hope you understand my english... ^^'
When you update the orientation you only need to apply the latest change. So, using YawLeft as an example, try replacing "yaw -= rotationSpeed;" with "yaw = -rotationSpeed;".

Also, you may find this rotation tutorial interesting.
If you really need to be able to rotate correctly on all 3 axes, I suggest you move to quaternions ASAP. I used regular euler angles for a while, until I realised their crippling issues when attempting 6 DOF.

Also, the D3DX library also includes the D3DXMatrixRotationYawPitchRoll() function, which allows you to create a rotation matrix with only 1 function call.

If you want to create an entire world transform matrix, you might want to look into the D3DXMatrixTransformation() function, which allows you to just plug in all the values and it generates your transform matrix for you.
NextWar: The Quest for Earth available now for Windows Phone 7.

This topic is closed to new replies.

Advertisement