c++vectorglm-mathcross-product

Cross product of a vector with local y direction and another vector


So I have a cube represented by a glm::mat4 m_yellow_mat

enter image description here

and I performed a 30 degree rotation around z-axis

m_yellow_mat = glm::rotate(m_yellow_mat, glm::radians(30), glm::vec3(0, 0, 1));

Now refer the below picture

enter image description here

I want to find the cross product between the blue vector and red vector

I know the blue vector, but not sure how to find the red vector? Red vector represents the direction of the local y axis of cube?

cross( ?????? , blue_vector);

Solution

  • The local y axis in world space is stored in the second row of the matrix:

    vec3 yaxis_world = normalize(m_yellow_mat[1][0], m_yellow_mat[1][1], m_yellow_mat[1][2]);
    

    Explanation:

    The y axis in object space (let's call it yaxis) is by definition [0,1,0]. In order to transform a vector from object space to world space, we multiply the vector with the model matrix. Since we are interested in a direction, the homogeneous coordinate has to be 0:

    axisy_world = modelMatrix * yaxis
    
    axisy_world = modelMatrix * [0,1,0,0]
    

    When looking at the matrix multiplication, we notice that the this will return exactly the second row of the matrix.

    Note, that the result has to be normalized in order to cancel out scaling factors from the matrix. If it is guaranteed that only translations/rotations are contained, the normalization can be skipped.