Rotation question (orbiting entities in a 2D game)

Started by
3 comments, last by Dookie 17 years, 8 months ago
Hey guys, here's a slightly more challenging question out of the usual silly easy questions I usually ask. [smile] In my 2D game, I have a guy who has things orbiting his body...

    0  0
  0      0
 0   ME   0
  0      0
    0  0
Currently, the objects rotate around his body like you see in the illustration. The objects rotate the body on the 'z' axis, which simply makes the objects rotate in a clockwise fashion. I'm using a variation of this formula to make the orbit effect happen:
Xnew = Xold*cos(theta) - Yold*sin(theta)
Ynew = Xold*sin(theta) + Yold*cos(theta)
And it looks pretty cool! However, I've enabled z-buffering in my 2D game to let me easily move 2D objects in front or behind other game objects. So what I'd like to do is modify my orbiting code to allow orbit rotation on multiple axis... Imagine the objects orbiting the guy in a clockwise rotation like they currently do, but also the entire rotating 'mass' slowly rotating around the guy along the Y and X axes. I can almost do that by calling the above formula multiple times, for each axis:
Xnew = Xold*cos(theta) - Yold*sin(theta)
Ynew = Xold*sin(theta) + Yold*cos(theta)

Xnew = Xold*cos(theta) - Zold*sin(theta)
Znew = Xold*sin(theta) + Zold*cos(theta)

Ynew = Yold*cos(theta) - Zold*sin(theta)
Znew = Yold*sin(theta) + Zold*cos(theta)
The folly with the above method is that the orbiting mass starts rotating clockwise, but as the other axes are rotated the orbiting mass slows and sometimes starts to rotate in the opposite direction. It's a cool effect too, I guess, but not the effect I want. Would any of you kind guys and gals help this poor programmer to consolidate the above method into one formula, and help me to get the entities to orbit at a constant speed as the other axes are modified? Thanks in advance for the help! [smile]
"The crows seemed to be calling his name, thought Caw"
Advertisement
The simplest way to orbit around a point (x0, y0) with constant radius r is:

x = x0 + r.cos(theta)
y = y0 + r.sin(theta)

If you rely on the previous position to calculate your new one (as you are), you'll find that over time, floating point error distorts the orbit quite severely.

Perhaps you'd prefer to use three constant decoupled rotations:

x = x0 + r.(cos(theta) * sin(phi) * 1);y = y0 + r.(sin(theta) * 1        * cos(psi));z = z0 + r.(1          * cos(phi) * sin(psi));

(the 1s are there for clarity)

You could also use different constants (in place of r) on the terms for axially-aligned eccentricity.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Wow, that's awesome TheAdmiral! That looks a LOT cleaner than what I was using. And thanks for including the 'ones', because I'm autistic and sometimes have a hard time visualizing how a math formula works without it being written out as plainly as possible. The ones really helped! Thanks for the info, I really appreciate it!

But I'm a dummy when it comes to math terminology, especially when it comes to Greek letters. In my method, 'theta' is the radian value of the object's rotaton. I presume that's how you're using 'theta', but I don't fully understand what 'psi' is... Is that for the second axis of rotation? In other words, 'theta' is the rotational position of the orbiting entities along an axis, and 'psi' is the angle of the axis itself?
"The crows seemed to be calling his name, thought Caw"
Yes. I was just using traditional greek letters for angles.
Here:

theta rotates about the z-axis;
phi about the y-axis,
psi about the x-axis.

The rotations are 2pi periodic, so you don't have to worry too much about fmodf()ing the angles (although it's probably a good idea if they are going to get huge).

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Very cool, thanks for the clarification TheAdmiral! What confused me was that I didn't see the difference between the spellings of 'psi' and 'phi' (I thought they were both 'psi' - oops!), so I didn't see how one would differentiate between each of the possible three axes of rotation.

Thanks again for all your help, TheAdmiral. I really appreciate it! [smile]
"The crows seemed to be calling his name, thought Caw"

This topic is closed to new replies.

Advertisement