Jump to content
  • Advertisement
Sign in to follow this  
clearz

FPS shooter movement anomolies

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

Hi I am new to direct3D and have been working on a very small small 3D engine. It is a FPS style(mouse looks arround) W,A,S,D controls movement and Spacebar to jump. I have got this working OK using a camera class that I put together.
void Camera::RotateX(float angle)
{
	if(up.y > 0 )
	{
		look = rotateAroundAxis(look, right, angle);
		CrossProduct(look, right, up); // Up
	}
	else
	{
		look = rotateAroundAxis(look, right, .01);
		CrossProduct(look, right, up); // Up
	}
}

void Camera::RotateY(float angle)
{
	float xxx, zzz;
	float sinAng, cosAng;
	
	// Get sin()/cos() of angle
	sinAng = sinf(angle);
	cosAng = cosf(angle);
	
	// Save off look components for computation
	xxx = look.x;
	zzz = look.z;
	
	// Rotate look vector
	look.x = xxx * cosAng + zzz * sinAng;
	look.z = xxx * -sinAng + zzz * cosAng;
	
	// Save off up components for computation
	xxx = up.x;
	zzz = up.z;
	
	// Rotate up vector
	up.x = xxx * cosAng + zzz * sinAng;
	up.z = xxx * -sinAng + zzz * cosAng;
	
	// Save off right components for computation
	xxx = right.x;
	zzz = right.z;
	
	// Rotate right vector
	right.x = xxx * cosAng + zzz * sinAng;
	right.z = xxx * -sinAng + zzz * cosAng;
}


void Camera::Move(float amt)
{
	amt = amt * jumpMoveSpeed;
	pos += D3DXVECTOR3(look.x, 0, look.z ) * amt;
}

void Camera::Strafe(float amt)
{
	amt = amt * jumpMoveSpeed;
	pos += D3DXVECTOR3(right.x, 0, right.z ) * amt;
}

void Camera::Jump()
{
	if(jumping)
	{
		jumpMoveSpeed = 2;
		velY = velY - accel;
		pos += D3DXVECTOR3(0, up.y, 0) * velY;
		if(pos.y <= 0)
		{
			jumpMoveSpeed = 1;
			velY = 5.0f;
			accel = .015f;
			pos.y =	 0;
			jumping = false;
		}
	}
}
However there are two problems with this code that I have been trying to fix and im sure a lot of you have come across before. 1) If I rotate to the ground or to the sky the camera flips arround into some kind of reverse controls. This is especially noticable if I have jumped where gravity will reverse and I will fall away from the ground. If you look in the RotateX function you will see I have been trying to stop this using an if statement but this is very jerky. 2) The more I face the ground or the sky the slower I can move forwards and backwards until I stop when I am directly facing the ground or sky. This is also noticable when I jump. If I look at the grouns or sky I will stop falling. If sombody could help me out with these problems I would bee most gratefull. thanks John.

Share this post


Link to post
Share on other sites
Advertisement
The reverse camera is from the camera's up vector pointing in the opposite direction (towards the ground) rotated enough on x. you have to keep the camera's up vector from going further than paralell to the ground plane. The second problem seems to me that you are trying to move along the look vector, in your move code you constrain the camera motion to x and z, in which case when the look vector is more at y there is no or less x and z component of the vector and therefore less motion in those directions, untill when 0,1,0 is hit, there is no motion in x or z. In order to work around this problem, you have to make sure that the motion function moves the camera the same amount in the x and z directions regardless of the actual look vector (as you seem to have it set up). I hope this helps.

Share this post


Link to post
Share on other sites
Thanks for the reply. I understand what is going on but I cant get my head around the solution. As I move the mouse forward I am rotating the z axis until I am looking at the ground. But how do I solve this.

Share this post


Link to post
Share on other sites
You need to code in some constraints to the camera's up vector. Make sure it is always positive in y to take care of the flip effect, which can be done with a simple if statement. As to the motion slowing, it also has to do with your up vector calculation. The only thing running when you look up or down I am assuming is

void Camera::RotateX(float angle)

and in this you are saying up.y >0, when up.y should always be 1 when doing this kind of calculation because the camera is the only thing that needs the up vector, when doing jump and rotation calculations the vector should always be {0,1,0} which is directly up from the ground.

Again, hope this helps.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!