Sign in to follow this  
n000b

Scene graph questions

Recommended Posts

n000b    122
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 this post


Link to post
Share on other sites
n000b    122
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 this post


Link to post
Share on other sites
s_ridenour    100
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 this post


Link to post
Share on other sites
n000b    122
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)?).

Share this post


Link to post
Share on other sites
Cecil_PL    122
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 this post


Link to post
Share on other sites
s_ridenour    100
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 this post


Link to post
Share on other sites
f8k8    171
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 this post


Link to post
Share on other sites
Kylotan    9872
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 this post


Link to post
Share on other sites
MaR_dev    154
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.

I recommend this article when dealing with transformations:
http://www.gamasutra.com/features/20020510/johnson_pfv.htm

Share this post


Link to post
Share on other sites
n000b    122
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this