【发布时间】:2022-01-06 16:18:15
【问题描述】:
我正在尝试使用 OpenGL 对太阳系进行视觉“模拟”,并使用此功能围绕太阳运行行星(在圆形轨道上)。
glm::vec3 application::orbit(glm::vec3 thisPlanet, glm::vec3 otherPlanet, float rotSpeed, const time &dt)
{
float radius = glm::distance(thisPlanet, otherPlanet);
float angle = acosf((thisPlanet.x - otherPlanet.x) / radius) - atanf(1.0f) * 4.0f;
angle += dt.as_seconds() * rotSpeed;
if (angle > 2 * atanf(1.0f) * 4.0f)
angle -= 2 * atanf(1.0f) * 4.0f;
float x = otherPlanet.x + cosf(angle) * radius;
float z = otherPlanet.z + sinf(angle) * radius;
return glm::vec3(x, thisPlanet.y, z);
}
每个帧都会调用这个函数,如下所示:
void application::tick(const time &dt)
{
if (m_keyboard.key_released(GLFW_KEY_ESCAPE)) {
m_running = false;
}
m_controller.update(m_keyboard, m_mouse, dt);
m_cube_rotation += dt.as_seconds();
m_mercury_position = orbit(m_mercury_position, m_sun_position, 2.0f, dt);
// glm::mat4 world = glm::translate(glm::mat4(1.0f), m_cube_position)
// * glm::rotate(glm::mat4(1.0f), m_cube_rotation, glm::normalize(glm::vec3(1.0f, 1.0f, -1.0f)));
glm::mat4 sun = glm::translate(glm::scale(glm::mat4(1.0f), glm::vec3(2.0f, 2.0f, 2.0f)), m_sun_position)
* glm::rotate(glm::mat4(1.0f), m_cube_rotation, glm::normalize(glm::vec3(1.0f, 1.0f, -1.0f)));
glm::mat4 mercury = glm::translate(glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f)), m_mercury_position)
* glm::rotate(glm::mat4(1.0f), m_cube_rotation, glm::normalize(glm::vec3(1.0f, 1.0f, -1.0f)));
//m_crate.set_transform(world);
m_sun.set_transform(sun);
m_mercury.set_transform(mercury);
const int frames_per_second = int(1.0f / dt.as_seconds());
const int frame_timing_ms = int(dt.as_milliseconds());
m_overlay.pre_frame(m_width, m_height);
m_overlay.push_line("FPS: %d (%dms)", frames_per_second, frame_timing_ms);
}
地球为什么不动?
【问题讨论】:
-
您好 Anton,您似乎是在尝试通过将行星绕一圈移动来模拟轨道?大多数轨道实际上是椭圆的。看看我的代码:github.com/sjhalayka/mercury_orbit_glut/blob/…——它是 OpenGL / GLUT 中的水星轨道模拟器。并且在积分轨道时,强烈建议您使用 RK4(例如 Runge-Kutta order 4)。欧拉积分不能很好地模拟轨道。还有辛积分方案(例如 Verlet)。
-
它不是对轨道的实际模拟,我试图将其移动一圈
-
您是否检查过每次调用
application::orbit时计算angle的哪个值?我怀疑每次都是同一个角度。 -
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。