Hi.
I'm trying to move and rotate groups of objects. I'm getting the objects' positions and angles relative to a single origin marker, and moving them to a destination marker. I've had success positioning the objects correctly with Chesko's http://www.creationkit.com/Spatial_functions_and_snippets.
Angles are a problem, though. I'm trying get an angle equal to: (Object angle - Origin angle) + Destination angle. This simple equation only works on the Z-axis, though (the initial object must have angleX=0 and angleY=0). I understand that this is because Skyrim uses Euler angles, which can't be simply added together.
(My background isn't in programming or math, so I don't really understand what's happening here. But I've been giving it a shot.)
I've been able to convert an object's angles to a z-matrix (essentially what http://www.creationkit.com/Setting_Local_Rotation does), and then inverse the matrix to get the original angles back. But this doesn't allow for addition either.
Following the advice from the aptly named http://www.sjbaker.org/steve/omniv/eulers_are_evil.html, I've created a destination matrix and an offset matrix (just z-matrices, though), post-multiplied destination x offset, inversed the result, and extracted X/Y/Z angles from that. This doesn't work.
My next thought is to do the above but with each of the X, Y and Z matrices (perhaps in Z-Y-X order).
But before I spend more hours on this, I'd like to get advice from the experienced modders here. Any help is greatly appreciated!
Edit: Here's my code for my last attempt. I apologize it's not well-organized or commented -- I haven't bothered to clean it up because it isn't working.
Matrices are in the format:
| A1 B1 C1 |
| A2 B2 C2 |
| A3 B3 C3 |
And so forth...
Scriptname _ePedestal02 extends _ePedestalTriggerImport Mathfloat[] Function GetNewAngle(Float[] OriginPos, Float[] DestinationPos, Float AngleX, Float AngleY, Float AngleZ) float[] DestAngle = new Float[3] DestAngle[_X] = DestinationPos[ANGLE_X] DestAngle[_Y] = DestinationPos[ANGLE_Y] DestAngle[_Z] = DestinationPos[ANGLE_Z] float[] OffsetAngle = new Float[3] OffsetAngle[_X] = AngleX - OriginPos[ANGLE_X] OffsetAngle[_Y] = AngleY - OriginPos[ANGLE_Y] OffsetAngle[_Z] = AngleZ - OriginPos[ANGLE_Z] PopulateMatrices(DestAngle, OffsetAngle) PostMultiplyMatrices() float[] NewAngle = InverseAndGetAngles() return NewAngleEndFunctionfloat[] mDest ; destination matrixint A1 = 0int A2 = 1int A3 = 2int B1 = 3int B2 = 4int B3 = 5int C1 = 6int C2 = 7int C3 = 8float[] mOff ; offset matrixint D1 = 0int D2 = 1int D3 = 2int E1 = 3int E2 = 4int E3 = 5int F1 = 6int F2 = 7int F3 = 8float[] mNew ; new matrixint G1 = 0int G2 = 1int G3 = 2int H1 = 3int H2 = 4int H3 = 5int I1 = 6int I2 = 7int I3 = 8Function PopulateMatrices(float[] DestAngle, float[] OffsetAngle) float fX = DestAngle[_X] float fY = DestAngle[_Y] float fZ = DestAngle[_Z] mDest = new float[9] mDest[A1] = fX * cos(fZ) mDest[A2] = fY * sin(fZ) mDest[A3] = fZ * 0 mDest[B1] = fX * (-sin(fZ)) mDest[B2] = fY * cos(fZ) mDest[B3] = fZ * 0 mDest[C1] = fX * 0 mDest[C2] = fY * 0 mDest[C3] = fZ * 1 fX = OffsetAngle[_X] fY = OffsetAngle[_Y] fZ = OffsetAngle[_Z] mOff = new float[9] mOff[D1] = fX * cos(fZ) mOff[D2] = fY * sin(fZ) mOff[D3] = fZ * 0 mOff[E1] = fX * (-sin(fZ)) mOff[E2] = fY * cos(fZ) mOff[E3] = fZ * 0 mOff[F1] = fX * 0 mOff[F2] = fY * 0 mOff[F3] = fZ * 1EndFunctionFunction PostMultiplyMatrices() ; multiplying Destination X Offset matrices mNew = new float[9] mNew[G1] = mDest[A1] * mOff[D1] + mDest[B1] * mOff[D2] + mDest[C1] * mOff[D3] mNew[G2] = mDest[A2] * mOff[D1] + mDest[B2] * mOff[D2] + mDest[C2] * mOff[D3] mNew[G3] = mDest[A3] * mOff[D1] + mDest[B3] * mOff[D2] + mDest[C3] * mOff[D3] mNew[H1] = mDest[A1] * mOff[E1] + mDest[B1] * mOff[E2] + mDest[C1] * mOff[E3] mNew[H2] = mDest[A2] * mOff[E1] + mDest[B2] * mOff[E2] + mDest[C2] * mOff[E3] mNew[H3] = mDest[A3] * mOff[E1] + mDest[B3] * mOff[E2] + mDest[C3] * mOff[E3] mNew[I1] = mDest[A1] * mOff[F1] + mDest[B1] * mOff[F2] + mDest[C1] * mOff[F3] mNew[I2] = mDest[A2] * mOff[F1] + mDest[B2] * mOff[F2] + mDest[C2] * mOff[F3] mNew[I3] = mDest[A3] * mOff[F1] + mDest[B3] * mOff[F2] + mDest[C3] * mOff[F3]EndFunctionfloat[] Function InverseAndGetAngles() float[] NewAngle = new float[3] NewAngle[_X] = mNew[G1] + mNew[H1] + mNew[I1] NewAngle[_Y] = mNew[G2] + mNew[H2] + mNew[I2] NewAngle[_Z] = mNew[G3] + mNew[H3] + mNew[I3] return NewAngleEndFunction