Jump to content
  • Advertisement
Sign in to follow this  
TheKrane

OpenGL Get forward vector of TransformComponent

This topic is 912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently working on an Entity-Component based, 3d Engine in C++ and OpenGL.

Right now I am having problems getting the forward vector of my transform component.

void TransformComponent::Update()
{
    m_modelMatrix = glm::translate(m_position) * mat4_cast(m_rotation) * glm::scale(m_scale);
}

glm::vec3 TransformComponent::getForward()
{
    const float* matrix = glm::value_ptr(m_modelMatrix);

    std::cout << matrix[8] << " - " << matrix[9] << " - " <<  matrix[10] << std::endl;
    return glm::vec3(matrix[8], matrix[9], matrix[10]);
}

I read somewhere that the 8th, 9th and 10th element of my model matrix give me the forward vector of my transform.

Something is wrong with my model matrix as element 8 and 9 increase every frame and therefore give me weird results.

Any suggestions on how to tackle this problem?

 

Thanks in advance,

TheKrane

Edited by TheKrane

Share this post


Link to post
Share on other sites
Advertisement

You can extract the forward vector using the transpose of your model matrix and get the third row or column based on how you store the matrix.

If you store euler angles you can also compute the forward vector like that :

Forward.x = sin( Rotation.y );
Forward.y = -tan( Rotation.x );
Forward.z = cos( Rotation.y );

From a quaternion you can get it like that :

CVector3 CQuaternion::ComputeForwardVector() const
{
  const float x2 = 2.0f * x;
  const float y2 = 2.0f * y;
  const float z2 = 2.0f * z;
  const float x2w = x2 * w;
  const float y2w = y2 * w;
  const float x2x = x2 * x;
  const float z2x = z2 * x;
  const float y2y = y2 * y;
  const float z2y = z2 * y;
  return CVector3( z2x + y2w, z2y - x2w, 1.0f - ( x2x + y2y ) );
}
Edited by Alundra

Share this post


Link to post
Share on other sites

[x  y  z -]

[x  y  z -]

[x  y  z -]

[-  -  -  -]

The forward up and side vectors are x,y,z. So for me I always export models facing in openGL down the z-axis begin the forward direction. So I know that I can just grab the z component of the matrix and that is where the model is facing.  However, if you have any transforms going on, you may need to put a custom forward vector and multiply it by your transform matrix. For instance if your model is exported looking 45 degrees left, then you need to know that by default (going through the identity matrix), that it will display looking 45 degrees left.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!