问题
I'm trying to create a very basic planet movement (sun, earth moon represented as cubes).
As for now i managed to move one cube around another, but I have the following question... What will be the best way to move another cube (ex. the moon) around an already moving one?
I put them in motion using simple operations ex. for movement on x axis:
d * Math.sin(x * Math.PI / 180.0)
Where d is the distance from center or projection (dont know if i used this term right). I thought about putting the "satellite" by calulating the distance of the already rotating cube and adding some parameter, but don't know how to make that planet like a "reference" for center of rotation...
回答1:
You do this by creating a composition of transformations. Say you want to orbit a planet around a central star. First the planet is moved into orbit, a translation, in say the x direction:
orbit_distance = translation_orbit · planet_center
followed by a rotation around the center.
orbit_position = rotation_orbit · orbit_distance
Or, you can write this in one line
orbit_position = rotation_orbit · translation_orbit · planet_center
It is important, that the order of operations matter, rotating first and then translating is not the same as first translating and then rotating. Or, in mathematical terms, geometrical transformations are not commutative.
In a 3 dimensional space all linear transformations can be described by linear equations in a 4 dimensional homogenous set of coordinates. I.e. you can use 4×4 matrices to express all kinds of linear geometrical transformations.
A rotatng moon orbiting a planet orbiting a sun:
R_orbit_planet · T_orbit_planet · R_orbit_moon · T_orbit_moon · R_rotation_moon
来源:https://stackoverflow.com/questions/13444111/opengl-rotations-and-object-movement