Sign in to follow this  

Directions and Vectors problem

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

I'm trying to make FPS-style navigation, and i'm using the following code:
UpdatePlayer()
{
	vec3 x_axis(1,0,0);
	vec3 y_axis(0,1,0);
	vec3 z_axis(0,0,1);
	vec3 angles;
	clamp(Player.Dir.x,-1,1);
	clamp(Player.Dir.y,-1,1);
	clamp(Player.Dir.z,-1,1);
	angles.x = Rad2Deg(acos(Player.Dir % x_axis));
	angles.y = Rad2Deg(acos(Player.Dir % y_axis));
	angles.z = Rad2Deg(acos(Player.Dir % z_axis));
	if(Player.State[MOVELEFT])
	{
		angles.x += Player.DPS * GameInterval;
		while(angles.x < 0)
		{
			angles.x += 360;
		}
		while(angles.x > 360)
		{
			angles.x -= 360;
		}
	}
	if(Player.State[MOVERIGHT])
	{
		angles.x -= Player.DPS * GameInterval;
		while(angles.x < 0)
		{
			angles.x += 360;
		}
		while(angles.x > 360)
		{
			angles.x -= 360;
		}
	}
	Player.Dir.x = cos(Deg2Rad(angles.x));
	Player.Dir.y = cos(Deg2Rad(angles.y));
	Player.Dir.z = cos(Deg2Rad(angles.z));
	Player.Dir.Normalize();
	Player.Pos = Player.Pos + ((Player.Dir * Player.Speed) * GameInterval);
}

every frame after updating the player, i place the camera at Player.Pos and make it look at Player.Pos + Player.Dir the problem is that if i keep turning left or right, i cannot make a 90 degree turn. i reach a maximum of 80 degrees and it stops. besides, from around 65 degrees and above, the turning speed gets slower as the angle gets bigger... can someone plz tell me what's the problem?

Share this post


Link to post
Share on other sites
Maybe insted of clamping your Player.Dir vector, you should try to normalize it. It does the same thing, and keeps the vector direction true. Thats the only thing I can see might be wrong.

Share this post


Link to post
Share on other sites
My apology, but from the code above it seems that you are missing the concept of so-called "navigation".

You cannot get (x,y,z) directly from (angx,angy,angz), one by one. They affect each other.
If you are willing to perform full-3D, entire-arbitrary look direction, then you will have to go with quaternions. Try digging within this site's articles.

But if you are pointing into something like quake's navig, then it's a whole another story.

What you've got to keep is then:
angleXZ [0, 2*pi] - your angle with X axis, on XZ plane, that is, when looking from above.
angleY [-pi, pi] - your angle with XZ plane.

You can of course go with 180 insted of pi.

Your looking dir would then be calculated like:
dir.x = cos(angleXZ) * cos(angleY);
dir.z = sin(angleXZ) * cos(angleY);
dir.y = sin(angleY);

Also, if you are using gl, your dir.z should be negated.


Hope that was helpful.
/def

Share this post


Link to post
Share on other sites

This topic is 4717 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.

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