矩阵乘法 - 如何使行星绕其自身的轴旋转并绕其父轨道运行?

Matrix multiplication - How to make a planet spin on it's own axis, and orbit it's parent?

我有两个行星,太阳和地球。我想让它们绕着自己的轴自转,同时绕着地球转。

我可以让这两种行为单独工作,但我不知道如何将它们结合起来。

void planet::render() {
    mat4 axisPos = mat4(1.0f);
    axialRotation = rotate(axisPos, axisRotationSpeedConstant, vec3(0.0f, 1.0f, 0.0f));

    if (hostPlanet != NULL) {
        mat4 hostTransformPosition = mat4(1.0f);
        hostTransformPosition[3] = hostPlanet->getTransform()[3];
        orbitalSpeed += orbitalSpeedConstant;

        orbitRotation = rotate(hostTransformPosition, orbitalSpeed, vec3(0.0f, 1.0f, 0.0f));
        orbitRotation = translate(orbitRotation, vec3(distanceFromParent, 0.0f, 0.0f));

        //rotTransform will make them spin on their axis, but not orbit their parent planet
        mat4 rotTransform = transform * axialRotation;

        //transform *= rotTransform;

        //orbitRotation will make the planet orbit, but it won't spin on it's own axis.
        transform = orbitRotation;
    }
    else {
        transform *= axialRotation;
    }


    glUniform4fv(gColLoc, 1, &color[0]);
    glUniformMatrix4fv(gModelToWorldTransformLoc, 1, GL_FALSE, &getTransform()[0][0]);
    glDrawArrays(GL_LINES, 0, NUMVERTS);
};

哇哦!像往常一样,问这个问题让我能够回答它。在最后一行之后,知道 transform[0 to 2] 表示 4x4 矩阵中的旋转(transform[3] 表示 3D 中的位置 space),我想用之前的矩阵计算替换旋转当前的。 八大饼,我得到答案了

    transform = orbitRotation;
    transform[0] = rotTransform[0];
    transform[1] = rotTransform[1];
    transform[2] = rotTransform[2];