How to revolve an object around another object (C++ and Ogre3D)?

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.


    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.

    链接地址: http://www.djcxy.com/p/95552.html

    上一篇: 异步屏幕更新为游戏玩法逻辑,C ++

    下一篇: 如何围绕另一个对象(C ++和Ogre3D)旋转对象?