Sign in to follow this  
Thomas Mathers

Camera coding problems!

Recommended Posts

Hello All, I am having trouble with camera system I just implemented into my engine. Right now I am testing out my third-person camera code and I am getting some strange results. My camera is able to follow an object on a specific angle up to the origin. Once the camera passes the origin my camera rotates so that it is facing the opposite direction of the object that it is following. Here is a sample of the code that I am using for calculating the position and orientation of my third-person camera based on an object its following:
void CFNGECamera::Update()
{
	switch (m_Type)
	{
	case TYPE_FIXED: 
		break;
	case TYPE_FREE:
		break;
	case TYPE_FOLLOWING: 
		if (m_Target.m_vecPosition == NULL || m_Target.m_vecRotation == NULL)
			return;

		m_vecPos.x = m_Target.m_vecPosition->x + m_fOffset * sin(m_Target.m_vecRotation->y);
		m_vecPos.y = m_fHeight; 
		m_vecPos.z = m_Target.m_vecPosition->z + m_fOffset * cos(m_Target.m_vecRotation->y);
		
		m_vecDir.x = atan(m_fHeight/m_fOffset);
		m_vecDir.y = m_Target.m_vecRotation->y;
		m_vecDir.z = 0.0f;
		break;
	case TYPE_CINEMATIC:
		break;
	}
	CalcViewMatrix();
}

Share this post


Link to post
Share on other sites
I don't understand what is strange about the results. They sound like what should happen. Your camera passes the object's origin, so the object is behind it. That means it must do a 180 to see the player again.

One way you could accomplish this is to make the camera face the character before moving it. If the camera is "stuck" to the player (like it always moves exactly with him), you could then place the camera on the player's exact location, then simply back it up using it's forward vector multiplied with whatever specific negative distance.

Let me know if you need details.

Share this post


Link to post
Share on other sites
Oh, my mistake. You need to subtract the object's position from the camera, rotate it, then add the object's position back to it.

That should do it, if I understand the problem correctly.

edit: Actually I think more detail is needed. You're rotating both the camera's positon and direction. Which one is not working? The position code actually looks fine, so I doubt my advice above will help.

edit2: Also, you are setting direction-axis z to zero. Why is that? Anyway, I think you should at least normalize that direction.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this