• Advertisement
Sign in to follow this  

3d person camera woes

This topic is 2889 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

Hi there, Some of you might have noticed that I deleted my post I created a couple of minutes ago. I've done this because I found the answer myself and the error I made was epic in proportion :D. So I thought it would be in the best interest of everybody to delete it :p. Now however, I believe I have a genuine problem I can't figure out. What I want to do is create a third person camera, a camera that will be able to change direction in the Y (left - right) position and change viewing angle in the X (up - down) position. Of course if you change directing in the Y position there should be a new "Z vector" in the direction you are looking at to move around. Now all except changing the Z direction is working fine. So what I would like to know is how I can make it so that my model also moves in the direction I'm looking? My code so far:
int main(int argc, char** argv) {
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	
	//Gamemode
	
	glutGameModeString("1920x1080:32@60");
	glutEnterGameMode();
	
	// Debug mode
	/*
	glutInitWindowSize(500,500);
	glutInitWindowPosition((Fullscreenw/2)-250,(Fullscreenh/2)-250);
	glutCreateWindow("Artesis project");
	*/

	// glut function calls
	initRendering();
	glutDisplayFunc(drawScene);
	glutTimerFunc(1,updateMouse,0);
	glutReshapeFunc(handleResize);
	glutKeyboardFunc(keyboardFunc);
	glutMainLoop();

	return 0;
}

void drawScene(){
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	// Print fps on screen (Loadidentity and changing to modelview is also done in these function calls)
	if(showFps==true){
		glEnable2D();
		glColor3f(0,255,0);
		output(10,10,strFrameRate);
		glDisable2D();
	}

	// Setting distance from model
	glTranslatef(0,0,-3.0f);

	// 3d person look up/down (without change the Z direction
	glRotatef(yrot,1.0f,0.0f,0.0f);

	//temp Model
	glColor3f(1.0f, 0.0f, 0.0f);
	glutSolidCube(1);

	// 3d person turn left right  (changing the Z direction)
	glRotatef(xrot,0.0f,1.0f,0.0f);
	glTranslated(xpos,0.0f,zpos);

	//draw temp world
	glPushMatrix();
	drawbox();
	glPopMatrix();
	
	//Send information to screen
	glutSwapBuffers();

	//calculate fps
	fps();
}

void updateMouse(int value) {
		POINT ptCursorPos;
		GLfloat centerX=glutGet(GLUT_WINDOW_WIDTH)/2;
		GLfloat centerY=glutGet(GLUT_WINDOW_HEIGHT)/2;

		//Getting cursor position under windows
		GetCursorPos(&ptCursorPos);
		
		// Variables
		float x=ptCursorPos.x;
		float y=ptCursorPos.y;
		float diffx=x-centerX;
		float diffy=y-centerY;
		
		//Changing X rotation (left right) and also making sure it's between -360 and 360 degrees
		xrot+=diffx*0.2;
		if(xrot>360){
			xrot=xrot-360;
		}
		if(xrot<-360){
			xrot=xrot+360;
		}
		//Chaning Y rotation (up down) and making sure you can't go beyond +90 and below 0 degrees
		if(yrot+diffy>90){
			yrot=90;
		}else if(yrot+diffy<0){
			yrot=0;
		}
		else{
			yrot+=diffy*0.2;
		}

		// warping point back to center
		glutWarpPointer(centerX,centerY);
		
		//Redisplay and reenvoke this function
		glutPostRedisplay();
	    glutTimerFunc(1, updateMouse, 0);

}
Thanks in advance Best regards

Share this post


Link to post
Share on other sites
Advertisement
What do you mean by this:
"Now all except changing the Z direction is working fine."

Also, is your camera rotating about the Y axis (in horizontal direction) ?

And once your camera is rotated, is your player not moving along the rotated direction ? Is that your question ?

Share this post


Link to post
Share on other sites
Quote:
Original post by brainydexter
What do you mean by this:
"Now all except changing the Z direction is working fine."

Also, is your camera rotating about the Y axis (in horizontal direction) ?

And once your camera is rotated, is your player not moving along the rotated direction ? Is that your question ?


Hi there, thanks for answering but I found the problem already. The problem I was having was that my model (a simple cube atm) doesn't move in the direction I was looking at when pressing the w button.

But I fixed it by doing this when pressing the w button:

...
if(key=='w'){
float xrotrad, yrotrad;
xrotrad = (yrot / 180 * 3.141592654f);
yrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(-yrotrad))*speed;
zpos += float(cos(-yrotrad))*speed;
ypos -= float(sin(xrotrad));
glutPostRedisplay();
}
...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement