Reputation: 35
I'm working on making a Breakout game in C++ / SFML / OpenGL for learning purposes. This is my first trek into modern opengl.
My problem is that I can't figure out how to make the paddle move (or the ball for that matter). Right now, I translate the entire view, causing the camera to move but the shapes stay in their original position. I've been following certain tutorials online (Arcsysnthesis and open.gl mainly), but I'm just not understanding something, so I was wondering if I could get feedback. I know I need to translate the position of the paddle, presently I'm using the MVP matrix setup.
In my Vertex shader I have
gl_Position = MVP * vec4(position, 1.0);
Where MVP is a uniform mat4 variable. I use glm::perspective and glm::lookAt functions to generate the P * V portion of the MVP. Lastly, when I gather keyboard input in my update() function I use glm::translate and generate a Model. I know the multiplication order needs to be P*V*M (from what I've gathered reading tutorials), and I use the result to modify the uniform variable in the vertex shader. The result is that the screen / camera translates positions, but the shapes obviously don't move. I think I understand why this is happening, but I cannot figure out how to only translate the shapes and essentially control their movement.
EDIT - The comment below is right - all I needed wast to change my update / render sequence because I was using the same transform on all my shapes!
Upvotes: 2
Views: 6327
Reputation: 2471
In your C++ Code you may have a class for the paddle, which should contain the following:
Before calling the update method you generate the View matrix from the camera using glm::lookAt or something similar. Then you call the update method of the paddle and change the position according to you game logic. When you render the paddle you have to upload the view / modelview matrix to the shader using a uniform. Before setting the uniform you can change the modelview Matrix and translate the object using the following math:
glm::mat4 mvp = glm::translate(orgModelView, glm::vec3(x,y,z));
This will translate all objects which apply this modelview matrix.
The math behind this is here:
TransformedVector = TranslationMatrix * RotationMatrix * ScaleMatrix * OriginalVector;
A Modelview matrix is the following Matrix
[ R T ]
0 0 0 1
R is a rotation matrix and T a translation vector. If you do not want to rotate the object R is the identity and if you do not want to translate the object, T is (0,0,0)
Applying this to all vertices of an object you can rotate and translate the object (Attention the order of matrix multiplications are important).
If you want to move to objects different like the first to the right and the 2nd to the left, you will need two different Modelview matrices.
In my shaders I am doing it like this:
in vec3 pos;
uniform mat4 modelview;
uniform mat4 projection;
void main(){
gl_Position = projection * modelview * vec4(pos,1.0);
}
Upvotes: 4