OpenGL OpenGL Vector Based Camera Class Troubles

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

Recommended Posts

I have been attempting to put together a camera class for a couple of weeks and trying various ways, but never any luck figuring it out. I would Like some help getting what I have done till now to work please. I have set up a camera class in opengl that uses 4 vectors to represent the camera axis and the it position in the "world". Position Forward Right Up I am trying to get my camera to have three modes, FPS style camera, Manual(free-fly) camera and a Agent or 3rd person camera. I havent attempted the 3rd person on as i havnt got any of the "standard" ones to work as yet. My fps camera works to a point actully until the point where you rotate 180 degrees. I rotate on the Y based on the World's Z axis so (0,0,-1), this is where the camera starts and is its facing. When i rotate the Forward vector, i then get the cross product of Forward and Up to recalculate Right. The camera works as it should until the camera facing is (0,0,1). forward movement is so
Position -= Forward*(GameSpeed*movementSpeed);

the call to camera rotateY is
Forward = defaultDirection.RotateY(angleY);
Right = Forward.CrossProduct(Up);

and rotation on the Y formula is
   static Vector3D rota;
//calculate a new look direction using the default start location of the
//camera
//this prevents overtime rounding down of values by the compiler as well as
//giving us the orgin axis
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
rota.x = x;
rota.y = y * cosAngle  + z *sinAngle;
rota.z = -y *sinAngle + z*cosAngle;

return rota;

when it is time to display the scene the calls to gl are
glRotatef(glCam->GetAngleX(),1.0f,0.0f,0.0f);
glRotatef(glCam->GetAngleY(),0.0f,1.0f,0.0f);
glTranslatef(glCam->GetX(),glCam->GetY(),glCam->GetZ());

Now I am fully lost on my Manual camera as movment on the right vector doesnt work. where
Position += Right*(GameSpeed*movementSpeed);

works for the FPS camera, on manual it effectivly loops back and forth between a very small point so effectivly standing still. for the Manual camera this is how i look down and right,(up and left are just negative values to these functions)
void GLCamera::LookUp(float GameSpeed)
{
if(invertCam !=true)
angleX -= GameSpeed*rotationSpeed;
else
angleX += GameSpeed*rotationSpeed;

//need to rotate on the local right vector, which doesnt change
//this gives a new look direction
//Forward = Forward.RotateLocalX(angleX,Right,Up);
Forward = defaultDirection.RotateX(angleX);
Up = Forward.CrossProduct(Right);
Up.Normalize();
}
void GLCamera::TurnRight(float GameSpeed)
{
angleY += GameSpeed*rotationSpeed;

//Forward = defaultDirection.RotateLocalY(angleY,Up,Right);
Forward = defaultDirection.RotateY(angleY);
Forward.Normalize();
Right = Forward.CrossProduct(Up);
}

I was not getting anywhere with my attempts at local rotation and found rotating from the axis works if i rotate on the X as well where in a FPS camera it is just the Y rotation that the Translation vectors need to change. here are my rotation codes in full
/****Move camera around*****/
//-Rotation-//
//using the given x,y,z use the Rotation function called to will rotate on the give axis to rotate along
Vector3D Vector3D::RotateX(float angle)
{
static Vector3D rota;
//calculate a new look direction using the default start location of the camera
//this prevents overtime rounding down of values by the compiler as well as giving us the orgin axis
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
rota.x = x;
rota.y = y * cosAngle  + z *sinAngle;
rota.z = -y *sinAngle + z*cosAngle;

return rota;

}
//Rotate around the local X axis, the local Z axis calls this function
Vector3D Vector3D::RotateLocalX(float angle,Vector3D const &X,Vector3D &Y)
{

float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
static Vector3D v1 = *this;

//v1 = (v1 * cosAngle) + (Y * sinAngle);
//v1.Normalize();
////create the new Up
//Y = v1.CrossProduct(X) *-1;

//original code attempt
//create the new Forward which is this object
v1 = (Y * -sinAngle) + (v1 * cosAngle);
v1.Normalize();

//create the new Up
Y = v1.CrossProduct(X);
Y.Normalize();

return v1;
}

