# camera problems

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, im trying to use camera functions like goLeft and goRight using GLUT and GLU. so this is my code: in main():
void keyboard(unsigned char key,int x,int y)
{
switch (key)
{
case 'w':
cam.goUp(1);
glutPostRedisplay();
break;
case 'a':
cam.goLeft(1);
glutPostRedisplay();
break;
case 'd':
cam.goRight(1);
glutPostRedisplay();
break;
case 's':
cam.goDown(1);
glutPostRedisplay();
break;
default:
break;
};
}
in Camera class:
void Camera::updateCamera()
{
float px = m_vPos.x;
float py = m_vPos.y;
float pz = m_vPos.z;
float lx = m_vLookat.x;
float ly = m_vLookat.y;
float lz = m_vLookat.z;
gluLookAt(px,py,pz,lx,ly,lz,0,1,0);
};
/* Bla Bla Bla... */
void Camera::goUp(float fUnits)
{
if (fUnits < 0.0f)
fUnits += fUnits*-2;
m_vPos.y = fUnits+m_vLookat.y;
updateCamera();
};
void Camera::goDown(float fUnits)
{
if (fUnits > 0.0f)
fUnits -= fUnits*2;
m_vPos.y = fUnits-m_vLookat.y;
updateCamera();
};
void Camera::goRight(float fUnits)
{
if (fUnits < 0.0f)
fUnits += fUnits*-2;
m_vPos.x = fUnits+m_vLookat.x;
if (m_bWalking)
{
if (m_bUp)
m_vPos.y += 1.0f;
else
m_vPos.y -= 1.0f;
};
updateCamera();
};
void Camera::goLeft(float fUnits)
{
if (fUnits > 0.0f)
fUnits -= fUnits*2;
m_vPos.x = fUnits-m_vLookat.x;
if (m_bWalking)
{
if (m_bUp)
m_vPos.y += 1.0f;
else
m_vPos.y -= 1.0f;
};
updateCamera();
};

m_bWalking is set to false in the Camera initialization. camera position begins at 0,0,6 in direction 0,0,0 but when i run the program, it seems like every time i call a Camera function, it also goes backward and maybe roll itself (the camera). what am i doing wrong, how to fix it? thanks, pex.

##### Share on other sites
If I understad correctly what you want to do, I'd change this:

void Camera::goUp(float fUnits){	if (fUnits < 0.0f)		fUnits += fUnits*-2;	m_vPos.y = fUnits+m_vLookat.y;	updateCamera();};

to this:

void Camera::goUp(float fUnits){	if(fUnits < 0.0f)		fUnits = -fUnits;	// This does the same as above	m_vPos.y += fUnits;	// move center of view up	m_vLookat.y += fUnits;	// move viewer position up	updateCamera();}

And the same with the other directions. This will move the entire camera, thats what you wanted right?

Edit: Looking over your code again I suspect you might be wanting to make the camera pan instead of move is that right?
Edit2: Or do you want the camera to move and pan slightly in the direction it moves?

##### Share on other sites
From my little experience, I'd recommend you to store your yaw and pitch angles (this is, lookUpDown and lookLeftRight for example) and the eye position. Then, each time you need to set matrixes do some math and get a point for the camera's target.

This is an example:

gluLookAt(Position.X, Position.Y, Position.Z,           Position.X + 10.0f * cos (LeftRight * ((float)M_PI) / 180.0f ) * cos (UpDown * ((float)M_PI) / 180.0f ), 	  Position.Y + 10.0f * sin (UpDown * ((float)M_PI) / 180.0f ),           Position.Z + 10.0f * sin (LeftRight * ((float)M_PI) / 180.0f ) * cos (UpDown * ((float)M_PI) / 180.0f ),           0.0f, 1.0f, 0.0f);

First line is the camera's (eye) position. The second, third and fourth are the target point, which I set at 10.0 units from the eye, based on the direction pointed by UpDown and LeftRight. The last line is the camera's up vector, and that one seems to work ok if you don't look more -90 or 90 degrees up or down.

Hope it helps a bit.

##### Share on other sites
hm, i dont know what is a camera pan..
is it a camera 2d movement (left/right/up/down with same Z)? (thats what i need)

##### Share on other sites
Quote:
 Original post by pex22hm, i dont know what is a camera pan..is it a camera 2d movement (left/right/up/down with same Z)? (thats what i need)

Ok, then you need to change both the lookAt and the pos coordinates in the direction you want the camera to move.

For example if you want to move up 1 unit you do:

lookAt.y += 1;
pos.y += 1;

You might also consider if it is simpler to use one general move function instead of 4, like this:

void Camera::move(float xUnits, float yUnits){	m_vPos.x += xUnits;	m_vPos.y += yUnits;		m_vLookat.x = m_vPos.x;	m_vLookat.y = m_vPos.y;}// The go functions could then be defined using the above functionvoid Camera::goUp(float fUnits){	move(0.0f, fUnits);	// if you want to make sure it moves in a positive direction you can use:	// move(0.0f, fabsf(fUnits));}void Camera::goDown(float fUnits){	move(0.0f, -fUnits);// or move(0.0f, -fabsf(fUnits));}// and so on..

##### Share on other sites
hm, never though about creating a move() function..thanks for tip
but i still think my camera is going backward in each movement call..
for example, now this is my goRight function (i didnt used move() yet):
void Camera::goRight(float fUnits)	{	  if (fUnits < 0.0f)		  fUnits = -fUnits;	  m_vPos.x += fUnits;	  m_vLookat.x += fUnits;	  if (m_bWalking)		  {			if (m_bUp)			{				m_vPos.y += 1.0f;				m_vLookat.y += 1.0f;			}			else			{				m_vPos.y -= 1.0f;				m_vLookat.y -= 1.0f;			};		  };	  updateCamera();	};

and each time i call goRight, i see my triangle smaller and smaller ...
i dont know if its important, but this is my gluPerspective():
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1,150.0f);

@jjmontes: i dont have yaw/pitch in Camera and i store the eye position in m_vPos.

##### Share on other sites
Doesnt look like the problem is in the go function. One thing, you could make sure to set pos.x = lookAt.x and pos.y = lookAt.y so you are sure that they are the same value.

In fact, if you will always look in one direction, and dont need to move in the z-direction you only need two coordinates for position (and optionally a zoom value).

Why do you put semicolons after { }'s btw? They are not needed (Unless its a class/struct declaration or an array initialization).

Also, make sure you call glLoadIdentity() before you set the camera postion (you could try to do that in the updateCamera() function if you dont do it somwhere else).

[Edited by - Jesper T on October 13, 2004 4:42:46 AM]

##### Share on other sites
oh, i just like to put ; after }'s (sometimes i dont put).
i put glLoadIdentity() in updateCamera() and it works now. thanks! [smile]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46