围绕不断旋转的物体旋转物体 [英] Rotate object around constantly rotating object

查看:56
本文介绍了围绕不断旋转的物体旋转物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to simulate the solar system and need to get the moon to orbit a planet orbiting the sun

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

i am currently using the following code to rotate the planets

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

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

i'm trying to use the code below to make my moon orbit the earth however at the moment all i can do is rotate around a specific point.

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函数中找到一些知道地球位置的方法.

i think what i need to do is find some way of knowing earths position from the rotatef function.

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

I have a class for the planets with the following attributes and methods:

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();
}

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

the class' coordinates do not get updated when the planet rotates

有谁知道如何让它发挥作用?

Does anyone know how to get this to work?

推荐答案

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

Found a fix that works as intended in case anyone else is struggling with this concept

//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();
    //

希望这对其他人有帮助

这篇关于围绕不断旋转的物体旋转物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