Chris
Chris

Reputation: 49

Moving in different directions around a point (planet)

I'm working on a game that involves moving around a planet and have currently got the player moving in one direction using forward and back using:

if( forward)
  yAngle = yAngle + 0.005f;
if( backward)
  yAngle = yAngle - 0.005f;
if(left)
  zAngle = zAngle - 0.005f;
if(right)
  zAngle = zAngle + 0.005f;

Then these angles are put into the matrix.

The character rotates perfectly using the Z Rotation and moves forward and backwards orbiting the planet using the Y rotation but will only move in that direction.

How would I calculate the X and Y rotations so the character will move around the planet in the right direction?

Upvotes: 0

Views: 107

Answers (2)

John Bollinger
John Bollinger

Reputation: 180998

The rotation matrix for a rotation around the x-axis of a 3D Cartesian coordinate system is

     1            0            0
     0        cos(xAngle)  sin(xAngle)
     0       -sin(xAngle)  cos(xAngle)

The matrix for a rotation around the y-axis is

 cos(yAngle)      0       -sin(yAngle)
     0            1            0
 sin(yAngle)      0        cos(yAngle)

The matrix for a rotation around the z-axis is

 cos(zAngle)  sin(zAngle)      0
-sin(zAngle)  cos(zAngle)      0
     0            0            1

You can reverse the sense of rotation of any of those matrices by reversing the signs of its sine terms.

There is no unique definition or matrix for a combined rotation about two axes, however, because the result depends on the order in which the rotations are performed. Of particular relevance to your case, it is still different if the rotations are performed in a series of small, alternating steps.

Therefore, rather than tracking overall rotation angles, I suggest keeping a transformation matrix describing the current orientation (so that, for example, the current direction to the ship is M * (1, 0, 0)). Update that on each tick, or otherwise as needed, by multiplying it by incremental rotation matrices of the above forms describing the movement since the last update. This should give you smooth movement consistent with intuitive controls.

Upvotes: 1

jamador
jamador

Reputation: 139

I suggest trying something like this:

if (forward) {
  xAngle += 0.005f * (cos(zAngle));
  yAngle += 0.005f * (sin(zAngle));
}
if (backward) {
  xAngle -= 0.005f * (cos(zAngle));
  yAngle -= 0.005f * (sin(zAngle));
}

I'm going on a hunch here, but try it out!

Upvotes: 1

Related Questions