Skip to main content
gamedev.net
🔒 Locked

object rotations

Started by Helron Feb 1, 2007 at 2:31 PM 3 replies 1.8k views
Original Post
Helron
Helron
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.
webjeff
webjeff
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.
Zook
Zook
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.
Sc4Freak
Sc4Freak
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.

Topic Locked

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

Sign in to reply to this topic.