# Modifying X Rotation According to Mouse

This topic is 2681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there everyone, I'm quite new to linear algebra with regards to 3D games, as my prior work was all in 2D and need some pointers and advice. Side by side to reading up on some math textbooks, I've begun programming a small 3rd person space fighter game. At the moment I've begun using the C4 Engine and followed a tutorial. Everything is well and good, however I now need my ship to rotate with the camera. Here is some sample code to better illustrate my issue. I notice the CharacterOrientation function was used which takes an azimuth and rotates the ship on the Z axis. I was wondering how I could achieve a similar effect with the X rotation, and what the theory behind it is.

 float azm = modelAzimuth + TheInputMgr->GetMouseDeltaX(); if (azm < -K::pi) azm += K::two_pi; else if (azm > K::pi) azm -= K::two_pi; float alt = modelAltitude + TheInputMgr->GetMouseDeltaY(); if (alt < -1.45F) alt = -1.45F; else if (alt > 1.45F) alt = 1.45F; modelAzimuth = azm; modelAltitude = alt; Vector2D propel(0.0F, 0.0F); long index = movementIndexTable[movementFlags & kMovementPlanarMask]; if (index < 8) { static const float movementDirectionTable[8] = { 0.0F, 4.0F, 2.0F, -2.0F, 1.0F, -1.0F, 3.0F, -3.0F }; float direction = movementDirectionTable[index] * K::pi_over_4 + modelAzimuth; propel.x = Cos(direction) * 100.0F; propel.y = Sin(direction) * 100.0F; SetExternalForce(propel); SetExternalDampening(Vector2D(10.0F, 10.0F)); // Change the Player's orientation based on horizontal mouse movement. SetCharacterOrientation(modelAzimuth);

Any help would be much appreciated.

Cheers!

##### Share on other sites
Just a little update. I've made some progress after having the time to learn a little bit more linear algebra. Instead of using the set CharacterOrientation function, I am now doing this to try and rotate the character based on the mouse Y position. It works perfectly and follows the Y delta of the mouse when I comment out the SetRotationAboutZ function. However, when I uncomment this line, it doesn't follow as accurately if that makes any sense? Any help as to what's causing it to do this would be much appreciated!

 Matrix3D orientation = GetFinalNodeTransform().GetMatrix3D(); orientation.SetRotationAboutY(-modelAltitude); //orientation.SetRotationAboutZ(modelAzimuth); SetRigidBodyOrientation(orientation); 

##### Share on other sites
This is going to depend how you want your camera control. Third person camera will work differently to first person camera as the camera has to pan around the player/character as opposed to simply rotating.

 Matrix3D orientation = GetFinalNodeTransform().GetMatrix3D(); orientation.SetRotationAboutY(-modelAltitude); //orientation.SetRotationAboutZ(modelAzimuth); SetRigidBodyOrientation(orientation); 

Firstly, according to C4 documentation:
The SetRotationAboutY function replaces all entries of a matrix with those representing a rotation about the y axis through the angle given by the angle parameter.[/quote]
So the function doesn't work the way you are using it.

Also, you are taking the wrong approach. You should be rebuilding it using the altitude/azimuth values. The way you do this is by multiplying them together.

Corrected version of the above code:
Matrix3D yaw, pitch; yaw.SetRotationAboutY(-modelAltitude); pitch.SetRotationAboutZ(modelAzimuth); Matrix3D orientation = pitch * yaw; SetRigidBodyOrientation(orientation);

Another important point is to remember that matrix multiplication order matters. pitch * yaw does not do the same thing yaw * pitch. If you're adding roll in there too (rotation around X), which is common for flight sims, the multiplication order should be roll * pitch * yaw. A different order will give you different and probably undesirable results.

##### Share on other sites

This is going to depend how you want your camera control. Third person camera will work differently to first person camera as the camera has to pan around the player/character as opposed to simply rotating.

[quote name='UraniumSlug' timestamp='1302971847' post='4799166']
 Matrix3D orientation = GetFinalNodeTransform().GetMatrix3D(); orientation.SetRotationAboutY(-modelAltitude); //orientation.SetRotationAboutZ(modelAzimuth); SetRigidBodyOrientation(orientation); 

Firstly, according to C4 documentation:
The SetRotationAboutY function replaces all entries of a matrix with those representing a rotation about the y axis through the angle given by the angle parameter.[/quote]
So the function doesn't work the way you are using it.

Also, you are taking the wrong approach. You should be rebuilding it using the altitude/azimuth values. The way you do this is by multiplying them together.

Corrected version of the above code:
Matrix3D yaw, pitch; yaw.SetRotationAboutY(-modelAltitude); pitch.SetRotationAboutZ(modelAzimuth); Matrix3D orientation = pitch * yaw; SetRigidBodyOrientation(orientation);

Another important point is to remember that matrix multiplication order matters. pitch * yaw does not do the same thing yaw * pitch. If you're adding roll in there too (rotation around X), which is common for flight sims, the multiplication order should be roll * pitch * yaw. A different order will give you different and probably undesirable results.

[/quote]

Thank you very much, that opened my eyes a lot. Due to the fact that I'm learning from various game math books, it helps to have some practical explanations of the implementations which are more eluding than others, which you have now provided me with. Thanks again!

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632163
• Total Posts
3004514

×