Jump to content
  • Advertisement
Sign in to follow this  
divined

3ds fps camera

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

Hello everybody I`ve created this camera class which should resemble an fps style camera. I want the character to be able to view up-down (pitch) and rotate around the y-axis (roll) for start. This is the code i`m using
  void CCamera::MoveCamera(float amount)
{
   CVector3 dir = m_view - m_pos;	// direction of movement
   dir.Normal();					// normalize direction vector

   // Move the camera.
	m_pos += dir * amount;

   // Move the view along with the position.
    m_view += dir * amount;
}

void CCamera::RotateCamera(float angle, float X, float Y, float Z)
{
   float ca = (float)cos(angle);
   float sa = (float)sin(angle);

	// Get direction.
	CVector3 dir = m_view - m_pos;
	dir.Normal();

   CVector3 dir2;

	// Calculate the new X position.
	dir2.x = (ca + (1 - ca) * X) * dir.x;
	dir2.x += ((1 - ca) * X * Y - Z * sa)* dir.y;
	dir2.x += ((1 - ca) * X * Z + Y * sa) * dir.z;

	// Calculate the new Y position.
	dir2.y = ((1 - ca) * X * Y + Z * sa) * dir.x;
	dir2.y += (ca + (1 - ca) * Y) * dir.y;
	dir2.y += ((1 - ca) * Y * Z - X * sa) * dir.z;

	// Calculate the new Z position.
	dir2.z = ((1 - ca) * X * Z - Y * sa) * dir.x;
	dir2.z += ((1 - ca) * Y * Z + X * sa) * dir.y;
	dir2.z += (ca + (1 - ca) * Z) * dir.z;


	// Apply to view.
	m_view = m_pos + dir2;
}

I use the RotateCamera in my game loop to make the character look up-down according to the mouse movement. Like this...
curPoint = g_InputSystem->GetMousePos();
   int xDelta = curPoint.x - oldPoint.x;
   int yDelta = curPoint.y - oldPoint.y;
   oldPoint = curPoint;

   //g_camera.UpdatePitchYaw(g_InputSystem->GetMousePos());
   if (xDelta != 0) 
	   g_camera.RotateCamera(xDelta * 0.01f, 0, 1, 0); // rotate around y-axis
   if (yDelta != 0)
	   g_camera.RotateCamera(yDelta * 0.01f, 0, 0, 1);	// rotate around z-axis
   //g_camera.Pitch(yDelta * 0.2f);
the problem is that after some rotating the when I move forward-backward the camera starts flying around or below the ground. I want it to stay on the ground and just be able to look at the sky or below. Is my code totally off? thx, in advance George

Share this post


Link to post
Share on other sites
Advertisement
CVector3 dir = m_view - m_pos;
dir.Normal();
m_pos += dir * amount;

I think, here:

CVector3 dir2 = CVector3(dir.x, 0, dir.z);
m_pos += dir2 * amount;

or after "m_pos += dir * amount;"

m_pos.y = 0; //or s.e.

Because in the "dir" vector3, the 'y' value isn't 0, so your camera will go up/down.
But this is just one idea.

Share this post


Link to post
Share on other sites
I need one more thing. I want to implement a strafe method. In my camera class I store the following vectors


CVector3 m_pos;
CVector3 m_view;
CVector3 m_up;


Now in order or strafe I need to calculate the m_right vector. I know that the cross product of two vectors gives me a perpendicular one which is just what I need. I tried using the cross product of m_pos and m_view but it doesn`t work very well. Any ideas as to how I can calculate the m_right vector?

Share this post


Link to post
Share on other sites
You need to use the cross product of your view and up vectors, not your position vector. Depending on the order of parameters passed too your cross product function you will get either a right or a left vector. The cross oduct of yz =x,
so the cross product of your up and view vectors will give you a vector pointing into your x+ direction.

Share this post


Link to post
Share on other sites
so that means that I must also update my m_up vector every time I transform my view vector correspondingly. Will try that. thx, for the idea!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!