I need an alternative 3d rotation/translation matrix for a pygame visualization.

87 Views Asked by At

Rotation around arbitrary axis

# Operation 1
c1,s1 = cos(     X),sin(     X)
c2,s2 = cos(sign*Y),sin(sign*Y)
c3,s3 = cos(sign*Z),sin(sign*Z)
s1s2,s1s3,s2s3,s1s2s3 = s1*s2,s1*s3,s2*s3,s1*s2*s3
c1c2,c1c3,c2c3 = c1*c2,c1*c3,c2*c3
m = [
    [c2c3 ,s1s2*c3-c1*s3,c1c3*s2+s1s3 ],
    [c2*s3,s1s2s3+c1c3  ,c1*s2s3-s1*c3],
    [-s2  ,s1*c2        ,c1c2         ]
]
# Operation 2
x1 = x0*m[0][0]+y0*m[0][1]+z0*m[0][2]
y1 = x0*m[1][0]+y0*m[1][1]+z0*m[1][2]
z1 = x0*m[2][0]+y0*m[2][1]+z0*m[2][2]

This works perfectly for standard rotations but object tumbling is non-intuitive.

Observers know up/down/left/right/front/back. I want to rotate an object around observer axes then rotate the rotated object around observer axes.

I would prefer a 4x4 matrix to handle rotation, translation, and perspective in one operation. The code above shows my definition of two operations since all code in each block must be executed from start to finish as a unit to be atomic, consistent, isolated, and durable.

For those curious about my use of sign... I use a bilateral display where rotations around X are the same for both but rotations around Y and Z produce symmetric opposed rotations. This all works very well, except for the tumbling problem.

The math escapes me so far. I will keep trying, but I would love a hint or link.

1

There are 1 best solutions below

4
On BEST ANSWER

It seems that what you are looking for is the integration (in the calculus sense) of a rotation matrix according to some angular velocity $w$. The standard formula for this is:

$$ \frac{dR}{dt} = [w]_\times R$$

where $$[w]_\times = \begin{bmatrix} 0 && -w_z && w_y \\ w_z && 0 && -w_x \\ -w_y && w_x && 0 \end{bmatrix}$$

Then at each time step you would update your rotation matrix with

$$R \leftarrow R + ([w]_\times R)dt$$

Of course with this the rotation matrix will naturally drift from being a true rotation matrix (i.e. columns are length 1 and are mutually orthogonal), so you will occasionally have to correct for this drift. It is for this reason most prefer quaternions for this type of calculation, there is only one constraint.