Follow 2d player openGL

Viewed 279

So I am ecountering this small problem where my camera is fixed on the player wrongly. Game screen

The blue sprite in the left upper corner is the player but it is supposed to be in the center of the screen. All the other threads on this matter where using the fixed rendering pipeline while I use the VBO based one.

My matrices are as followed:

Transform matrix:

glm::vec2 position = glm::vec2(x, y); 
glm::vec2 size = glm::vec2(width, height); 
this->transform = glm::translate(this->transform, glm::vec3(position, 0.0f));
this->transform = glm::scale(this->transform, glm::vec3(size, 1.0f));

Projection matrix:

glm::mat4 Screen::projection2D = glm::ortho(0.0f, (float)800, (float)600, 0.0f, -1.0f, 1.0f);

View matrix (where translation is the translation of the player):

Screen::view = glm::lookAt(translation, translation+glm::vec3(0,0,-1), glm::vec3(0,1,0));

And the vertex shader:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

uniform mat4 transform;
uniform mat4 projection;
uniform mat4 view;


out vec2 TexCoord;


void main()
{
 gl_Position = projection * view* transform *  vec4(aPos.xy, 0.0, 1.0);
 TexCoord = aTexCoord;
}

So what is going wrong here. Is there something I did not understand about the way it works? Or did I make a minor mistake somewhere?

1 Answers

So I found the answer myself XD,

The translation has to be centered by subtracting half of the screen width and height.

glm::vec3 cameraPos = glm::vec3(translation.x-Screen::width*0.5f, translation.y-Screen::height*0.5f, translation.z);

Related