# Weird GLM perspective problems with my 2D Game

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"

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

The problem is your World matrix multiplication order.

What you do is

W = T * R * S; where W is the world transform, T is translation, R is rotation, and S is scaling.

W = S * R * T.

THis is because matrix multiplication is not commutative.

Hope that helps.

• I tried using ortho but it didn't work Sep 26, 2014 at 7:44
• Are you sure you used the correct parameters for that? This function takes very different parameters than that of perspective... Sep 26, 2014 at 7:45
• Yes and the sliding continued happening Sep 26, 2014 at 7:47
• It was even more exaggerated Sep 26, 2014 at 7:48
• Ah! got it!... getting it in an answer... Sep 26, 2014 at 7:51

I found the solution, I had to change this line:

glm::mat4 MVP = Model * ViewProj;


To this line

 glm::mat4 MVP =  ViewProj * Model;


I made some research and it seems that in DirectX its the other way around.