First some background info...
I have been working on a platformer game, which I draw with OpenGL. The platforms are cubes and the player is a cube.
The platforms and the player (both cubes) are given a position, from which the vertices should be drawn. However, the first cube is at position [0, 0, 0] and I would like the player to be on top of this cube. The size of the cubes and player is [0.5, 0.5, 0.5] initially.
However, I didn't manage to get the player on [0, 1, 0] (cubes are drawn from their center point I remembered), because the scaling didn't seem to work, which led me to believe the method I'm using to store the translations and scaling is inherently bad.
I am currently storing the translations and scaling in a matrix, which is part of the object. The object also holds the vertices and the VBO. Let's call it class Cube
, which thus holds glm::mat4 m_transform
.
I expose m_transform
and pass it with glUniformMatrix4fv(shader.GetUniformLocation("model"), 1, GL_FALSE, glm::value_ptr(m_transform));
And at some point I call glDrawArrays(GL_TRIANGLES, 0, 36);
to draw the platform/player cube.
So to alter the position of such a cube, I would alter m_transform
. Such as: m_transform = glm::translate(m_transform, vec);
where glm::vec3 vec
is the desired translation to apply.
However, the issues arise when u wish to scale a cube as well, so if we scale with vec3(0, 2, 0)
, the translations will be twice as large. And I'm unsure where to go from here.
I've read that having different matrices for translation, rotation and scaling is the way to go, which I will try, however, I would like to know what the best practices are surrounding this subject.
Any suggestions are much appreciated!