0

I am trying to rotate an object around its own x-axis. It works exactly as expected when it is on the right side of the y-axis but showcases strange behavior when it on the left.

void Animation::rotate_x(float degree) {
    // get the position of the object wrt to origin
    glm::vec3 position = glm::vec3(m_model_mat[3]);
    glm::vec3 position_rev = -position;

    // get the angle of the obj wrt y-axis
    float angleY = atan2(m_model_mat[0][2], sqrt((m_model_mat[0][0] * m_model_mat[0][0]) + (m_model_mat[0][1] * m_model_mat[0][1])));
    angleY = angleY * 180 / M_PI;
    std::cout << angleY << "\n";
    if(position[0] < 0){
        if(angleY > 0){
            angleY = 90 + angleY;
        } else if (angleY < 0)
        {
            angleY = -90 + angleY;
        } else{
            angleY = 180;
        }   
    }
    std::cout << angleY << "\n";

    // rotate so angle with y is 0
    glm::mat4 rot_y_mat = glm::mat4(1.0f);
    rot_y_mat = glm::rotate(rot_y_mat, glm::radians(-angleY), glm::vec3(0.0f, 1.0f, 0.0f));
    

    // rotate to bring back to the correct position wrt y
    glm::mat4 rev_rot_y_mat = glm::mat4(1.0f);
    rev_rot_y_mat = glm::rotate(rev_rot_y_mat, glm::radians(angleY), glm::vec3(0.0f, 1.0f, 0.0f));

    // rotate on x
    glm::mat4 rotation_mat = glm::mat4(1.0f);
    rotation_mat = glm::rotate(rotation_mat, glm::radians(degree), glm::vec3(1.0f, 0.0f, 0.0f));
    
    // translate back to the original position
    glm::mat4 trans_to_position = glm::mat4(1.0f);
    trans_to_position = glm::translate(trans_to_position, position);

    // move obj to origin
    glm::mat4 trans_to_origin = glm::mat4(1.0f);
    trans_to_origin = glm::translate(trans_to_origin, position_rev);

    m_model_mat = trans_to_origin * m_model_mat;
    m_model_mat = rev_rot_y_mat * m_model_mat;
    m_model_mat = rotation_mat * m_model_mat;
    m_model_mat = rot_y_mat * m_model_mat;
    m_model_mat = trans_to_position * m_model_mat;
}

Here the m_model_mat is my object matrix which I am transforming.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    [tag:glm] != [tag:glm-math] – genpfault Sep 20 '22 at 21:26
  • I don't understand, why do you translate to origin and rotating on accumulated negative angle to rotate again and translate again? Wouldn't it be easier if you keep the position, rotation, scale, accumulate the rotation vector, and only then build a new model matrix. The if statements around angleY are also smelly – vikAy Sep 20 '22 at 22:37
  • 2
    see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) especially bullet #5 and the links in the end of answer... – Spektre Sep 21 '22 at 05:18
  • I have done it step by step for debugging purposes so I could visualize each transformation... – Dhruv Phansalkar Sep 22 '22 at 00:17

0 Answers0