2

I am currently making a solar system in Ogre3D. The other planets revolve around the sun perfectly but when I try to revolve the moon around the earth, it revolves at a certain point NEAR earth, not around earth, which makes it intersect with the earth at times.

This is the code for creating the planet's distance from the origin (0,0,0)

    Vector3 location = Vector3::ZERO;
    float distance = revolverDistance;
    location.x = distance; location.z = distance; location.y = 0;
    planet->setPosition(location.x, location.y, location.z);

And this is the code for the revolution

Degree planetRevolution = Degree(revolution * evt.timeSinceLastFrame);
    Vector3 location = Vector3::ZERO;
    location.x = revolver->getPosition().x - revolve->getPosition().x; 
    location.z = revolver->getPosition().z - revolve->getPosition().z;
    float oldX = location.x;
    float oldZ = location.z;
    float newX = (oldX*Math::Cos(planetRevolution)) + (oldZ*Math::Sin(planetRevolution));
    float newZ = (oldX*-Math::Sin(planetRevolution)) + (oldZ*Math::Cos(planetRevolution));
    mPlanet->setPosition(newX, mPlanet->getPosition().y, newZ);
  • Revolve = A planet (ex. earth)
  • Revolver = Another planet that is revolving around another planet (ex. moon)

For variables, I have sunDistance = 0, earthDistance = 150, and moonDistance = 155, all in which are floats.*

I have tried setting float distance = revolveDistance + revolverDistance, and changing the distance of the moon to 5 but the point of which it revolves just moved slightly further away.

Thank you in advance!

REMINDER (Thank you to one of the people who answered for reminding me): I am not allowed to use the scene hierarchy.

Stuart Den
  • 51
  • 7
  • Just an idea (had a similar problem before). For moons you have to do a special type of revolution. You want the moon to not be based around the sun for revolution. Instead you focus on making a revolution around earth, so then you have an orbit for the moon based on the earth center. Then add the displacement from the earth's orbit as additional displacement since the moon follows earth (for practical purposes). It's not the perfect representation but it works. As for your code you will probably have to make a special case in your code for the moon ( if you are using OO polymorphism can work – Garrigan Stafford Dec 09 '17 at 09:46
  • 1
    Also it can be useful to store the distance of the orbit and correct the distance of the orbit as necessary (computer arithmetic is imperfect (at small numbers (floats)) and the computer doest work at infitesmial steps so small arithmetic over/underflows will eventually combine and cause weird errors) – Garrigan Stafford Dec 09 '17 at 09:53
  • @GarriganStafford I think that's what I've been trying to do! The code above is flexible as I can put anything in the parameters (SceneNode* revolver and revolve) since they are in a function. For the other planets, their code looks like `mercury->planetMovement(evt, sun->getPlanet(), mercury->getPlanet(), generalRotation, mercuryRevolution);` where sun->getPlanet is the SceneNode* revolve and mercury->getPlanet() is the SceneNode* revolver. My code for the moon is: `moon->planetMovement(evt, earth->getPlanet(), moon->getPlanet(), generalRotation, moonRevolution);` – Stuart Den Dec 09 '17 at 10:20
  • I think you should start with one class 'Object' that should store all needed values like name, radius, location, texture etc. and a virtual function update(float secondsSinceLastFrame) which is called for every object before a frame is rendered. As Garrigan mentioned derive an 'ChildObject' that stores parent, distance, degreePerSecond, degree. For ChildObject update implements the calculations, like degree += degreePerSecond * secondsSinceLastFrame. – Tobias Wollgam Dec 10 '17 at 09:38

1 Answers1

3

Your question is an excellent example what the scene graph hierarchy is good for :)

  • Set up an Ogre scene node at the sun's center point.
  • Attach the sun entity to the sun node
  • Place another scene node at the sun's position. This will be the planet's pivot node, around which it revolves.
  • Make the pivot node a child of the sun node.
  • Place a scene node at the planet's start position
  • Attach planet entity
  • make the planet node a child of the pivot node
  • Create another pivot scene node at the planet position and make it child of the planet's center node.
  • Create a scene node at the moon's position, attach moon entity to it and make it child at the moon pivot node.

Now Ogre will do everything for you without fiddling around with hardcoded positions. You will just have to rotate the pivot nodes, no need for setting positions anymore.

user2328447
  • 1,807
  • 1
  • 21
  • 27
  • Ahh, I should have mentioned this! This was all of my classmate's initial idea but our professor told us not to use the hierarchy for this specific project as it would be "too easy" for us :( – Stuart Den Dec 09 '17 at 10:13