Skip to main content
gamedev.net
🔒 Locked

Camera

Started by VitaliBR 15 replies 2.4k views
Original Post
VitaliBR
VitaliBR
I tried to apply a camera in my code,
but their movements are not correct, I not get fix it.

If someone can look at the code and try to help fix it
I'm using Code::Blocks (Opengl w/ SDL)

http://www.easy-share.com/1911197554/GL_GAME.rar

The functions of camera are:

In your render function before drawing anything you have to call Cam.setView(); To rotate around the (global) y-axis you call Cam.rotateGlob(degree, 0,1,0); while to look up/down you would call Cam.rotateLoc(degree, 1,0,0); Moving forward would be Cam.moveLoc(0,0,distance); while being shoved in a fixed direction would be Cam.moveGlob(deltaX, deltaY, deltaZ);


I am trying to fix almost 2hours
Thanks
http://mateusvitali.wordpress.com/
VitaliBR
VitaliBR
what is the difference between GLfloat and float?

[Edited by - VitaliBR on July 3, 2010 12:34:25 AM]
http://mateusvitali.wordpress.com/
Kasya
Kasya
Quote:
what is the difference between GLfloat and float?


nothing.

typedef float GLfloat; //in gl.h i think
VitaliBR
VitaliBR
Thanks

I managed to fix the camera,
I'm trying now is to make works with two keystrokes.
For example 'w' and 'a' it will move forward and turning left to go.

Here is the code:
    //We read the state of the keys    Uint8* keys = SDL_GetKeyState(NULL);    //With the Left arrow is pressed?    if (keys[SDLK_LEFT])    {        Camera.StrafeRight(-0.1);    }    //With the Right arrow is pressed?    else if (keys[SDLK_RIGHT])    {        Camera.StrafeRight(0.1);    }    //With the Up arrow is pressed?    else if(keys[SDLK_UP])    {        Camera.RotateX(0.5);    }    //With the Down arrow is pressed?    else if(keys[SDLK_DOWN])    {        Camera.RotateX(-0.5);    }    //With the 'W' arrow is pressed?    else if(keys[SDLK_w])    {        Camera.MoveForward(-0.1);    }    //With the 'S' arrow is pressed?    else if(keys[SDLK_s])    {        Camera.MoveForward(0.1);    }    //With the 'A' arrow is pressed?    else if(keys[SDLK_a])    {        Camera.RotateY(0.5);    }    //With the 'D' arrow is pressed?    else if(keys[SDLK_d])    {        Camera.RotateY(-0.5);    }



Thanks

[Edited by - VitaliBR on July 3, 2010 2:10:03 PM]
http://mateusvitali.wordpress.com/
android_808
android_808
I've just got back from holiday so I'm a little tired and may be being stupid. Basically IIRC if the user presses the left key down, none of the other logic will be checked because the first 'if' statement was met.

Break it down into sections. It makes it a bit easier to read and keeps the related code together. The example below shows that if 'w' is not pressed it checks for 's' and that the end of the MoveForward related code.
// Forward/backward codeif(keys[SDLK_w]){    Camera.MoveForward(-0.1);}else if(keys[SDLK_s]){    Camera.MoveForward(0.1);}// Rotation codeif(keys[SDLK_a]){    Camera.RotateY(0.5);}else if(keys[SDLK_d]){    Camera.RotateY(-0.5);}


The only kink is if 'w' and 's' are pressed in which case the code for 'w' is executed. One way round would be to replace all 'else if' statements with 'if'. That would solve the problem by running the MoveForward code twice, once to go forward and then once to return you to your original position.
VitaliBR
VitaliBR
friend, I'm embarrassed.
How could not I see this?

I'm sorry, sometimes it is something so simple that goes unnoticed.
Thank you very much!! :)


As for handling the mouse, I have an idea(generic), something like:

