Hi,
This is a follow up on this thread:
http://away3d.com/forum/viewthread/3299/
Basically I have an aircraft moving along +z axis. When moving left/right or up/down I can;t just set the rotationX,rotationY or rotationZ properties of the aircraft mesh because the rotationX for example will change the orientation of the local Y and Z axis.
On the above mentioned thread, Sandro said I should use:
public var matrix:Matrix3D;
public var upVec:Vector3D = new Vector3D(0,1,0);
matrix = object.transform.clone();
matrix.appendRotation(rotY, upVec, matrix.position);
matrix.appendRotation(rotX, object.rightVector, matrix.position);
matrix.appendRotation(rotZ, object.forwardVector, matrix.position);
object.transform = matrix;
But this is not working properly because consecutively appendRotation calls lose precision, and after a couple of moving around and rotating, the aircraft rotation is messed up.
Sandro also mentioned I should put the mesh in a container, do the Y rotation on that container, and the X and Z rotation on the actual mesh. That works. Here is how I did it and avoid losing precision:
dummyContainer.rotationY = rotY; matrix3D.appendRotation(currPitch, RIGHT_VECTOR,
matrix3D.position);
matrix3D.appendRotation(currRoll, FORWARD_VECTOR,
matrix3D.position);
mesh.transform = matrix3D;
RIGHT_VECTOR and FORWARD_VECTOR are (1,0,0) and (0,0,1).
matrix3D is the initial matrix when the aircraft moves along +z.
But now I have problems with the camera. My camera is right behind the aircraft. It should rotate on a sphere around the aircraft, so that you would always see the aircraft from behind.
I have used all types of controllers, none give me the behaviour I want.
In the end I have come up with the following equations to move my camera:
var shipRotX:Number = currPitch;
var shipRotY:Number = dummyContainer.rotationY;
var shipRotZ:Number = currRoll;
var cameraPosZ:Number = _currModelMesh.dummyContainer.position.z - CAMERA_POSITION_AXIS_Z * Math.cos(shipRotY * Math.PI / 180)
* Math.cos(shipRotX * Math.PI / 180);
var cameraPosX:Number = _currModelMesh.dummyContainer.position.x - CAMERA_POSITION_AXIS_Z * Math.sin(shipRotY * Math.PI / 180)
* Math.cos(shipRotX * Math.PI / 180);
var cameraPosY:Number = _currModelMesh.dummyContainer.position.y + CAMERA_POSITION_AXIS_Z * Math.sin(shipRotX * Math.PI / 180);
When the aircraft rotates only around Z and Y, or only around X, the camera position is perfect. When I rotate on all 3 axis, the camera position is wrong.
Somehow, I think I need to translate the local rotation of the mesh to global rotation, but I don’t know. And that dummy container in between even confuses me more.
Pleaseeeeeeeee, could someone help me? I am really stuck for 2 weeks now on this one. It doesnt have to go like this, any other implementation would do. But please don;t send me to some wikipedia article of gimbal lock, or tell me to use quaternions…just please give me a sample or something.
Thank you very much!