Advertisement

Moving a character with the camera...

Started by March 08, 2003 01:04 PM
2 comments, last by Eric_003 21 years, 11 months ago
What im trying to do is make the camera and the character(cube for now) move and rotate at the same time. I can get my character to move with the arrow keys and look around with the mouse, but the cube I cant get to work. I use GLuLookAt() to move the camera around. I also made a strcture to hold the character data (xPos,yPos,zPos,xRot,yRot,ZRot). I need to keep track of this info so that evenually i can send it to a server that will keep track of all character. So all i need help on is making the camera and character move in sync(for now a couple units infront of the camera for testing). Well anyone who can help me that would be awesome. Draw the scene...
      
int DrawGLScene(GLvoid)									
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glLoadIdentity();									

	// Tell OpenGL where to point the camera

	camera.Look();

	GLfloat x_m, y_m, z_m, u_m, v_m;
	int numtriangles;

	numtriangles = sector1.numtriangles;
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	// Process Each Triangle

	for (int loop_m = 0; loop_m < numtriangles; loop_m++)
	{
		glBindTexture(GL_TEXTURE_2D, texture[sector1.triangle[loop_m].t]);
		glBegin(GL_TRIANGLES);
			glNormal3f( 0.0f, 0.0f, 1.0f);
			x_m = sector1.triangle[loop_m].vertex[0].x;
			y_m = sector1.triangle[loop_m].vertex[0].y;
			z_m = sector1.triangle[loop_m].vertex[0].z;
			u_m = sector1.triangle[loop_m].vertex[0].u;
			v_m = sector1.triangle[loop_m].vertex[0].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
			
			x_m = sector1.triangle[loop_m].vertex[1].x;
			y_m = sector1.triangle[loop_m].vertex[1].y;
			z_m = sector1.triangle[loop_m].vertex[1].z;
			u_m = sector1.triangle[loop_m].vertex[1].u;
			v_m = sector1.triangle[loop_m].vertex[1].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
			
			x_m = sector1.triangle[loop_m].vertex[2].x;
			y_m = sector1.triangle[loop_m].vertex[2].y;
			z_m = sector1.triangle[loop_m].vertex[2].z;
			u_m = sector1.triangle[loop_m].vertex[2].u;
			v_m = sector1.triangle[loop_m].vertex[2].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
		glEnd();
	}
	glPopMatrix();
		
		glTranslatef(char1.xPos, char1.yPos, char1.zPos);

		glRotatef(char1.xRot, 1.0f, 0.0f, 0.0f);
		glRotatef(char1.yRot , 0.0f, 1.0f, 0.0f);
		glRotatef(char1.zRot, 0.0f, 0.0f, 1.0f);
		
		glBindTexture(GL_TEXTURE_2D, texture[3]);		
		glBegin(GL_QUADS);

			//front of character

			glTexCoord2f(0.0f, 1.0f);	glVertex3f(-0.3f, 1.0f, -1.0f);
			glTexCoord2f(1.0f, 1.0f);	glVertex3f(+0.3f, 1.0f, -1.0f);
			glTexCoord2f(1.0f, 0.0f);	glVertex3f(+0.3f, 0.0f, -1.0f);
			glTexCoord2f(0.0f, 0.0f);	glVertex3f(-0.3f, 0.0f, -1.0f);
		glEnd();

		glBindTexture(GL_TEXTURE_2D, texture[4]);		
		glBegin(GL_QUADS);

			//back of character

			glTexCoord2f(0.0f, 1.0f);	glVertex3f(-0.3f, 1.0f, -0.7f);
			glTexCoord2f(1.0f, 1.0f);	glVertex3f(+0.3f, 1.0f, -0.7f);
			glTexCoord2f(1.0f, 0.0f);	glVertex3f(+0.3f, 0.0f, -0.7f);
			glTexCoord2f(0.0f, 0.0f);	glVertex3f(-0.3f, 0.0f, -0.7f);

			//left of character

			glTexCoord2f(0.0f, 1.0f);	glVertex3f(-0.3f, 1.0f, -0.7f);
			glTexCoord2f(1.0f, 1.0f);	glVertex3f(-0.3f, 1.0f, -1.0f);
			glTexCoord2f(1.0f, 0.0f);	glVertex3f(-0.3f, 0.0f, -1.0f);
			glTexCoord2f(0.0f, 0.0f);	glVertex3f(-0.3f, 0.0f, -0.7f);

			//right of character

			glTexCoord2f(0.0f, 1.0f);	glVertex3f(+0.3f, 1.0f, -0.7f);
			glTexCoord2f(1.0f, 1.0f);	glVertex3f(+0.3f, 1.0f, -1.0f);
			glTexCoord2f(1.0f, 0.0f);	glVertex3f(+0.3f, 0.0f, -1.0f);
			glTexCoord2f(0.0f, 0.0f);	glVertex3f(+0.3f, 0.0f, -0.7f);

			glEnd();

		glColor3f(1.0f, 1.0f, 1.0f);

	return TRUE;										
}
  
