PDA

View Full Version : help with my camera func



ShinGouki
05-08-2003, 05:26 PM
hi ive tried creating a simple camera movement function, for rotation i just change the look at vector. for movement ive tried working out the current vector from the pos vector to the look at vector, but for some reason when i go into different quadrants sometimes forward makes me go back, and all kinds of strange things happen...

///////Camera Functions////////////////////////////////////////////
void rotateFunc() ////Rotation vectors ........
{
vView[0]=vPosition[0]+(10*sin(cameraAngle)); //vector for rotating in circular motion
vView[2]=vPosition[2]+(10*cos(cameraAngle));
}

void throttleFunc()
{

vPosition[0]+=(cameraVector[0]*zoom); //function for calculating look vector and travelling along it
vPosition[1]+=(cameraVector[1]*zoom); //
vPosition[2]+=(cameraVector[2]*zoom);

vView[0]+=(cameraVector[0]*zoom);
vView[1]+=(cameraVector[1]*zoom);
vView[2]+=(cameraVector[2]*zoom);
}

////////Function for the looking mode setup etc and actual camera location............
void camera()
{

static float vectorLength=0;

vectorLength= (vView[0]-vPosition[0]) + (vView[1]-vPosition[1]) + (vView[2]-vPosition[2]);

cameraVector[0] = (vView[0]-vPosition[0])/vectorLength;
cameraVector[1] = (vView[1]-vPosition[1])/vectorLength;
cameraVector[2] = (vView[2]-vPosition[2])/vectorLength;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

rotateFunc();

// Define a perspective view volume - this creates a projection matrix
// and multiples it onto the top of the projection matrix stack
gluPerspective(45.0, ASPECT_RATIO, 1.0, 6000.0); //field of view, aspect ratio, near plane, far plane

gluLookAt(vPosition[0], vPosition[1], vPosition[2], //posx,y,z,
vView[0], vView[1], vView[2], //viewx.y.z
0.0, 1.0, 0.0);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
throttleFunc();
}
Here are all the functions im using. Anywone who can tell me where im going wrong or if you have any easy alternatives to what ime doing then please let me know. I also need a way of resizeing this so that when the window size is changed the camera changes with it.

shinpaughp
05-08-2003, 07:40 PM
Originally posted by ShinGouki:
hi ive tried creating a simple camera movement function, for rotation i just change the look at vector. for movement ive tried working out the current vector from the pos vector to the look at vector, but for some reason when i go into different quadrants sometimes forward makes me go back, and all kinds of strange things happen...

///////Camera Functions////////////////////////////////////////////
void rotateFunc() ////Rotation vectors ........
{
vView[0]=vPosition[0]+(10*sin(cameraAngle)); //vector for rotating in circular motion
vView[2]=vPosition[2]+(10*cos(cameraAngle));
}

void throttleFunc()
{

vPosition[0]+=(cameraVector[0]*zoom); //function for calculating look vector and travelling along it
vPosition[1]+=(cameraVector[1]*zoom); //
vPosition[2]+=(cameraVector[2]*zoom);

vView[0]+=(cameraVector[0]*zoom);
vView[1]+=(cameraVector[1]*zoom);
vView[2]+=(cameraVector[2]*zoom);
}

////////Function for the looking mode setup etc and actual camera location............
void camera()
{

static float vectorLength=0;

vectorLength= (vView[0]-vPosition[0]) + (vView[1]-vPosition[1]) + (vView[2]-vPosition[2]);

cameraVector[0] = (vView[0]-vPosition[0])/vectorLength;
cameraVector[1] = (vView[1]-vPosition[1])/vectorLength;
cameraVector[2] = (vView[2]-vPosition[2])/vectorLength;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

rotateFunc();

// Define a perspective view volume - this creates a projection matrix
// and multiples it onto the top of the projection matrix stack
gluPerspective(45.0, ASPECT_RATIO, 1.0, 6000.0); //field of view, aspect ratio, near plane, far plane

gluLookAt(vPosition[0], vPosition[1], vPosition[2], //posx,y,z,
vView[0], vView[1], vView[2], //viewx.y.z
0.0, 1.0, 0.0);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
throttleFunc();
}
Here are all the functions im using. Anywone who can tell me where im going wrong or if you have any easy alternatives to what ime doing then please let me know. I also need a way of resizeing this so that when the window size is changed the camera changes with it.

Part of the problem could be your vector length as it should always be positive and yours could be negative resulting strange behavior. Vector (line) length is defined as the sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1)). Another possibility is your CameraAngle - not sure how it is calculated but sin and cos expect radians not angles so verify that they are in radians. And, I am also not sure why you are multiplying the sin and cos by 10 when you want the vector formed by view and position to be unit vector which would be achieved automatically by removing the 10* as the sin and cos are complimentary for same angle and the line will always have a length of 1.

Hope some of that helps.

ShinGouki
05-08-2003, 07:55 PM
thanks a lot, and thanks again you've helped me quite a lot this week.

ShinGouki
05-08-2003, 08:27 PM
The camera works great now thanks again.