• ### Popular Now

• 12
• 12
• 9
• 10
• 13

#### Archived

This topic is now archived and is closed to further replies.

# Free moving camera (first person type)

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

## Recommended Posts

Hello, sorry for being noobish (ive been using opengl for awile, but im trying to learn new concepts and do different things, so many areas im learning in im just getting my feet wet for the first time) Right now, im trying to get away from using glRotatef() and glTranslatef(), they work great for a few things, but just seem redundant and annoying after awile, and i cant get the features i want from them. Ive been playing around with gluLookAt() and im getting some cool results, but one thing id like to do specifically is make a sort of first person free-flying camera to implement into my engine so that any future demos i create i can simply "fly" around the scene and look anywhere i want, i read a few tutorials that seemed to have been written by professors and i simply didnt get the break down of the matrix calculations and such, i was hoping one of you could help me do this, below is a rough sketch of how i was thinking it would work: (sorry if the image doesnt work, i host these myself and my ISP sucks) Thanks in advance for any help~ EDIT: i sat down for awile and tryed to figure a solution but no luck, im looking for a math equation that i can somehow feed in data, and figure the point on the perimeter of the circle, this equation should work both in 2d and 3d, i just need to make sure as the camera moves, it arcs properly on the outside of the circle and stays the distance of the radius O_o "If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside." [edited by - SilverLogic on June 10, 2004 7:00:34 AM] #### Share this post ##### Link to post ##### Share on other sites Advertisement YES i figured it out!!! Theres some wonderful information at http://www.math.com/tables/geometry/circles.htm (if your not already good with geometry). I just studied the bottom part about the perimeter of a circle for awile and got a working demo. basically you specify the origin (which can be the players position in the map, and with a given radius, the camera will always be an exact distance from the player no matter which direction you are facing. With a few modifications this code can be used with gluLookAt(). Use the players coords for the eye[], and then use the equations camera position (in my example, the end of the line), and use that for the look[] portion. Hope someone finds this useful p.s. i used SDL, but the important bits of the code are still easily made out in c++ and opengl #include <sdl\sdl.h> #include <sdl\sdl_opengl.h> #include <gl\gl.h> #include <gl\glu.h> #include <stdio.h> #include <stdlib.h> #include <math.h> double PI = 3.141592; SDL_Surface *screen; SDL_Event hEvent; Uint8 *keyarray; bool done=false; float CameraPos = 0.0f; float x, y, z; float Radius = 200.0f; void RenderScene() { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef( (float)(screen->w >> 1) , (float)(screen->h >> 1) , 0.0); z = 0.0f; glPointSize(5); glBegin(GL_POINTS); for(float angle = 0.0f; angle<=PI; angle+=0.1f) { // top half of circle x = Radius*sin(angle); y = Radius*cos(angle); glVertex3f(x, y, z); // bottom half of circle x = Radius*sin(angle+PI); y = Radius*cos(angle+PI); glVertex3f(x, y, z); } glEnd(); // determine camera position from origin based on the radius glBegin(GL_LINES); glVertex3f(0, 0, z); // origin glVertex3f( (Radius*sin(CameraPos/PI)), (Radius*cos(CameraPos/PI)), z); // current camera position on the perimeter of the circle glEnd(); glPopMatrix(); SDL_GL_SwapBuffers(); } void HandleEvents() { SDL_PollEvent(&hEvent); keyarray = SDL_GetKeyState(NULL); if(hEvent.type==SDL_QUIT) done=true; if(keyarray[SDLK_ESCAPE]==1) done=true; if(keyarray[SDLK_LEFT]==1) { CameraPos+=0.2f; if(CameraPos>360.0f) CameraPos=0.0f; } if(keyarray[SDLK_RIGHT]==1) { CameraPos-=0.2f; if(CameraPos<0.0f) CameraPos=360.0f; } } int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); screen = SDL_SetVideoMode(800, 600, 0, SDL_OPENGL); glViewport(0, 0, screen->w, screen->h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, screen->w, screen->h, 0, -1.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); while(!done) { RenderScene(); HandleEvents(); SDL_Delay(10); } SDL_Quit(); return 0; } EDIT: for those of you who dont know, the portion of code "screen->w >> 1", this is just a faster binary way to divide a number in half --------------------------------------------- "If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost$100, get a million miles per gallon, and explode once a year, killing everyone inside."

[edited by - SilverLogic on June 10, 2004 8:19:43 AM]