与太阳有关的月球轨道 [英] Moon orbit in relation to sun

查看:118
本文介绍了与太阳有关的月球轨道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使地球绕太阳和绕其自身的轴旋转,但是我不能使月球绕地球旋转. (我只希望它在它周围循环,我不需要计算重力或类似的东西.)

I can get the Earth to rotate around the Sun and around its own axis, but I can't get the Moon to rotate around the Earth. (I just want it to circulate around it, I don't need to calculate gravity or anything like that.)

这是我的代码:

double earth_x = 50.0 * cos(orbit / 180.0 * Math::Constants<double>::pi);
double earth_y = 45.0 * sin(orbit / 180.0 * Math::Constants<double>::pi);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//sun
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(1.5f,1.0f,0.0f));
glTranslate(0.0f, 0.0f, 0.0f);
glRotate(0.0, 0.0, 0.0, 00.0);
drawEllipsoid(10.0, 1.0, 4, 4);



//Earth
glPushMatrix();
glTranslate(earth_x, earth_y, 0.0);
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(0.2f,50.0f,50.5f));
glRotatef(110,0.0,23.0,110.0f); 

glRotatef(orbit2, 0.0f, 0.0f,1.0f);
drawPlanetGrid(5, 1, 20, 20, 1.5);

glPopMatrix();

glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT,GLColor<GLfloat,4>(50.2f,50.0f,50.5f));
//Moon
glTranslate(earth_x+10, earth_y+10, 0.0);
glRotate(moonOrb, 0.0, 0.0, 1.0);

drawEllipsoid(1, 1, 9, 9);  

moonOrb += .5;
if (moonOrb > 360)
moonOrb = 0.0;
orbit += .9;
if (orbit > 360)    
{
    orbit = 0;
}
orbit2 += 6.5;
if (orbit2 > 360)
{
 orbit2 = 0;
}

任何想法我的代码有什么问题吗? 到目前为止,我的对象上没有纹理,因此这就是代码中缺少纹理的原因.在更改尺寸,轨道形状和类似的东西之前,我真的只是想了解太阳系是如何工作的.

Any idea what could be wrong with my code? As of right now I don't have texture on my objects, so that's why it's missing from the code. I am really just trying to understand how the solar system works before I make any changes to the sizes, orbit shape and things like that.

推荐答案

假设您按照我在 https://stackoverflow中的建议进行了操作. com/a/16594168/252687 ,您要做的就是再次为月球复制代码.由于天体不受严格约束,因此明智的做法是独立计算其轨道,而不是嵌套其参考系.

Assuming you did what I recommended in https://stackoverflow.com/a/16594168/252687, all you have to do is to replicate the code once more for the Moon. As celestial objects are not rigidly bound, it's wiser to compute their orbits independently, instead of nesting their reference frames.

地球的位置是:

earth_x = 30.0 * cos(earth_orbit / 180.0 * PI)
earth_y = 30.0 * sin(earth_orbit / 180.0 * PI)

月亮的位置是:

moon_x = earth_x + 15.0 * cos(moon_orbit / 180.0 * PI)
moon_y = earth_y + 15.0 * sin(moon_orbit / 180.0 * PI)

代码应如下所示:

drawTheSun();

glPushMatrix();  // enter the Earth's frame of reference
glTranslate(earth_x, earth_y, 0.0);  // move to the position of the Earth
glRotate(110, 0.0, 23.0, 110.0f);  // earth-local transformations
drawTheEarth();
glPopMatrix();  // exit the Earth's frame of reference

glPushMatrix();  // enter the Moon's frame of reference
glTranslate(moon_x, moon_y, 0.0);  // move to the position of the Moon
glRotate(110, 0.0, 23.0, 110.0f);  // moon-local transformations
drawTheMoon();
glPopMatrix();  // exit the Moon's frame of reference

这篇关于与太阳有关的月球轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