OpenGL Get forward vector of TransformComponent

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

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?

TheKrane

Edited by TheKrane

Share on other sites

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 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.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 13
• 9
• 9
• Forum Statistics

• Total Topics
634081
• Total Posts
3015384
×