Author Topic: Rotate an object to match another object's rotation matrix  (Read 3502 times)

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Rotate an object to match another object's rotation matrix
« on: October 20, 2011, 09:43:32 am »
I have two Matrices;

1) object3D1: The source Object3D rotational matrix (getRotationMatrix())
2) object3D2: The destination Object3D rotational matrix (getRotationMatrix())

I would like to slowly rotate the source object to match the rotation matrix of the destination object frame by frame, so both rotations are exactly the same (both objects are facing the same way).

I'm guessing I can use object3D1.getZAxis() and object3D2.rotateAxis() but how do you do the calculations?

Or can I use Matrix.interpolate()?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate an object to match another object's rotation matrix
« Reply #1 on: October 20, 2011, 10:16:33 am »
You can use Matrix.interpolate() as long as the matrices aren't too different or the rotations happen in the same plane. If you try to interpolate between a matrix that makes the object facing down and one that makes it facing up, the results will look funny. For that case, you might want to look into some quaternion code. Bones has classes for this for example.

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Rotate an object to match another object's rotation matrix
« Reply #2 on: October 20, 2011, 10:37:41 am »
I think interpolate() is no good because I need to vary the xyz movement, and quaternion seems a bit overkill but I'll have a look anyway.

Basically I'm trying to automate the path of a plane so it rotates using preset rudder and yaw incremental values. The plane needs to slowly rotate into the destination matrix, using scaleMul to move the plane.


Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Rotate an object to match another object's rotation matrix
« Reply #3 on: October 20, 2011, 12:40:38 pm »
Ok I've decided to try quaternions. It's all ready to go but I'm having difficulty seeing how I can use it to determine how much the object needs to rotate to match the other object.

// I'm assuming what I need to do is put the matrix rotation into each quaternion
q1.fromMatrix(object3D.getRotationMatrix());
q2.fromMatrix(object3D.getRotationMatrix());
fRotation = q1.calcSub(q2);

Then call Object3D1.rotateAxis(getYAxis, fRotation).

I'll give that a try ..

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate an object to match another object's rotation matrix
« Reply #4 on: October 20, 2011, 01:03:00 pm »
No idea...in case of problems, i suggest to post this in the Bones forum instead (assuming that you are using Bone's quaternion classes).

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Rotate an object to match another object's rotation matrix
« Reply #5 on: October 20, 2011, 01:32:22 pm »
I got the auto direction/rotation working perfectly simply by testing the xy values against both quats.

For example:
      if(q2.x < q1.x) fYIncrement += 0.001f;
      else           fYIncrement -= 0.001f;
      object3D.rotateAxis(object3D.getXAxis(), fYIncrement);

Thanks Egon.