# Scene graph questions

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 :)

##### Share on other sites
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.

##### Share on other sites
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 :)

##### Share on other sites
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.

##### Share on other sites
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? :)

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Like f8k8 wrote - its row major vs column major issue. BUT: logical operation must be ALWAYS the same(ie first rotation and then translation). The only change is order of multiplications when you create your final transformation matrix.

http://www.gamasutra.com/features/20020510/johnson_pfv.htm

##### Share on other sites
Thanks guys! I finally got it to work - I was accidently translating on the X axis and also trying to rotate on the X axis when I should have been rotating on the Y axis!

One more question - is it possible to get the current translation/rotation/scale amounts that are already in place? Because as I traverse through my scene graph I need to keep applying more transformations but these overwrite the previous ones, and I need them to add on top of the old ones instead. So I figure if I can get the current transformation values, I can just add them on.

##### Share on other sites

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628698
• Total Posts
2984275

• 20
• 10
• 13
• 13
• 11