Mesh rotation around local axis (reloaded)

Software: Away3D 4.x

Miha003, Newbie
Posted: 25 October 2012 12:12 PM   Total Posts: 26

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(rotYupVecmatrix.position);
matrix.appendRotation(rotXobject.rightVectormatrix.position);
matrix.appendRotation(rotZobject.forwardVectormatrix.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(currPitchRIGHT_VECTOR,                                      
             
matrix3D.position);
matrix3D.appendRotation(currRollFORWARD_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.CAMERA_POSITION_AXIS_Z Math.cos(shipRotY Math.PI 180)
                    * 
Math.cos(shipRotX Math.PI 180);
var 
cameraPosX:Number _currModelMesh.dummyContainer.position.CAMERA_POSITION_AXIS_Z Math.sin(shipRotY Math.PI 180)
                    * 
Math.cos(shipRotX Math.PI 180);
var 
cameraPosY:Number _currModelMesh.dummyContainer.position.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!

 

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 01:11 PM   Total Posts: 28   [ # 1 ]

Try using a hover camera, and adjust the pan property to be the same as the y rotation of the container, and the tilt property as the x rotation of the mesh.
Set the camera lookAt to the container.

   

Miha003, Newbie
Posted: 25 October 2012 01:36 PM   Total Posts: 26   [ # 2 ]

That doesn’t accomplish what I want. When rotating around X axis, the camera changes position on Y axis but gets more and more distant from the object. I want the distance between camera and mesh to be constant (the camera needs to stay on a sphere around the mesh).

Man….I am so frustrated. In an android native app I did this in like 15 minutes with openGL apis. I don;t know why the Away3d framework makes it so difficult. It should make things easier, not harder. :(

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 01:57 PM   Total Posts: 28   [ # 3 ]

all the tests i have done with the hover controller, keep the camera at the desired distance from the lookAt target, when using panAngle and tiltAngle.
Are you sure you are using the correct controller for the camera?

   

Miha003, Newbie
Posted: 25 October 2012 02:00 PM   Total Posts: 26   [ # 4 ]

Hmmm, could you post the code used to test the hover controller?

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 02:08 PM   Total Posts: 28   [ # 5 ]
var cameraController:HoverController = new HoverController(camera);
 
cameraController.distance 1000;
 
cameraController.minTiltAngle 0;
 
cameraController.maxTiltAngle 180;
 
cameraController.panAngle =0;
 
cameraController.tiltAngle =0;

cameraController.lookAtObject=dummyContainer

in an ENTER_FRAME routine, you can set something like this:

cameraController.panAngle =  dummyContainer.rotationY+180;
cameraController.tiltAngle =mesh.rotationX
   

Miha003, Newbie
Posted: 25 October 2012 02:44 PM   Total Posts: 26   [ # 6 ]

In order to make it work on y axis with your code, I had to set the yFactor to 1. But now the camera position on x axis is not right.

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 04:21 PM   Total Posts: 28   [ # 7 ]

What happens with the camera when you rotate the mesh in the x axis?
Also, do you have the camera inside any container?

   

Miha003, Newbie
Posted: 25 October 2012 05:21 PM   Total Posts: 26   [ # 8 ]

The camera remains behind the rotation, I can see it goes into the right direction, but it stops too early. The controller steps is 1. The more I rotate, the more the difference is more obvious.

The camera is added to a view3d. The view3d also contains my mesh.

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 08:32 PM   Total Posts: 28   [ # 9 ]

From this point on, I guess I can only comment after making a similar test file…

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 08:37 PM   Total Posts: 28   [ # 10 ]

if you put more steps in the controller, the hover movement gets softer. 1 seems too much sudden.

Also, try rotating the x of the mesh changing rotationX instead of pitch.

   

Avatar
Alex-Crafter, Newbie
Posted: 25 October 2012 08:42 PM   Total Posts: 28   [ # 11 ]

yaw, pitch and roll prepend rotations to the object transform matrix. This probably gives unpredicted results when reading from the core rotationX, rotationY, rotationZ values. But you can force a rotation directly to these properties, and even pre-filter them to a limit of 0-360 degress (avoiding euler mishapenings)

   

Miha003, Newbie
Posted: 26 October 2012 11:46 AM   Total Posts: 26   [ # 12 ]

I’ve played a little with the HoverController but still I can’t get the needed behaviour. Let me explain again.

I have an aircraft mesh facing z+ at position (0,0,0). Camera is behind him at
(0,0,-1000).
Pressing and holding keys A/D rolls the aircraft and also changes the yaw with the same value as roll.
Pressing and holding keys W/S changes the pitch.

Example of scenario : press and hold A to rotate to left until aircraft is at max roll 90 degrees,  its yaw is also 90 degrees(pitch is 0 for now), the camera position is now (1000, 0,0). so the aircraft is now facing along global -X axis. This is fine.

The problem is that now I change the pitch(pressing W/S), the aircraft rotates around local X axis (which is now global Y axis). But the camera position moves on global Y axis, and I need it to move on global X and Z axis to describe a circle around my aircraft.


Using HoverController I get this behaviour. Using spheric equations for the camera position I get same behaviour. I’ve tried all I know.

And what is most unbelivebale is that this is a basic thing, and I would have expected that Away3d has a common solution for it. But it doesn’t :(

   

Avatar
Fabrice Closier, Administrator
Posted: 26 October 2012 01:14 PM   Total Posts: 1265   [ # 13 ]

http://en.wikipedia.org/wiki/Gimbal_lock

   

Avatar
alihm, Jr. Member
Posted: 26 October 2012 01:19 PM   Total Posts: 49   [ # 14 ]

For aircraft camera you need to use Quaternions. and use pitch, yaw, roll for locally rotating the aircraft.

   

Miha003, Newbie
Posted: 26 October 2012 01:50 PM   Total Posts: 26   [ # 15 ]

Fabrice, I am not sure if you do this on purpose, but you gave me the same link to wikipedia gimbal lock on my previous post. I already read that and still dont know how to do it. So I really don’t need your link. I already wrote that I spent 2 weeks on this I don’t know what to do. So could you please stop playing smart with me?

Alihm, I know I have to use Quaternions, I don’t know how to use them.
So if you have a precise example post it here. thanks.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X