2017-12-04 25 views
0

私は2つの惑星、太陽と地球を持っています。私は彼らに彼ら自身の軸で回転させ、同時に惑星の軌道を回るようにします。行列の乗算 - 惑星を自軸上に回転させ、親の軌道を周回するにはどうすればいいですか?

私はこれらの2つの動作を個別に動作させることができますが、それらをどのように組み合わせるかは分かりません。

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

答えて

0

Woohoo!いつものように、質問をすることで私はそれに答えることができます。最後の行の後に、transform[0 to 2]が4x4行列の回転を表していることを知って(私はtransform[3]が3D空間内の位置を表しています)、私は以前の行列計算からの回転を現在のものと置き換えると考えました。 Badabing、私は私の答えを得た。

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