Mesh rotation around local axis (reloaded)

Software: Away3D 4.x

Avatar
Fabrice Closier, Administrator
Posted: 26 October 2012 02:07 PM   Total Posts: 1265   [ # 16 ]

If you define a q from the current matrix and have a tmp matrix for your rotation, you can define a second q2 from this tmp matrix and multiply them after you have rotated, the resulting q3.matrix will be the matrix that you expect without the gimbal_lock issue.

   

Avatar
alihm, Jr. Member
Posted: 26 October 2012 03:34 PM   Total Posts: 49   [ # 17 ]

Actually it’s very easy. If you want the camera to smoothly follow the aircraft you can do something like this:

var _qCam:Quaternion = new Quaternion();
var 
_qTarget:Quaternion = new Quaternion();

_qTarget.fromMatrix(_yourAircraft.transform);
_qCam.fromMatrix(_yourCamera.transform);

// this is for tweening from current cam q to target q
_qCam.slerp(_qCam_qTarget0.1);

_yourCamera.transform _qCam.toMatrix3D().clone();

//now camera's orientation is smoothly following your
//aircraft's orientation
//you just need to adjust the camera position

_yourCamera.position _yourAircraft.position;
_yourCamera.moveBackward(...);
... 

If you want to rotate around the aircraft you can clone the aircraft’s transform matrix to a dummy object and rotate it as you want then use it as target quaternion, and camera will smoothly slerp to that orientation without any gimbal lock.
Oh, and don’t use X,Y,Z rotation or you will mess up things. Try using pitch,yaw,roll instead.

Hope it helps.

   

Miha003, Newbie
Posted: 27 October 2012 11:00 AM   Total Posts: 26   [ # 18 ]

alihm thank you so much. That si the kind of answer I needed. ‘slerp’ and ‘moveBackward’ do the trick.

Here is my current implementation(In case anyone else has similar problems)
1. To move the aircraft
- save the ini matrix of the ship when the ship is in its initial position:

_iniMatrix Matrix3D mesh.transform.clone(); 

- perform the rotation of the ship by appending needed rotations to the ship’s rotation matrix.

matrix3D _iniMatrix.clone();
matrix3D.appendRotation(-currRollUP_VECTORmatrix3D.position);
mesh.transform matrix3D;
matrix3D.appendRotation(currPitchmesh.rightVector,
       
matrix3D.position);
matrix3D.appendRotation(currRollmesh.forwardVector,
       
matrix3D.position);
mesh.transform matrix3D

The 1st mesh.transform right after the up vector rotation is very important to update the mesh right and forward vector. Otherwise the more you rotate, the more you can see the errors because of working with obsolete vectors.
UP_VECTOR is (0,1,0). rightVector and forwardVector are the ship’s current local X and Z axis.
As you can see, no need to wrap the ship in another container for Y rotation.
2. Move the camera

var _qCam:Quaternion = new Quaternion();
var 
_qTarget:Quaternion = new Quaternion();
_qTarget.fromMatrix(mesh.transform);
_qCam.fromMatrix(view.camera.transform);
_qCam.slerp(_qCam_qTarget0.1);
view.camera.transform _qCam.toMatrix3D().clone();
view.camera.position _currModelMesh.mesh.position;
view.camera.moveBackward(1000);
view.camera.moveUp(300); 

Please reply if this solution is wrong, or it can be improved.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X