I actually did a project a lot like this for a graphics class I’m taking. Unfortunately, for this class we are using something called the VTK rather than OpenGL. Anyway… I’ll describe how I setup my code.
I had a C++ class for each body in the scene. In this class I stored a number of matrices. These matrices were basically these:
CurAxisRot - a matrix for the current axis rotation
AxisRotInc - the incremental axis rotation
CurOrbitRot - matrix for the current orbit rotation
OrbitRotInc - the incremental orbit rotation
OrbitRadiusMatrix - a translation matrix for the orbit radius from the parent body.
Final - a final matrix
FinalPos - final positional matrix that does not include the planet’s axis rotation.
The class also had a pointer to the parent object, and a list of children objects. There was a function that got the parents FinalPos matrix.
Each frame the axis and orbit rotational matrices were incremented by the incremental matrices.
When rendering, I started with the root parent object (the sun) and went through all the children, and all the children’s children.
To calculate the final matrices, I basically did something like so…
FinalPos = GetParentPos() * CurOrbitRot * OrbitRadiusMatrix;
Final = FinalPos * CurAxisRot;
Since the matrices are actually affected in the reverse order that they are specified, you can see that in effect the order of transformations would be something like so…
- Rotate the body around it’s axis
- Translate the body the length of it’s radius from the parent body.
- Rotate the body to it’s correct location around it’s parent
- Do whatever translations are necessary from the parent body. (Essentially none if the parent is the sun)
I could probably send you my code if that would make things more clear. I should almost translate the whole program to OpenGL just for the fun of it, but I haven’t really had the time.
[This message has been edited by Deiussum (edited 12-01-2000).]