Vector3D Vector3D::RotateY(float angle)
{
static Vector3D rota;
//rotate are current direction facing about the Y axis
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
rota.x = x* cosAngle - z *sinAngle ;
rota.y = y;
rota.z = x *sinAngle + z*cosAngle;
//assign the new rotation of the vector to the given vector
return rota;

}

//rotate around the local Y axis, the local Z axis calls this function
Vector3D Vector3D::RotateLocalY(float angle,Vector3D const &Y,Vector3D &X)
{
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
static Vector3D v1 = *this;
//v1 =  (v1 * cosAngle) - (X * sinAngle) ;
//v1.Normalize();

////calculate the new X
//X = v1.CrossProduct(Y);

//original code attempt
v1 = (X * sinAngle) + (v1 * cosAngle);
v1.Normalize();

//calculate the new X
X = Y.CrossProduct(v1);
X.Normalize();

return v1;
}

Vector3D Vector3D::RotateZ(float angle)
{
static Vector3D rota;
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
rota.x = x* cosAngle + y *sinAngle ;
rota.y = -x *sinAngle + y*cosAngle;
rota.z = z;
return rota;
}

//Rotate on the local Z axis, the local right axis calls the function
Vector3D Vector3D::RotateLocalZ(float angle,Vector3D const &Z, Vector3D &Y)
{
float sinAngle, cosAngle;
SinCos( angle, &sinAngle, &cosAngle );
static Vector3D v1 = *this;

//v1 = (v1 *cosAngle) + (Y * sinAngle);
//v1.Normalize();
////create the new Up
//Y = Z.CrossProduct(v1)*-1;

//original code attempt
v1 = (v1 *cosAngle) + (Y * sinAngle);
v1.Normalize();
//create the new Up
Y = Z.CrossProduct(v1);
Y.Normalize();

return v1;
}

so to iterate the errors and troubles I am having. In FPS mode since I rotate the cameras default look Z axis around the world Y axis. This works until I am looking at the positive Z direction. I dont know how to test I have reached this point and Forward.z never gets to 1 or greater to invert my default look. With the manual camera same problem with the Z, but a new problem arise in that I cant move sideways(i.e. strafe). What am i doing wrong please help, and if any more info from me is needed let me know.

Share on other sites
Please put the code inside [ "source" ] tags... without the ", of course.

I decided to drop my vector based camera and went for a matrix-based instead. It saved me a lot of headaches.. =)

Share on other sites
maybe this helps:

const float piover180 = 0.0174532925f;

this is what my forward movement looks like(as stolen out of a NeHe-tutorial):

cam_x += (float)sin(-lookleftright*piover180) * 0.05f;
cam_y += (float)cos(lookleftright*piover180) * 0.05f;

if not please explain(didnt get it):
can you move the camera around normally with your mouse?
or does a problem only occur if you try to walk left right up or down?
or do you only get a problem walking around if you face a certain angle?

Share on other sites
Working code for FPS/6DOF OpenGL-style object/camera.

(No support for 3rd-person, I'm afraid, but you should be able to add that fairly easily.)

Note that I'm not suggesting that you simply use the code as is (although you're welcome to if you wish); rather, I'm just offering it as a reference in the hopes that it might help you identify where your own code is going off the rails.

Share on other sites
Quote:
 if not please explain(didnt get it):can you move the camera around normally with your mouse?or does a problem only occur if you try to walk left right up or down?or do you only get a problem walking around if you face a certain angle?

well my rotation is done with the mouse and it does rotate correctly , so yeah the problems happen with the translation, so when i am walking around when i face the opposite direction its starts to go wrong, as in I look down the camera 0,0,-1 z axis. when i look down the 0,0,1 everything messes up

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• Forum Statistics

• Total Topics
632961
• Total Posts
3009487
• Who's Online (See full list)

There are no registered users currently online

×