void camera (void) {    glRotatef(xrot,1.0,0.0,0.0);  //rotate our camera on teh x-axis (left and right)    glRotatef(yrot,0.0,1.0,0.0);  //rotate our camera on the y-axis(up and down)    glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera}void mouseMovement(int x, int y) {    int diffx=x-lastx; //check the difference between the current x and the //last x position    int diffy=y-lasty; //check the difference between the current y and the //last y position    lastx=x; //set lastx to the current x position    lasty=y; //set lasty to the current y position    xrot += (float) diffy; //set the xrot to xrot with the addition //of the difference in the y position    yrot += (float) diffx;    //set the xrot to yrot with the addition// of the difference in the x position}


the logic is correct? would use a function from the SDL to get the x and y position of the mouse right?

thanks
http://mateusvitali.wordpress.com/
VitaliBR
VitaliBR
Can someone explain to me the theory of how to use the mouse to move the camera (like the games FSP)?
http://mateusvitali.wordpress.com/
VitaliBR
VitaliBR
I'm trying to get my camera with the mouse.

When moving the mouse (on the axis X and Y), the camera makes strange rotations. I'm putting my full code for download (Source + Executable).

http://www.mediafire.com/?vdzjuocjim2

Please help me, I'm days trying to solve this problem

Thanks
http://mateusvitali.wordpress.com/
Zakwayda
Zakwayda
Quote:
I'm putting my full code for download (Source + Executable).

http://www.mediafire.com/?vdzjuocjim2
Perhaps you could also post the relevant code here (you might get more folks to look at it that way).
VitaliBR
VitaliBR
Sorry, I'll put the most important parts then.

About Camera:
//...	//as we know the up vector, we can easily use gluLookAt:	gluLookAt(	Position.x,Position.y,Position.z,				ViewPoint.x,ViewPoint.y,ViewPoint.z,				UpVector.x,UpVector.y,UpVector.z);        //...


Function of the mouse motion:

void CCamera::Mouse(const int Width, const int Height){   //Mid x/y   int mid_x = Width  >> 1;   int mid_y = Height >> 1;   //Mouse coord   int mouseX,mouseY;   //Get the position of mouse in the screen   SDL_GetMouseState(&mouseX,&mouseY);    //Difference between the midpoint and the mouse position after of movimented    float relative_mouseX = mid_x - mouseX;    float relative_mouseY = mid_y - mouseY;   //the mouse cursor is at the center of the screen   SDL_WarpMouse(mid_x, mid_y);   //ViewPoint   SF3dVector ViewPoint = Position+ViewDir;   //Angles   const float      angle_y = relative_mouseX/10,      angle_x = relative_mouseY/10,      angle_z = 0;   //Viewpoint Y   ViewPoint.y += angle_y * 2;   if((ViewPoint.y - Position.y) >  8)  ViewPoint.y = Position.y + 8;   if((ViewPoint.y - Position.y) < -8)  ViewPoint.y = Position.y - 8;   //Viewpoint X   ViewPoint.x += angle_x * 2;   if((ViewPoint.x - Position.x) >  8)  ViewPoint.x = Position.x + 8;   if((ViewPoint.x - Position.x) < -8)  ViewPoint.x = Position.x - 8;   //Rotate methods   if(angle_y)        RotateY(angle_y);   if(angle_x)        RotateX(angle_x);   if(angle_z)        RotateZ(angle_z);}


Events of Keyboard and Mouse:

            //With the Left arrow is pressed? - Rotate to left    if (keys[SDLK_LEFT])    {        Camera.RotateY(0.5);    }    //With the Right arrow is pressed? - Rotate to right    else if (keys[SDLK_RIGHT])    {        Camera.RotateY(-0.5);    }    //With the Up arrow is pressed? - Look to up    if(keys[SDLK_UP])    {        Camera.RotateX(0.5);    }    //With the Down arrow is pressed? - Look to down    else if(keys[SDLK_DOWN])    {        Camera.RotateX(-0.5);    }        case SDL_MOUSEMOTION:    {        Camera.Mouse(800,600);    }
http://mateusvitali.wordpress.com/
Zakwayda
Zakwayda
Can you post your RotateX/Y/Z() functions?

Also, is this code based on a tutorial of some sort? If so, can you provide a link to the tutorial?
VitaliBR
VitaliBR
Here:

void CCamera::RotateX (GLfloat Angle){	RotatedX += Angle;	//Rotate viewdir around the right vector:	ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)								+ UpVector*sin(Angle*PIdiv180));	//now compute the new UpVector (by cross product)	UpVector = CrossProduct(&ViewDir, ⇀)*-1;}void CCamera::RotateY (GLfloat Angle){	RotatedY += Angle;	//Rotate viewdir around the up vector:	ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)								- RightVector*sin(Angle*PIdiv180));	//now compute the new RightVector (by cross product)	RightVector = CrossProduct(&ViewDir, &UpVector);}void CCamera::RotateZ (GLfloat Angle){	RotatedZ += Angle;	//Rotate viewdir around the right vector:	RightVector = Normalize3dVector(RightVector*cos(Angle*PIdiv180)								+ UpVector*sin(Angle*PIdiv180));	//now compute the new UpVector (by cross product)	UpVector = CrossProduct(&ViewDir, ⇀)*-1;}


I basie in the following tutorials:
http://www.euclideanspace.com/software/games/threed/openglcpp/keyboard/index.htm
http://www.codecolony.de/docs/camera2.htm

thanks
http://mateusvitali.wordpress.com/
earl
earl
What you're describing as 'strange rotations' is due to the accumulation of floating point errors over multiple frames. Because floating point numbers don't have infinite precision, rounding errors will gradually build up. Eventually your Up, Right and Forward vectors will drift and the camera's up will no longer align with the rest of the world.

To fix this, your best bet is to build the camera view vectors from scratch every frame, using accumulated yaw and pitch values.
VitaliBR
VitaliBR
Let's see if I understand.

You said that the float is impeded by lack of rounding, right?
And that every frame should I build the vectors of the camera from scratch, so in my code I'll have to redo everything again?(Without the need to rewrite my code from scratch again) or is there a way to get it to start from scratch every frame?

Sorry, got a little more lost.

Thanks
http://mateusvitali.wordpress.com/
VitaliBR
VitaliBR
I read some discussions here on GameDev, and I'm reading the tutorial http://www.toymaker.info/Games/html/camera.html

I hope you help me understand that I'm wrong, but still hope that you will want to help me

a video example of what happens with the camera:


Thanks

[Edited by - VitaliBR on July 6, 2010 10:18:27 AM]
http://mateusvitali.wordpress.com/
earl
earl
Actually, looking at that video, it seems the problem is actually that you are rotating the camera around the local y axis, rather than the world y axis. So when pointing the camera down, any rotation around the y axis will cause the camera to no longer be level with the ground.

The same fix basically applies, and would be required regardless.

Add a new ResetOrientation method to CCamera:

void CCamera::ResetOrientation(){	ViewDir = F3dVector( 0.0, 0.0, -1.0);	RightVector = F3dVector (1.0, 0.0, 0.0);	UpVector = F3dVector (0.0, 1.0, 0.0);	RotatedX = RotatedY = RotatedZ = 0.0;}


Fix up the Mouse method so it resets the orientation and uses accumulated rotation (also removed junk):

void CCamera::Mouse(const int Width, const int Height){	//Mid x/y   int mid_x = Width  >> 1;   int mid_y = Height >> 1;   //Mouse coord   int mouseX,mouseY;   //Get the position of mouse in the screen   SDL_GetMouseState(&mouseX,&mouseY);    //Difference between the midpoint and the mouse position after of movimented    float relative_mouseX = mid_x - mouseX;    float relative_mouseY = mid_y - mouseY;   //the mouse cursor is at the center of the screen   SDL_WarpMouse(mid_x, mid_y);   // Store old rotation before we reset it   float old_rotatedX = RotatedX;   float old_rotatedY = RotatedY;   //Reset view vectors   ResetOrientation();   //Angles   const float      angle_y = relative_mouseX/10.0f,      angle_x = relative_mouseY/10.0f;   //Rotate methods   RotateY(old_rotatedY + angle_y);   RotateX(old_rotatedX + angle_x);}


While you can probably just copy and paste the above, I highly advise you to understand the problem fully, and also understand how the above fix works. Feel free to ask any questions you have about it here!

Eventually you will want to move to a Matrix or Quaternion class, which will make your life much easier in future.
VitaliBR
VitaliBR
I appreciate the help,
I managed to resolve the problem by removing the following piece of code

	//now compute the new UpVector (by cross product)	UpVector = CrossProduct(&ViewDir, ⇀)*-1;


Function before
void CCamera::RotateX (GLfloat Angle){	RotatedX += Angle;	//Rotate viewdir around the right vector:	ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)					+ UpVector*sin(Angle*PIdiv180));	//now compute the new UpVector (by cross product)	UpVector = CrossProduct(&ViewDir, ⇀)*-1;}


Function after
void CCamera::RotateX (GLfloat Angle){	RotatedX += Angle;	//Rotate viewdir around the right vector:	ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)				+ UpVector*sin(Angle*PIdiv180));}
http://mateusvitali.wordpress.com/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.