月球相对于太阳的轨道
Moon orbit in relation to sun
我可以让地球绕着太阳和自己的轴旋转,但我不能让月球绕着地球旋转。(我只是想让它绕着它旋转,我不需要计算重力或类似的东西。)
下面是我的代码: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;
}
你知道我的代码有什么问题吗?到目前为止,我的对象上没有纹理,所以这就是为什么它在代码中缺失的原因。我只是想了解太阳系是如何运作的在我改变大小,轨道形状和类似的东西之前。
假设您按照我在https://stackoverflow.com/a/16594168/252687中建议的那样做了,那么您所要做的就是为Moon再次复制代码。由于天体没有严格的约束,独立计算它们的轨道比嵌套它们的参照系更明智。
地球的位置是:
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
试一试:
#include <GL/glut.h>
void display()
{
static int lastMs = glutGet( GLUT_ELAPSED_TIME );
int curMs = glutGet( GLUT_ELAPSED_TIME );
double dt = ( curMs - lastMs ) / 1000.0;
lastMs = curMs;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
gluPerspective( 60, w / h, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0, 0, -20 );
static float earth = 0;
static float moon = 0;
earth += 2 * dt;
moon += 36 * dt;
glPushMatrix();
{
glColor3ub( 255, 255, 0 );
glutSolidSphere( 4, 8, 8 );
glPushMatrix();
{
glRotatef( earth, 0, 0, 1 );
glTranslatef( 10, 0, 0 );
glColor3ub( 0, 255, 255 );
glutSolidSphere( 1.5, 8, 8 );
glPushMatrix();
{
glRotatef( moon, 0, 0, 1 );
glTranslatef( 2, 0, 0 );
glColor3ub( 128, 128, 128 );
glutSolidSphere( 0.5, 8, 8 );
}
glPopMatrix();
}
glPopMatrix();
}
glPopMatrix();
glutSwapBuffers();
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
相关文章:
- 我想知道变焦和轨道在哪里出了问题
- 无法在控制面板中创建轨道栏.示例代码不起作用
- 递归功能轨道目录未检测到所有文件夹
- WebRTC本机,AdiotrackSinkInterface添加到轨道上,但从未召集过ondata
- 编译轨道样品时的C 错误-LNK2019未解决的外部符号
- 使用C++创建带有“自定义”捕获器的WebRTC视频轨道
- mix_halt特定轨道
- 太阳工作室和" ",1号线:非法旗帜(-)
- 利用物理定律模拟轨道
- 平移 - 缩放 - 使用多点触控的轨道 - 虚幻引擎 -
- 如何重载模板类<< cout? 轨道结帐表单/视图:用户输入>查看输入>结帐
- 函数以过度振荡的轨道视角代替
- 物理月球-地球-太阳系统
- 轨道成员变量值更改
- 太阳运动(Y/Z运动)
- 将OpenGL深度测试应用于轨道球体
- 合并几个MP3轨道在一个轨道
- 是我还是部分Boost轨道SVN (Boost几何扩展溶解)是不可编译的
- 月球相对于太阳的轨道
- DirectX球体的轨道倾角