PDA

View Full Version : World Rotation to local rotation

loriswave
06-08-2009, 12:49 AM
hi,
anyone know what formula should I use to find the local rotations to be applied to an item knowing that the rotations have to get global.

is for lscript like rotation constrain with world option.

thanks for all

06-10-2009, 12:16 PM
getforward, right, up for world and multiply.
I don't have the equations on hand right now, but if you grab the LW sdk and look in the docs, the C code is in there.

loriswave
06-12-2009, 01:18 AM
Hi,
I have try with this:

have a drive item, find parent of ma ( were a script is applied )
calculate matrix for Driveitem and Parent

calculate invers of Matrix of Driveitem and multiply with Parent Matrix
from matrix result calculate H,P,B. but not have a right result, somebody know were i wrong. I try to reading a SDK but I'm not a programmer and not understand more.

//var1 is drive rotation
DriveRot = var1.getWorldRotation(time);

//search parent of item
ma2id = ma.objID;
Pma = ma2id.parent;
Prot = Pma.getWorldRotation(time);

//Mrot = Mfinal * invers( Morig );

//1 Mfinal
Exax = var1.getWorldRight(time); // | A D G |
Eyax = var1.getWorldUp(time); // | B E H |
Ezax = var1.getWorldForward(time); // | C F I |
//2 Morig
Pxax = Pma.getWorldRight(time); //left | L O R |
Pyax = Pma.getWorldUp(time); //middle | M P S |
Pzax = Pma.getWorldForward(time); //right | N Q T |

A = Exax.x; D = Exax.y; G = Exax.z;
B = Eyax.x; E = Eyax.y; H = Eyax.z;
C = Ezax.x; F = Ezax.y; I = Ezax.z;

L = Pxax.x; O = Pxax.y; R = Pxax.z;
M = Pyax.x; P = Pyax.y; S = Pyax.z;
N = Pzax.x; Q = Pzax.y; T = Pzax.z;
// calculate invers of matrix
det = L * (P*T - S*Q) - M * (O*T - R*Q) + N * (O*S - R*P);

m0 = P*T-Q*S / det;
m1 = -(M*T-S*N)/ det;
m2 =M*Q-P*N / det;
m3 =-(O*T-Q*R)/ det;
m4 =L*T-R*N / det;
m5 =-(L*Q-O*N)/ det;
m6 =O*S-R*P / det;
m7 =-(L*S-R*M)/ det;
m8 =L*P-M*O / det;

/* R = M x L

| A B C | | M0 M1 M2 |
= | D E F | x | M3 M4 M5 |
| G H I | | M6 M7 M8 |
*/
//Multiply Matrix

mat0 = A*m0+B*m3+C*m6;
mat1= A*m1+B*m4+C*m7;
mat2= A*m2+B*m5+C*m7;
mat3= 0;
mat4 = D*m0+E*m3+F*m6;
mat5= D*m1+E*m4+F*m7;
mat6= D*m2+E*m5+F*m7;
mat7= 0;
mat8 = G*m0+F*m3+I*m6;
mat9= G*m1+H*m4+I*m7;
mat10=G*m2+H*m5+I*m7;
mat11=0;
mat12=0;
mat13=0;
mat14=0;
mat15=1;

//Calculate Y-axis angle
angle_y = asin( mat2);
C = cos( angle_y );
angle_y = deg(angle_y);

//Gimball lock?
if ( C > 0.005 )
{
//No, so get X-axis angle
trx = mat10 / C;
try = -mat6 / C;

//angle_x = atan2( try, trx ) * RADIANS;
angle_x = deg(acos(try / sqrt( pow(trx,2) + pow(try,2) ) ) );
//angle_x = acos( try / (sqrt( pow(trx,2) + pow(try,2)));

trx = mat0 / C; // Get Z-axis angle
try = -mat1 / C;
angle_z = acos(try / sqrt( pow(trx,2) + pow(try,2) ) );
}
// Gimball lock has occurred
else
{
// Set X-axis angle to zero
angle_x = 0;
// And calculate Z-axis angle
trx = mat5;
try = mat4;
angle_z = deg( acos(try / sqrt( pow(trx,2) + pow(try,2) ) ) );
}

// return only positive angles in [0,360]
if (angle_x < 0) angle_x += 360;
if (angle_y < 0) angle_y += 360;
if (angle_z < 0) angle_z += 360;

Eroth = angle_x ;
Erotp = angle_y ;
Erotb = angle_z ;

06-18-2009, 07:27 PM
not sure what you're going for, but the world to local coords (taken right out of LW sdk) is just:

for ( i = 0; i < 3; i++ )
p[ i ] = ( q[ 0 ] - wpos[ 0 ] ) * worldrt[ i ]
+ ( q[ 1 ] - wpos[ 1 ] ) * worldup[ i ]
+ ( q[ 2 ] - wpos[ 2 ] ) * worldfd[ i ]
+ piv[ i ];

i goes from 0 to 2 for the vectors (3 total items).
q is the position (q-wpos = removes offset from the origin).

converting from matrix to HPB is more work, since you need to know the correct rotation order (ZYX I believe??).

loriswave
06-19-2009, 01:30 AM
I think that I have wrong question, I dont need a assign a different world Position, i need assign a different rotation based on a world rotation of other item, the problem is a rotation not a position.

06-19-2009, 12:25 PM
You're right, I gave you the wrong info. That equation above is for converting a point in world space to a point in local space. (what was I thinking?) :(

So, to get a local rotation of a world rotation, couldn't we just:
multiply the worldrotationmatrix * localrotationmatrix and pull out the HPB from that final matrix?
The catch would be the multiply would HAVE to be in that order (wmatrix * lmatrix).

Or am I missing something?
If I am missing something, then the best code example I found was here, but they include translation, which we dont need:
http://stackoverflow.com/questions/695043/how-does-one-convert-world-coordinates-to-camera-coordinates

matrix to HPB:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/
3d matrix math:
http://www.gamedev.net/reference/articles/article695.asp

I've dealt with this problem before, but my notes are at home.

loriswave
06-26-2009, 03:51 AM
I am sorry but I still do not understand, let's go for small steps,
I have two elements, one who guides the movement (var1) and one who is guided (ma)
I find the rotation world of "var1"
I find the parent of "ma" I call PMA
I find the rotation world of PMA

After that?

There is a strange thing, the commands
GetRight, getUp and getForward
build the matrix of rotation of the selected by applying to them the function matrix to hpb to find the angles would have resulted in the rotation for the selected item. But this does not happen, I have the wrong formula or wrong I don't understand what the commands are GetRight, getUp and getForward do?

thanks