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);
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 :)
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 ++