Louis Kuang
Louis Kuang

Reputation: 797

OpenGL rotation around an object while doing self rotation

I am trying to simulate a planet system in OpenGL using the programmable pipeline. Suppose now I have a planet A, whose centroid is planet_A_FixedPoint and a planet C, whose centroid is planet_C_FixedPoint. I want to let planet C rotate around planet A with rotation axis = normalize(planet_A_FixedPoint) and at the same time rotate around its own center with rotation axis = (0,1,0). I came up with the following codes but it didn't seem to work.

glm::mat4 rotateAroundPlanetA = glm::translate(glm::mat4(), planet_A_FixedPoint) * glm::rotate(glm::mat4(), degree, normalize(planet_A_FixedPoint)) \
    * glm::translate(glm::mat4(), -planet_A_FixedPoint);
planet_C_M_Matrix = rotateAroundPlanetA * planet_C_M_Matrix;
glm::vec4 tempFixedPoint = rotateAroundPlanetA * glm::vec4(planet_C_FixedPoint, 1.0f);
planet_C_FixedPoint = vec3(tempFixedPoint.x, tempFixedPoint.y, tempFixedPoint.z);
glm::mat4 rotateAroundPlanetC = glm::translate(glm::mat4(), planet_C_FixedPoint) * glm::rotate(glm::mat4(), degree, vec3(0, 1, 0)) \
    * glm::translate(glm::mat4(), -planet_C_FixedPoint);
planet_C_M_Matrix = rotateAroundPlanetC * planet_C_M_Matrix;

The achieved effect of the above codes is that planet C rotate around itself and rotate around the normalize(XA, YA, ZA) axis, but it does not align with the origin of planet A (XA, YA, ZA).

Can anyone tell me where I was wrong?

Upvotes: 1

Views: 307

Answers (1)

quazeeee
quazeeee

Reputation: 323

First of all, if you use scope operator (::) use it everywhere. Secondly, you code has expected behavior. If you want that planet C align with the origin of planet A, then you rotation axis need to be perpendicular plane, which contain origin C and origin A.

Upvotes: 1

Related Questions