2017-12-04 824 views
0

나는 태양과 지구라는 두 개의 행성을 가지고있다. 나는 그들로 하여금 자신들의 축을 중심으로 회전하고, 동시에 행성의 궤도를 돌리기를 바랍니다.행렬 곱셈 - 행성을 자신의 축으로 돌리고 부모의 궤도를 돌리는 방법?

이 두 가지 동작을 개별적으로 수행 할 수는 있지만, 어떻게 조합 할 것인가는 혼란 스럽습니다.

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

우후! 늘 그렇듯이, 질문을 해보면 대답 할 수있게되었습니다. 마지막 행 다음에, transform[0 to 2]이 4x4 행렬의 회전 (3 차원 공간에서의 위치를 ​​나타내는 transform[3]으로)을 나타냄을 알고, 이전 행렬 계산의 회전을 현재 행으로 대체하려고 생각했습니다. Badabing, 내 대답이 있습니다.

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