【问题标题】:Orbiting an object around another object, but it doesn't move一个物体绕着另一个物体旋转,但它不动
【发布时间】: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 的哪个值?我怀疑每次都是同一个角度。
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: c++ opengl glfw glm-math


【解决方案1】:

这就是我构建模型矩阵的方式:

mat4 model_mat;
const vec3 n_forward = dir.x;
const vec3 n_up = vec3(0, 1, 0);
const vec3 n_left = dir.z;

// construct a basis and a translation
model_mat[0] = normalize(vec4(n_left, 0.0f));
model_mat[1] = normalize(vec4(n_forward, 0.0f));
model_mat[2] = normalize(vec4(n_up, 0.0f));
model_mat[3] = vec4(p, 1.0f);

位置(例如 p)由参数圆方程 p = r cos(t) + r sin(t) 给出,其中 t 是经过的时间,r 是圆形轨道半径。另见:https://www.mathopenref.com/coordparamcircle.html

这有帮助吗?

【讨论】:

  • 又是如何解决地球不动的问题的呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 2014-01-27
  • 2015-04-26
  • 1970-01-01
相关资源
最近更新 更多