0
votes

Using C++ and SFML. I have three entities. We will call them the Sun, the Earth, and the Moon.

First up, the sun, non-moving at all. Second, the earth, rotating around the sun, this seems to work just fine. Lastly, the moon, which does keep in rotation to a point but seems to be doing a large oval around the earth instead of doing a generic rotation around it. When I actually stop the earth from rotating, the moon rotates fine.

I am assuming that the earth position changing is effecting how the moon should rotate.

sf::Transform rotation;
rotation.rotate(mOrbitSpeed * dt.asSeconds(), mParent->getPosition());
sf::Vector2f positionAfterRotation = rotation.transformPoint(getPosition());
setPosition(positionAfterRotation);

The origin of the planets are set to the middle, I.E. so getPosition() gets the center of the planet as well. Get position returns the x and y coordinates. mParent would be the orbiting parent (I.E. Sun for Earth.)

Any suggestions? Thanks!

1
You want a simple circular orbit in the Earth's reference frame, but you're doing the rotations in the Sun's reference frame. I'm surprised you get a result as well-behaved as an oval. Rotate the Moon about the Sun with the Earth's orbital motion, then about the Earth with the Moon's orbital motion, and you'll be all right. - Beta

1 Answers

0
votes

Based on what Beta said above, the coding above has been changed. Might as well share.

sf::Transform orbitalRotation;
orbitalRotation.rotate(mOrbitSpeed * dt.asSeconds(), mParent->getPosition());

if (mParent->mCanOrbit && mParent->mOrbitSpeed != 0.f)
    orbitalRotation.combine(mParent->getOrbitRotation());

sf::Vector2f positionAfterRotation = orbitalRotation.transformPoint(getPosition());
setPosition(positionAfterRotation);
mRotation = orbitalRotation;

Pretty much what is done here is that I store the rotation and combine it with the parents rotation speed if there is any. mRotation is a saved variable. You might want to initialize it with an initial rotation value but other then that it seem to rotate. Also tested with the moon having a 'mini-satellite' attached, and it also worked fine three deep.

Thanks!