Check for arrows keys being presses...
  
void CCamera::CheckForMovement()
{

	float speed = kSpeed * frameInterval;

	// Check if we hit the Up arrow or the 'w' key

	if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) {				

		// Move our camera forward by a positive SPEED

		MoveCamera(speed);
		char1.zPos -=speed;
	}

	// Check if we hit the Down arrow or the 's' key

	if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) {			

		// Move our camera backward by a negative SPEED

		MoveCamera(-speed);
		char1.zPos +=speed;
	}

	// Check if we hit the Left arrow or the 'a' key

	if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) {			

		// Strafe the camera left

		StrafeCamera(-speed);
		char1.xPos-=speed;
	}

	// Check if we hit the Right arrow or the 'd' key

	if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) {			

		// Strafe the camera right

		StrafeCamera(speed);
		char1.xPos+=speed;
	}	
}
  
Set the view based on mouse movement...
  
void CCamera::SetViewByMouse()
{
	POINT mousePos;									// This is a window structure that holds an X and Y

	int middleX = SCREEN_WIDTH  >> 1;				// This is a binary shift to get half the width

	int middleY = SCREEN_HEIGHT >> 1;				// This is a binary shift to get half the height

	float angleY = 0.0f;							// This is the direction for looking up or down

	float angleZ = 0.0f;							// This will be the value we need to rotate around the Y axis (Left and Right)

	static float currentRotX = 0.0f;
	
	// Get the mouse's current X,Y position

	GetCursorPos(&mousePos);						
	
	// If our cursor is still in the middle, we never moved... so don't update the screen

	if( (mousePos.x == middleX) && (mousePos.y == middleY) ) return;

	// Set the mouse position to the middle of our window

	SetCursorPos(middleX, middleY);							

	// Get the direction the mouse moved in, but bring the number down to a reasonable amount

	angleY = (float)( (middleX - mousePos.x) ) / 1000.0f;		
	angleZ = (float)( (middleY - mousePos.y) ) / 1000.0f;		

	currentRotX -= angleZ;  

	// If the current rotation (in radians) is greater than 1.0, we want to cap it.

	if(currentRotX > 0.5f)
		currentRotX = 0.5f;

	else if(currentRotX < -0.5f)
		currentRotX = -0.5f;

	else
	{

		CVector3 vAxis = Cross(m_vView - m_vPosition, m_vUpVector);
		vAxis = Normalize(vAxis);

		// Rotate around our perpendicular axis and along the y-axis

		RotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);
		RotateView(angleY, 0, 1, 0);
	}
}
  
If you need to see the exe or any other part of my source code to help let me know either replying to this message or sending me an email at E_Williams@Rogers.com [edited by - Eric_003 on March 8, 2003 2:14:09 PM]
Look for articles about 3rd. person camera. Most of them use polar coordinates.
Advertisement
Thanks, I will see what i can find on polar coordinates
look on gametutorials.com they have an article about a 3rd person camera and how to use it.

Dustin Davis
Owner / CEO
Programmers Unlimited
www.Programmers-Unlimited.com
Dustin DavisOwner / CEOProgrammers Unlimitedwww.Programmers-Unlimited.com

This topic is closed to new replies.

Advertisement