# Using gluLookAt to move camera in 2D iPhone game?

I'm trying to use gluLookAt to move the camera in my iPhone game, but every time I've tried to use gluLookAt my screen just goes "blank" ( grey in this case )

I'm trying to render a simple triangle and to move the camera, this is my code:

to setup my scene I do:

glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glRotatef(-90.0, 0.0, 0.0, 1.0); //using iPhone in horizontal mode
glOrthof(-240, 240, -160, 160, -1, 1);
glMatrixMode(GL_MODELVIEW);


then my "triangle rendering" code looks like:

GLfloat  triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);


This draws a red triangle in the middle of the screen, when I try to apply gluLookAt ( I got the implementation of the function from Cocos2D so I asume it's correct ), i do:

glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,1,0,0,0,0,0,1); // try to move the camera a bit ?

GLfloat  triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);


This leads me to grey screen (glClearColor is grey), I've tried all sort of things and read what I've found about gluLookAt on the net, but no luck :(, if someone could explain me or show me how to move to move the camera in a top-down fashion ( zelda, etc ), I would really appreciate it.

Thanks!

There's generally** no reason to use gluLookAt in a 2D game, it is meant for setting the camera position in 3D space. Use the simpler and faster glTranslatef instead.

If you really want your specific example to work then something like:

gluLookAt(200,0,1,200,0,0,1,0,0);


would move everything up by 200 pixels. But you could get the same effect by replacing gluLookAt with glTranslatef:

glTranslatef(-200, 0, 0);


** cocos2d supports this method of setting the camera for special '3D' effects. If that's what you want then you need to better understand how gluLookAt works. The projection example by Nate Robin can be a good place to start visualizing how projection works with that command. With that said, you should still be able to get similar 3D effects with just translation/scaling/rotation.

gluLookAt describes the position and orientation of the eye/camera in space. The first 3 arguments specify where the eye is located, the next 3 arguments specify where the eye is looking, and the last 3 specify the up direction that tells us how the camera is tilted. What this really does is define a viewing transformation by building a matrix.

In the gluLookAt example I gave, the eye position is (200, 0, 1). Because you rotated the projection matrix around the z axis earlier, I have to change x to move vertically and y to move horizontally. For the z coordinate, the point that we're looking at (200, 0, 0) should be in front of the eye, meaning you can't use a z coordinate of -1 for example because the look at point would be behind the eye. In this example it doesn't matter what z value you use as long as it is positive (comes before the look at point which has a z of 0).

The camera normally always looks straight ahead in 2D games so we use the same x and y for the look at point (200, 0) but we move it away from the camera in z direction (0). Finally we need to define a vector that specifies the up direction, this is normally (0, 1, 0) because the x-z plane represents the ground in many games. Since you rotated the projection matrix around the z axis I guess you could also use (1, 0, 0) and then you wouldn't need to swap x and y coordinates when specifying the position of the eye and look at point.

I still think it's simpler to just use the glTranslate and glRotate functions. If you familiarize yourself with their use you can easily write a simple camera class without the overhead of calling gluLookAt.

• Hey, thanks for your answer!, I was wondering if you could explain your gluLookAt example arguments a bit ? Why the 1 in the eyeZ parameter ( third arg ) , and why the 1 in the upY ? – Mr.Gando Feb 4 '11 at 15:24
• I updated my answer. Does that help? – Firas Assaad Feb 4 '11 at 20:37