【问题标题】:Rotate object around constantly rotating object围绕不断旋转的对象旋转对象
【发布时间】:2014-12-28 02:09:11
【问题描述】:

我正在尝试模拟太阳系,需要让月球绕着绕太阳运行的行星运行

我目前正在使用以下代码来旋转行星

glPushMatrix();
        glRotated((GLdouble)(spin*earth.speed), 0.0, 0.0, 1.0);
        glTranslated(earth.xPos, earth.yPos, earth.zPos);
        earth.draw();
glPopMatrix();

我正在尝试使用下面的代码使我的月球绕地球运行,但是目前我所能做的就是围绕特定点旋转。

glPushMatrix();
    //define one time only start location
    bool start = true;
    if (start)
    {
        glTranslated(earthMoon.xPos, earthMoon.yPos, earthMoon.zPos);
        start = false;
    }
    //orbit earths start point
    //perfectly fits around earth
        glTranslatef(-0.1, -0.1, 0);
        glRotatef(spin*10, 0, 0, 1);
        glTranslatef(0.1, 0.1, 0);
        // need translation vector to follow earth


    //glTranslated(earthMoon.xPos, earthMoon.yPos, earthMoon.zPos);
    earthMoon.draw();
glPopMatrix();

我认为我需要做的是从 rotatef 函数中找到一些了解地球位置的方法。

我有一个具有以下属性和方法的行星类:

float radius;
float xPos;
float yPos;
float zPos;
float speed;
planet(float r, float x, float y, float z, float speed);
~planet();
void draw(void)
{
    glPushMatrix();
        glColor3f(0.0, 1.0, 1.0);
        glutSolidSphere(radius, 20, 10);
    glPopMatrix();
}

行星旋转时类的坐标不会更新

有谁知道如何让它工作?

【问题讨论】:

    标签: c++ opengl rotation glut


    【解决方案1】:

    绘制地球后不要弹出矩阵,
    那么你的新参考将是地球位置, 你只需要调用月球绘图代码,它就会围绕你的地球旋转。

    【讨论】:

    • 这很好,它现在主要围绕地球旋转,但它也经过地球的中心点
    • 编辑:它现在绕着中心运行,但是我似乎无法使轨道足够小以至于它不会撞到其他行星,我可以让它更大没有问题,但它不会做更小的控制轨道大小的代码是 glTranslatef(-0.01, -0.01, 0); glRotatef(spin * 10, 0, 0, 1); glTranslatef(0.01, 0.01, 0);
    • 没有在这里发布所有代码,这将很难做到,我会看看我的一个讲座是否能发现问题
    【解决方案2】:

    找到了一个可以按预期工作的修复程序,以防其他人在这个概念上苦苦挣扎

    //earth
        glPushMatrix();
            //earth orbit
            glRotated((GLdouble)(spin*earth.speed), 0.0, 0.0, 1.0);
            glTranslated(earth.xPos, earth.yPos, earth.zPos);
                //earth mooon
            glPushMatrix();
                //orbit around earth
                glRotatef(spin * 5, 0, 0, 1);
                glTranslatef(0.1, 0.1, 0.0);
                //rotate around self
                glRotated((GLdouble)spin, 0.0, 1.0, 0.0);
                //draw moon
                earthMoon.draw();
            glPopMatrix();
            //rotate around self
            glRotated((GLdouble)spin, 0.0, 1.0, 0.0);
            //draw earth
            earth.draw();
        glPopMatrix();
        //
    

    希望这对其他人有帮助

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2015-01-06
      • 2017-07-14
      • 2013-05-21
      相关资源
      最近更新 更多