Jump to content
  • Advertisement


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


3rd person view in my project

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

If you intended to correct an error in the post then please contact us.

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.

		draw_screen ( );
		while( SDL_PollEvent ( &event ) )
				case SDL_KEYDOWN:				

					case SDLK_DOWN:	
						xpos += (float)sin(heading*piover180) * 0.50f;
						zpos += (float)cos(heading*piover180) * 0.50f;	
					case SDLK_UP:						
						xpos -= (float)sin(heading*piover180) * 0.50f;		

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

					case SDLK_PAGEDOWN:
						movy += 0.1f;
					case SDLK_ESCAPE:
						alDeleteBuffers(1, &SoundBuffer);
						alDeleteSources(1, &Source);
						return 0;	

//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. */

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

	glTranslatef(xtrans, movy, ztrans);


	//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
	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->EnableTextureMapping ( true );
	myMap->CreateHexDisplayLists ( );


Thanks for any help.

Share this post

Link to post
Share on other sites
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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!