TIGSource Forums

Developer => Technical => Topic started by: kurtss on March 18, 2013, 05:34:04 PM



Title: gluLookAt with position
Post by: kurtss on March 18, 2013, 05:34:04 PM
So I've got this:

(http://i.imgur.com/aK8bt3H.png)

But I want the center of the screen to be positioned on the player (red cube).

My setup for gluLookAt doesn't allow that:

Code:
GL11.glLoadIdentity();
GLU.gluLookAt(0, 16, 8, 0, 8, 4, 0, -1, 0);
GL11.glScalef(-1, 1, 1);
GL11.glTranslatef(-10, -8, -2);

Now if I want gluLookAt to follow the position of the cube (x and y), how would I do that?
Very new to this 3D stuff...


Title: Re: gluLookAt with position
Post by: epcc on March 18, 2013, 06:37:31 PM
instead of those 4 lines try:
Code:
GL11.glLoadIdentity();
GLU.gluLookAt(0, 16, 8, cube.x,cube.y,cube.z, 0, 1, 0);


Title: Re: gluLookAt with position
Post by: kurtss on March 18, 2013, 06:55:57 PM
instead of those 4 lines try:
Code:
GL11.glLoadIdentity();
GLU.gluLookAt(0, 16, 8, cube.x,cube.y,cube.z, 0, 1, 0);

(http://i.imgur.com/qIoSvms.png)

Better than what I had... but now the whole thing goes haywire when I move. Is there a way to keep the same orientation (like the first picture) but to follow the cube?


Title: Re: gluLookAt with position
Post by: eigenbom on March 18, 2013, 07:09:38 PM
Is your Y-AXIS of your world pointing upwards? The last 3 params of gluLookAt is the UP vector for the camera. If your y-axis is upwards then I'd say your matrix is getting messed up somewhere else..


Title: Re: gluLookAt with position
Post by: kurtss on March 18, 2013, 07:11:11 PM
Is your Y-AXIS of your world pointing upwards? The last 3 params of gluLookAt is the UP vector for the camera. If your y-axis is upwards then I'd say your matrix is getting messed up somewhere else..


Well, actually it's -1 because otherwise the entire thing is flipped upside down.


Title: Re: gluLookAt with position
Post by: justinfic on March 18, 2013, 10:01:04 PM
With the current code, the camera will stay in one place. You want the camera to move with the player, so you have to add the player's position to the camera's position.

Try something like this:

Code:
GLU.gluLookAt(0 + cube.x, 16 + cube.y, 8 + cube.z, cube.x,cube.y,cube.z, 0, 1, 0);


Title: Re: gluLookAt with position
Post by: eigenbom on March 18, 2013, 10:41:13 PM
Before you start moving your camera around, get it to track the cube properly. Make your world Y axis point up, this will save many headaches. And get used to thinking in terms of OpenGL's right handed coordinate system..

(http://viz.aset.psu.edu/gho/sem_notes/3d_fundamentals/gifs/left_right_hand.gif)

A very useful thing to do is to draw an axis into the world:

Code:
glBegin(GL_LINES)
glColor3f(1,0,0)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glColor3f(0,1,0)
glVertex3f(0,0,0)
glVertex3f(0,1,0)
glColor3f(0,0,1)
glVertex3f(0,0,0)
glVertex3f(0,0,1)
glEnd()