Peter Quiring
Peter Quiring

Reputation: 1706

matrix rotation multiple times

I'm having a problem understanding matrices. If I rotate my matrix 90 deg about X axis it works fine, but then, if I rotate it 90 deg about Y axis it actually rotates it on the Z axis. I guess after each rotation the axes move. How do I rotate a second time (or more) using the original axes? Is this called local and global rotation?

Upvotes: 1

Views: 2218

Answers (4)

Peter Quiring
Peter Quiring

Reputation: 1706

Thinking about it last night I found the answer (I always seem to do this...)

I have an object called GLMatrix that holds the matrix:

  class GLMatrix {
    public float m[] = new float[16];
    ...includes many methods to deal with matrix...
  }

And it has a function to add rotation:

  public void addRotate2(float angle, float ax, float ay, float az) {
    GLMatrix tmp = new GLMatrix();
    tmp.setAA(angle, ax, ay, az);
    mult4x4(tmp);
  }

As you can see I use Axis Angles (AA) which is applied to a temp matrix using setAA() and then multiplied to the current matrix. Last night I thought what if I rotate the input vector of the AA by the current matrix and then create the temp matrix and multiple.

So it would look like this:

  public void addRotate4(float angle, float ax, float ay, float az) {
    GLMatrix tmp = new GLMatrix();
    GLVector3 vec = new GLVector3();
    vec.v[0] = ax;
    vec.v[1] = ay;
    vec.v[2] = az;
    mult(vec);  //multiple vector by current matrix
    tmp.setAA(angle, vec.v[0], vec.v[1], vec.v[2]);
    mult4x4(tmp);
  }

And it works as expected! The addRotate4() function now rotates on the original axis'es.

Upvotes: 0

Hakes
Hakes

Reputation: 621

In practice best way to overcome this problem is to use quaternions, it is quite a bit math. You are right about; if you rotate it around Y 90 degrees than if you want to rotate it around Z you will be rotating around X.

Here is a nice source to convert euler angles to quaternions: http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/

And here is how to make a rotation matrix out of a quaternion: http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/

After you have filled the matrix, you can multiply by calling glMultMatrix( qMatrix);.

Upvotes: 0

datenwolf
datenwolf

Reputation: 162164

You don't "rotate" matrices. You apply rotation transformation matrices by multiplication. And yes, each time you call a OpenGL matrix manipulation function the outcome will be used as input for the next transformation multiplication.

A rotation by 90° about axis X will map the Y axis to Z and the Z axis to -Y, which is what you observe. So what ever transformation comes next start off with this.

Either build the whole transformation for each object anew using glLoadIdentity to reset to an identity, or use glPushMatrix / glPopMatrix to create a hierachy of "transformation blocks". Or better yet, abandon the OpenGL built-in matrix stack altogether and replace it with a proper matrix math library like GLM, Eigen or similar.

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 2822

Add 'glLoadIdentity' between the rotations.

Upvotes: 0

Related Questions