## Math problem with Pan, Tilt, Roll

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Fahim, Distinct Sun, Víctor Paredes, erey, Belgarath, slowtiger

Peteroid
Posts: 162
Joined: Thu Aug 12, 2010 3:57 am
Location: las vegas, NV
Contact:

### Math problem with Pan, Tilt, Roll

In order to add a feature to the camera tools, I need to be able to figure out the following:

given a point in 3D space, where is that point after AS pan, tilt, and roll are applied?

It sounds like an easy problem. But there is a snag. The problem stems from the pan tilt and roll numbers that AS uses are RELATIVE to the layer, not ABSOLUTE with respect to 3D space.

This mean it is vastly different from what MATH uses as ''pan', 'tilt' and 'roll'. Math sets these values INDEPENDENT of each other. That is, 'pan' is rotation about the Y-axis, 'tilt' is rotation about the X-axis, and 'roll' is rotation about the Z-axis.

The way AS does it is as follows. Say the layer is set at 'pan' = 45 degrees. Then 'tilt' is no longer rotation about the X-axis, but instead about an axis which is 45 degrees from the X-axis! And if one now wants to apply 'roll', it is no longer about the Z-axis, but about a complex line in 3D space!

Even the LM.Matrix doesn't get it right for this reason. The following code WOULD work if AS used MATH as its basis. That is, this should take the vector (delta_x, delta_y, delta_z ) and transform it to where it would be after being 'panned, 'tilt'ed, and 'roll'ed the given amounts:

local mat = LM.Matrix:new_local()
local vec = LM.Vector3:new_local()

mat:Identity()

mat:Rotate( LM.X_AXIS, tilt )
mat:Rotate( LM.Y_AXIS, pan )
mat:Rotate( LM.Z_AXIS, roll )

vec:Set( delta_x, delta_y, delta_z )

mat:Transform( vec )

delta_x = vec.x ;
delta_y = vec.y ;
delta_z = vec.z ;

This works only if at least two of 'pan', 'tilt' and 'roll' are zero, since then only one axis is ever used.

The formula to do this in AS is obscenely complex. One absurdity for example is that if a layer is panned 90 degrees then the 'roll' and 'tilt' are now mathematically 'tilt' and 'roll' (they swap functionality)!

I guess the best way to attack this is to find a function that maps 'pan', 'tilt and 'roll' from how AS does it to MATH numbers.

I understand why AS did it this way, but it makes scripting a bit hard. I might have an 'outside the box' method to solve this too... UPDATE: well my 'outside the box' method didn't work...

I figured out a way to describe graphically why this is so complex.

Imagine you have three arrows stuck in you... one in your stomach, one through the top of your head, and one in your side.

Now, AS 'pan' is defined rotating on the arrow through your head, 'tilt' is rotating on the arrow in your side, and 'roll' is defined as rotating on the arrow in your stomach.

The problem is, that as you rotate on one of the arrows, the other arrows CHANGE ORIENTATION. Thus, the function that is 'pan' before a 'tilt' is not the same function after the 'tilt', and in fact is a different function for different values of 'tilt'!

In contrast, in MATH, 'pan' is defined as rotating on the Y-Axis, 'tilt' is defined as rotating on the X-axis, and 'roll' is defined as rotating on the Z-axis. The X,Y,and Z-axes do not change orientation EVER! Hence, in MATH, the 'pan', 'tilt' and 'roll' functions are always absolute, constant, simple, and can be applied in any order.

Thus, given the numbers for 'pan', 'tilt' and 'roll' that MOHO supplies, it becomes an extremely difficult and complex math problem to compute the effects of applying more than one of them on even a single point!

The result is that (as it is so far unless I can figure this out) in my Point Camera and POV Camera tools if one sets the optional X,Y, and Z offsets to non-zero then the result will not be correct if the layer has more than one non-zero value for pan, tilt, and roll. It can deal with any one of them, but not two or three.

If anyone has any ideas on how to do this, please speak up! [==Peter==]