我被要求在openGL中设计和制作太阳系动画。我在 C 中这样做。我有点困惑,我应该如何制作轨道动画。我应该如何增加每颗行星的旋转角度来控制它围绕太阳的轨道速度?
以下是我到目前为止编写的所有代码,只是试图逐步完成它:
#include <GL/glut.h>
#include <GL/glu.h>
#define FACTOR 30.0
#define SLICES 25
#define STACKS 25
//Viewing angle variables
int eye_x = 2.0;
int eye_y = 3.0;
int eye_z = 10.0;
int up_x = 0.0;
int up_y = 1.0;
int up_z = 0.0;
//Planet diameters in relation to earth
double sun_radius = 100.0;
double earth_radius = 1.0;
double moon_radius = 0.2724;
double mercury_radius = 0.383;
double venus_radius = 0.949;
double mars_radius = 0.532;
double jupiter_radius = 11.21;
double saturn_radius = 9.45;
double uranus_radius = 4.01;
double neptune_radius = 3.88;
double pluto_radius = 0.187;
//Planet distances from sun in relation to earth's distance
double mercury_distance = (sun_radius / FACTOR) + 0.387;
double venus_distance = mercury_distance + 0.723;
double earth_distance = venus_distance + 1.0;
double mars_distance = earth_distance + 1.52;
double jupiter_distance = mars_distance + 5.20;
double saturn_distance = jupiter_distance + 9.58;
double uranus_distance = saturn_distance + 19.20;
double neptune_distance = uranus_distance + 30.05;
double pluto_distance = neptune_distance + 39.24;
/**
* Init function initializing the background to black.
*/
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
glMatrixMode(GL_MODELVIEW | GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluLookAt(eye_x, eye_y, eye_z, 0.0, 0.0, 0.0, up_x, up_y, up_z);
}
/*
void stars()
{
int noOfStars = rand() % 10;
int i = 0;
while(i < noOfStars)
{
glColor3f(1.0, 1.0, 1.0);
glPointSize(20.0f);
int x = rand() % 10;
int y = rand() % 10;
int z = -8.0;
glBegin(GL_POINTS);
glVertex3f(x, y, z);
glEnd();
i++;
}
glFlush();
glutSwapBuffers();
}
*/
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//stars();
//"Zoom out"
glTranslatef(0.0, 0.0, -20.0);
//Draw sun
glColor4f(1.0, 0.5, 0.0, 0.3);
glutWireSphere(sun_radius / FACTOR, SLICES, STACKS);
//Draw mercury
//Rotate around sun
//glRotatef(, 0.0, 1.0, 0.0);
//Distance from sun to mercury
glTranslatef(mercury_distance, 0.0, 0.0);
glPushMatrix();
//glRotatef( , 0.0, 1.0, 0.0);
glColor4f(1.0, 0.75, 0.75, 0.3);
glutWireSphere(mercury_radius, SLICES, STACKS);
glPopMatrix();
/*
//Draw venus
//Distance from sun to venus
glTranslatef(venus_distance, 0.0, 0.0);
glPushMatrix();
glColor4f(1.0, 0.75, 0.75, 0.3);
glutWireSphere(venus_radius, SLICES, STACKS);
glPopMatrix();
//Draw earth
//Distance from sun to earth
glTranslatef(earth_distance, 0.0, 0.0);
glPushMatrix();
glColor4f(1.0, 0.75, 0.75, 0.3);
glutWireSphere(earth_radius, SLICES, STACKS);
glPopMatrix();
*/
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(1000, 1000);
glutCreateWindow("solar system");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
首先,任何行星与太阳的距离都没有一个——它们以开普勒椭圆运动。(两周前的一月初,地球离太阳最近,夏天离太阳最远。
因此,如果你想准确地做到这一点,行星的位置角不会以恒定的速度变化。你当然可以简化并说行星轨道足够接近圆圈,这无关紧要(并说冥王星不再是行星,所以它在那里甚至无关紧要)。
但是,让我们去找一个确切的解决方案:这是由牛顿定律支配的:
F = g * M_1 * M_2 / r^2 // How do you put equations here?
和能量守恒,用动能 E = M v^2/2 换取势能 E = - g * M_1 * M_2/r
因此,要模拟行星围绕太阳的运动,得到它的位置,它的速度和作用在它上面的引力,计算一个时间步后你最终的位置,计算新的速度和作用在它上面的力,然后重复。(对所有行星都做同样的事情,暂时忽略它们之间的引力相互作用。
那将是太阳系的实际模拟。如果您只想模拟任何给定时间的位置,请查找开普勒定律 - 本质上是应用牛顿定律的结果。
我刚刚看到上面的文章甚至有一个关于"位置作为时间函数"的部分 - 所以这应该对算法有所帮助。