Camera coding problems!

Started by
2 comments, last by Jiia 18 years, 10 months ago
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();
}
Advertisement
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.
I never said the camera passes the objects origin, I said when the camera passes the scene origin
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.

This topic is closed to new replies.

Advertisement