I am trying to create the graphics system of a 2D game using OpenGL 3.3. To help me in this task I am using the GLM math libraries.
However I am having some trouble. When my objects (for now they are just squares) have a different scale, they "slide" through each other. I am posting some examples below.
In this picture both objects are at the center and camera is centered at the center
In this other picture the code has only moved the camera, but for some reason the big square moved down faster than the smaller one
Here are the matrices I am using to transform my camera
// Projection matrix : 120° Field of View, display range : 0.1 unit <-> 10000000 units
glm::mat4 Projection = glm::perspective(3.141516f/1.5f, (float)_WindowWidth / (float)_WindowHeight, 0.1f, 10000.0f);
//glm::mat4 Projection = glm::ortho(0.0f, 16.0f, 12.0f, 0.0f, 0.1f, 100000000.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(CameraPos.x, CameraPos.y ,2.0f), // Camera Position
glm::vec3(CameraPos.x, CameraPos.y ,1.0f), // Where the camera looks
glm::vec3(0,1,0) // This is another way to say camera is not rotated
);
And here are my matrices for my model transform
glm::mat4 ViewProj = View * Projection;
// Object to world matrix
glm::mat4 Model = glm::translate(glm::mat4(), glm::vec3(ElementToDraw.Position.x, ElementToDraw.Position.y, 1.0f)) *
glm::rotate(glm::mat4(), ElementToDraw.Rotation, glm::vec3(0.0f, 0.0f, 1.0f)) *
glm::scale(glm::mat4(), glm::vec3(ElementToDraw.Scale.x, ElementToDraw.Scale.y, 1.0f));
glm::mat4 MVP = Model * ViewProj;
I really don't know why this happens. Am I using some matrix wrong?
Another thing, this only happens on the axis of the different scale. If both objects have the same scale they don't "slide"
Thank you in advance :)
EDIT:
I tried using glm::ortho with the following parameters:
glm::mat4 Projection = glm::ortho(0.0f, 4.0f, 3.0f, 0.0f, 0.1f, 1000.0f);
EDIT 2:
I am passing the MVP using shaders
But the sliding continued and the objects werent centered at the center of the window