Archived

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

CBoardianDavidP

3rd person view in my project

Recommended Posts

I have been working on a project in OpenGL for quite a bit of time now. So far it is the current view is a first person 3d walkaround, however, I need to change it to 3rd person. There is a mech in the corner of the map that I have been working on animating for a long time now, and now that I am done with that, it is time to let the user actually control the mech, and so i need to change it from a 1st person view where you walk around the map and am able to expect everything freely, to a 3rd person view where you control the mech and walk the mech around the map. The idea is for the camera to be angled just slightly above and behind the mech. I know it is just a matter of doing translations and rotations correctly, but I am not exactly sure what translations and rotations need to be made, and in what order, in order to get the camera in the right spot and keep it in the right spot. Here is all relevant code:
//some global vars

float heading;
float xpos;
float zpos;
float ypos;
GLfloat	yrot;
float movy = -1;

//This is the event loop.

	while(1)
	{
		draw_screen ( );
		
		
		while( SDL_PollEvent ( &event ) )
		{
			switch(event.type)
			{
				case SDL_KEYDOWN:				

					switch(event.key.keysym.sym)
					{
					case SDLK_DOWN:	
						xpos += (float)sin(heading*piover180) * 0.50f;
						zpos += (float)cos(heading*piover180) * 0.50f;	
						break;
					case SDLK_UP:						
						xpos -= (float)sin(heading*piover180) * 0.50f;		

	
						zpos -= (float)cos(heading*piover180) * 0.50f;	
						break;
					case SDLK_LEFT:							
						heading += 3.75f;	
						yrot = heading;	
						break;
					case SDLK_RIGHT:					
						heading -= 3.75f;
						yrot = heading;
						break;
					case SDLK_PAGEUP:		
						movy -= 0.1f;						

	
						break;
					case SDLK_PAGEDOWN:
						movy += 0.1f;
						break;
					case SDLK_ESCAPE:
						alDeleteBuffers(1, &SoundBuffer);
						alDeleteSources(1, &Source);
						alutExit();
						bah.close();	
						return 0;	
						break;					
					}
				break;
				
			}
		}


//my draw function

static void draw_screen( void )
{   	
	GLfloat xtrans = -xpos;
	GLfloat ztrans = -zpos;		
	movy -= ypos;
	GLfloat sceneroty = 360.0f - yrot;

    /* Clear the color and depth buffers. */
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    /* We don''t want to modify the projection matrix. */
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );

	glRotatef(sceneroty,0,1.0f,0);
	glTranslatef(xtrans, movy, ztrans);
	

	myMap->DrawMap();	

	//prints any text on the screen i want to be printed
	CyclePrintBuffer ( );


	myMech.RunWalkSequence ( 5 );
	myMech.Model->RotateModel ( 270, X );		
	myMech.DrawMech ( );


    /*
     * Swap the buffers. This this tells the driver to
     * render the next frame from the contents of the
     * back-buffer, and to set all rendering operations
     * to occur on what was the front-buffer.
     *
     * Double buffering prevents nasty visual tearing
     * from the application drawing on areas of the
     * screen that are being updated at the same time.
     */

	

    SDL_GL_SwapBuffers( );

	movy += ypos;	
}

//my function to set up opengl

void setup_opengl( int width, int height )
{
    float ratio = (float) width / (float) height;

	//Texture Mapping Initialization	
	InitTextureNames ( );
	InitModelNames ( );
	LoadGLTextures();		// Texture Loading Routine
	BuildFont();
	glEnable(GL_TEXTURE_2D);// Enable Texture Mapping
	

    /* Our shading model--Gouraud (smooth). */
    glShadeModel( GL_SMOOTH );

    /* Set the clear color. */
    glClearColor( 0, 0, 0, 0 );

    /* Setup our viewport. */
    glViewport( 0, 0, width, height );

    /*
     * Change to the projection matrix and set
     * our viewing volume.
     */
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );

    gluPerspective( 60.0, ratio, 1.0, 1024.0 );

	myMap = new HEXMAP_DTP( 15, 17, 3.0f );
	myMap->SetBase ( 0.0f, -1.0f, 0.0f );
	myMap->LoadHeightMap ( "heightmap.dat" );
	myMap->HeightMap( );
	myMap->ApplyDefaultTerrainColors();
	myMap->EnableTextureMapping ( true );
	myMap->CreateHexDisplayLists ( );

	glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
}
[/CODE]


Thanks for any help.
   

Share this post


Link to post
Share on other sites
smitty1276    560
Sorry, I''m about to fall asleep or I''d be more verbose...

Use sin/cos to determine the x and z coordinates for the camera based on the angle of rotation (assuming you want the camera to "orbit" the mech)... then you can use the same angle to rotate the camera back towards the mech.

Share this post


Link to post
Share on other sites
jonbell    100
Easier way : take your look / direction vector for your cameras target, negate it, normalize it, mult by the distance you want from the target then finally add the Y value to move it up as much as you want. Job Done.

Share this post


Link to post
Share on other sites