2d Camera problem

I want to create a 2D camera but it does not follow the player and I do not really understand the gluLookAt.
Camera class http://pastebin.com/D8iECncx
Main class http://pastebin.com/D4RvFe5y

See the documentation of gluLookAt.
But gluLookAt() is only really suited for 3D. When you want to use 2D and want the camera to be “above” your player looking onto the XY-plane, then do [icode]glTranslatef(-x, -y, 0.0f)[/icode] instead of gluLookAt in your Camera’s update() method.
You probably also want to reset the OpenGL modelview/projection matrix each frame (so as not to accumulate the transformations of previous frames) by using [icode]glLoadIdentity()[/icode].

I do not have experience with GLU, but the basics of a camera is that it simulates a camera. So all it has to do is render all objects on their positions and the opposite way of the camera.

(Something like player.pos.x = player.pos.x - camera.pos.x)

So what the problem here is, that the player does not get subtracted to the camera.

[quote]See the documentation of gluLookAt.
But gluLookAt() is only really suited for 3D. When you want to use 2D and want the camera to be “above” your player looking onto the XY-plane, then do

glTranslatef(-x, -y, 0.0f)

instead of gluLookAt in your Camera’s update() method.
You probably also want to reset the OpenGL modelview/projection matrix each frame (so as not to accumulate the transformations of previous frames) by using

glLoadIdentity()

.
[/quote]
That worked but the camera does not follow the player perfectly. When I walk for too long the player actually escapes the camera.

Could you probably be alot more precise in your problem description?
What do you mean with “the camera does not follow the player perfectly”?
What do you mean with “when I walk”? How do you walk? What does “walk” mean in your context?
What do you mean with “too long”? What is long? What is too long? How do you measure it?
What do you mean with “the player actually escapes the camera.”? How does it escape? In which direction does it escape? How fast does it happen?
What are the critical values of the fields in the Camera and player classes at that moment? Debugging?!
How do you setup the rest of the projection/modelview matrix?
Do you use glOrtho() or glScalef()?
Maybe show more of your code.

Currently, it looks like your player’s position and your camera’s position are not specified in the same coordinate system. One may be a scaled version of the other.

Check out the site wiki. I made a thread just for 2d cameras. It uses trig. If you can move the camera in a cardinal directions, you can follow through that tutorial.

This is what I mean with not perfectly:
kLrBRZsJUt0

This is what I mean with Walking: http://pastebin.com/CFVpEvWB
With the player escapes the camera I mean he gets out of view.
I use glOrtho();

I seem to be having some trouble with this issue as well.

I’m using this approach and it works though it seems a bit jittery and am looking at the proper way to pan the camera.


gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(Player.x - Player.startX, Player.y - Player.startY, width + Player.x - Player.startX, height + Player.y - Player.startY);
gl.glMatrixMode(GL10.GL_MODELVIEW);

It’s basically keeping track of how far the player has moved from the origin and resetting the projection each time. It does follow the player around correctly but I’m sure this isn’t the proper way to move the camera. :persecutioncomplex:

What is your draw command for rendering the player?

public static void DrawQuadTex(Texture tex, float x, float y, float width,
		float height) {
	tex.bind();
	glTranslatef(x, y, 0);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0);
	glVertex2f(0, 0);
	glTexCoord2f(1, 0);
	glVertex2f(width, 0);
	glTexCoord2f(1, 1);
	glVertex2f(width, height);
	glTexCoord2f(0, 1);
	glVertex2f(0, height);
	glEnd();
	glLoadIdentity();

}

When GLU.gluLookAt and glTranslatef are called, it’s telling Open GL to modify the current matrix. In the modern versions of Open GL, there are three separate Matrices. 1) Model 2) View 3) Projection. With Open GL 1 the model and view matrices are combined. It looks like the code is modifying this model/view matrix twice. Once in camera and then again in your player.

So, what may be happening is that the camera looks directly at the player, but then the player is translated outside of that viewpoint.

And how can this be solved? I still have not solved this after a long long time.

In modern OpenGL there are no builtin matrices at all anymore. There is only a clip space output variable “gl_Position” in the vertex shader.
Everything else you want to do, you can do however you like, such as decomposing your transformations into any number of intermediary matrices, such as “model,” “view” and “projection” mat4 uniforms in your shader.

Get rid of the translate method in your player render call.

I would read up on how the matrices work with draw calls. Not necessarily how to do matrix math but at least a top level understanding of them.

Whats the pros of directly modifying the matrix directly over using Vectors to equate all the math? I find it much simpler to calculate a vector then apply it for the finale. That way I can do vector-type math, such as cross vector calcuation, etc.

I believe it’s because a single matrix can contain translations, rotations, and scales

But like KaiHH mentioned the matrices are defined outside the shader program and passed in. I learned to use one projection matrix that’s defined once the app starts and never again, one view matrix that pretends to be a camera, and then everything else get’s its own matrix for position, rotation, and scale.

Then each model that is rendered may use the one view matrix and the one projection matrix in the shader program while each having its own unique matrix.

I use 3 vectors for rotation, scale, and positions. The rotation is great to have as a vector because you basically preprocess the direction it should be facing. In the case of vectors, you can easily make something point to something else by based on the Y. As the equations for applying numbers to the matrix goes (translate(), rotate()) it seems like you need to compound everything out before the incorporation of matrix.

The rest of your reply wasn’t really relevant to what I was asking. I was asking corely about the purpose of directly modifying the matrix over using vectors with the matrix. Although, they are valid points of the end goal of using matrices - they are compounded together uniformly for the end result (being rotation, location, and scale from each matrix compounded).

In my app I have one float buffer representing vertices for one object but have a lot of that type and every one has it’s own matrix. Whenever I want to move it I just use a library that does the matrix math for me. It’s as simple as passing in the existing matrix and having the method translate it. I guess it just saves the hassle of having to create two variables. Though the library does support multiplying a matrix and a vector so it’s really whatever floats your boat.

It’s good you have a library to do all the maths for you. I have one too. Though using libraries, its important to know what you are dealing with. So I am asking the question of the advantages (I guess the things you can do) with editing a matrix directly that you can’t do (or do easily) with vectors.

A vector is really just a column/row matrix so mathematically similar stuff can be done to it. Though with the 4x4 matrix the advantage is that one matrix can be applied to a complex model of say 1000 different vertices.