Scene graph questions

Started by
10 comments, last by n000b 16 years, 4 months ago
Hi, I've just been reading through a tutorial/introduction on scene graphs (http://www.gamedev.net/reference/programming/features/scenegraph/) and I have a few questions. If we consider his first example, shown in the first image, I can't work out an easy way to rotate the moons around the planets. Am I correct in thinking that to apply the rotations shown (first to the planets, then to the moons), I should be translating the body and then applying a rotation to it? This will make the child object "orbit" the parent. However, when we get to the moons, I will have translated it twice already. Assuming the star is at (0, 0, 0), wont my moons then rotate around the star rather than around it's parent (the planet)? My understanding of transformations is that objects rotate around the origin. Is this correct? If so, how do I make the moons rotate around the planets instead of the star? Thanks :)
Advertisement
Yes transforms work around the objects origin.

I could be wrong, but I'm pretty sure you want to apply the rotation matrix, then translate to your position.
==============================
A Developers Blog | Dark Rock Studios - My Site
But translating then rotating allows an object to "orbit" around the origin - this is exactly what I need for the planets (assuming the star is at the origin). Obviously the moons wont orbit the planet correctly like this though.

I don't suppose there is any easy way to change the point that an object considers it's origin? Or if someone could suggest the easiest way to accomplish my problem that would be great too (DirectX9).

Thanks :)
Nope, other way around.

If you translate then rotate an object, the object will be moved to whatever point you translated to and then rotated around the object's origin (rotated around its own center point).

To make an object orbit something else, rotate then translate.

Unless things are very different in DirectX.

Some OpenGL-ish pseudocode would be:
loadIdentityMatrix();pushMatrix();drawSun();rotate(0.0, mercury_rotation, 0.0);translate(0.0, 0.0, mercury_distance);drawMercury();popMatrix();rotate(0.0, venus_rotation, 0.0);translate(0.0, 0.0, venus_distance);drawVenus();popMatrix();

etc.
Thanks - I *think* I've got it the right way around though.

If you go to http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B5.aspx and scroll down to figure 5.9 (a little over half way down the page) it shows that if you translate then rotate an object, it is rotated around its original point (is this point the point before it was translated, or (0, 0, 0)?).
Anyone? :)
Assuming you're correct in that rotation is done around the origin, not the object's center...

The way to get the Moon to orbit the Earth, not the Sun, is to:

1) Translate the Moon by its distance from the Earth.
2) Rotate the Moon (orbits by some angle).
3) Translate the Earth-Moon system by its distance from the Sun.
4) Rotate the Earth-Moon system, which orbits it around the Sun.

Having looked at the tutorial n000b linked to, it appears that DirectX and OpenGL do things differently.

In OpenGL, to make an object appear at (123, 456) facing 90 degrees to the side you'd translate to (123, 456) first, then rotate 90 degrees.

In DirectX, however, you'd do the opposite, rotating 90 degrees then translating to (123, 456).

That's for "normal" behavior, though. To make Object B orbit Object A, you'd do:

OpenGL:
Rotate by the number of degrees B should orbit A, then translate the distance B is from A.

DirectX:
Translate the distance B is from A, then rotate the number of degrees you want B to orbit around A.

IMHO the OpenGL method makes more sense, but that's just me.
It's different because OGL uses a column vectors and multiplies matricies in one order, and DX uses row vectors and multiplies the other way round.
Clicky
It has nothing to do with the differences between DirectX or OpenGL. These two approaches are both 2 different ways of achieving the same thing. Generally, to implement orbiting, you rotate the moon around the moon's origin and then translate the moon relative to the planet. But there's no reason why you couldn't translate the moon to the final position and then rotate. The reason why you wouldn't is because it's more complicated that way, as you'd need to use some trigonometry to work out where to translate to. If you rotated first and then translated, OpenGL or DirectX will do that trigonometry for you.

This topic is closed to new replies.

Advertisement