Jump to content

  • Log In with Google      Sign In   
  • Create Account


A First Person Camera Class in OpenGL C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Obi-Dan   Members   -  Reputation: 109

Like
0Likes
Like

Posted 05 March 2013 - 07:09 AM

I'm trying to create a first person style camera.
So far I have moving along the x and y axes sorted and now need to work on changing the pitch of the camera.

E.g. To move forward 2.5f units:

 

mainCamera->moveForward(2.5f);

Camera is derived from MovableObject:

void MovableObject::moveForward(float distance)
{

	m_position.z += cos(m_rotation.y) * distance;
	m_position.x -= sin(m_rotation.y) * distance;
}

 

 

m_position - position vector

m_rotation - rotation vector (in radians)

 

The camera update method sorts out the matrix stuff...

 

void Camera::Update()
{
	GUMatrix4 finalMat, translation, rotation, scale;
	
	GUVector3 rot = getRotation();

	// Create matricies
	translation = GUMatrix4::translationMatrix(getX(), getY(), getZ());
	rotation = GUMatrix4::rotationMatrix(rot.x, rot.y, rot.z);

	glMatrixMode(GL_PROJECTION);

	finalMat = getProjMat() * rotation * translation;

	glLoadMatrixf((GLfloat*) &finalMat);	

}

 

translation - the 4x4 translation matrix.

rotation- the 4x4 rotation matrix.

 

The code above works beautifully for moving forwards on the x and z axes. At the moment the camera is rotated about the Y axis using the mouses horizontal movement. 

 

void mouseMove(int x, int y) {

	if (mDown) {

		int dx = x - mouse_x;
		int dy = y - mouse_y;

		// rotate camera 
		if (mainCamera)
		{
			
			mainCamera->setYaw(mainCamera->getYaw() + (float)-dx);
				
		}
		
		mouse_x = x;
		mouse_y = y;
	}
}

 

I now want the camera to rotate about the x and z axes using the mouses vertical position. The camera then needs to move forwards with the Y axis included.

 

So far for changing the cameras pitch I have:


void MovableObject::changePitch(float degrees)
{
        // Convert change to radians
	float rads = MathHelp::degreesToRadians(degrees);
	
        // X rotate more when looking down Z axis
	m_rotation.x += cos(m_rotation.y) * rads;
}

 

 

 

Hopefully this is making sense to somebody. 

 

Thanks in advance!

Dan.

 

 



Sponsor:

#2 Vincent_M   Members   -  Reputation: 580

Like
1Likes
Like

Posted 05 March 2013 - 01:14 PM

So, you'd like the literally have the camera move in the direction the camera is facing, even along the Y-axis? For example, if you're looking 45 degrees up, you'd like the camera to move up the Y-axis?

 

If this is the case, then you would want to add this line to your MovableObject::moveForward() method:

m_position.y += sin(m_rotation.x) * distance;

 

Keep in mind, that sin() and cos() give you the normalized components of a vector along the unit circle. Using sin() and cos() with x and z will give you a direction vector with the length of 1 because it's normalized. When you add m_position.y into the mix, you're actually making the direction vector longer than 1 unit (before you scale it by distance).

 

The best thing to do is find your direction vector, normalize, then multiply by distance like so:

void MovableObject::moveForward(float distance)
{
	Vector3 direction;
	direction.x = -sin(m_rotation.y);
	direction.y =  sin(m_rotation.x);
	direction.z =  cos(m_rotation.y);
	direction.Normalize();

	// add the direction, scaled by distance, to the current position
	m_position += direction * distance;
}

 

Depending on your coordinate system, you may want to change direction.x to +cosf() and direction.z to -sinf(). You'd also want to scale your "distance" float by the elapsed time between frames in your update loop if it's not already so it moves smoothly regardless of framerate.



#3 Obi-Dan   Members   -  Reputation: 109

Like
0Likes
Like

Posted 06 March 2013 - 04:51 PM

Brilliant! Thanks, that works perfectly for moving forward. What about the change pitch method?

 

All I need now is to be able to rotate, currently the camera rotates on the Y axis only which lets me move around but not up or down.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS