combining strafe and walk?

Started by
20 comments, last by cozzie 10 years, 3 months ago

Hi all,

I've noticed that in my camera movement, when the camera/ player both walks forward/backward and strafes, the camera moves twice the amount as it should. At least that's what I think.

Below the code I'm using right now.

I have 2 questions:

- is my assumption correct that with the approach below, moving forward/backward + strafe at the same time, moves the camera/player twice the amount it should

- my approach to fix this, would be to check if the keys for both moving forward/backward and strafe and down, and if so, call a separate/ new function, moving the camera for strafe/ moving at once (if both keys down), if else only one etc.

What do you think?


			if(_dinput.KeyDown(DIK_W)) _player.SetPosition(_d3dcam.MoveForwardBackward(_timer.GetDelta() * _player.GetWalkSpeed()));
			if(_dinput.KeyDown(DIK_S)) _player.SetPosition(_d3dcam.MoveForwardBackward(_timer.GetDelta() * -_player.GetWalkSpeed()));		

			if(_dinput.KeyDown(DIK_A)) _player.SetPosition(_d3dcam.MoveLeftRight(_timer.GetDelta() * -_player.GetStrafeSpeed()));
			if(_dinput.KeyDown(DIK_D)) _player.SetPosition(_d3dcam.MoveLeftRight(_timer.GetDelta() * _player.GetStrafeSpeed()));


// the actual camera functions

/**************************************************************************************/
/***							MOVE FORWARD BACKWARD								***/
/*** ==> usage: when camera moves forward or backward								***/
/*** ==> calculates the changed X and Z camera position, based on movement value	***/
/**************************************************************************************/

VECTOR3 CD3dcam::MoveForwardBackward(const float pAmount)	
{ 
	mPosition += mLook * pAmount;					// 'flying' allowed, 6 DOF
	
	// to disable 'FLYING', use:
//	mPosition += D3DXVECTOR3(mLook.x, 0.0f, mLook.z) * pAmount;

	return VECTOR3(mPosition.x, mPosition.y, mPosition.z);
}

/**************************************************************************************/
/***							MOVE LEFT RIGHT										***/
/*** ==> usage: when camera strafes to the left or right							***/
/*** ==> calculates the changed X and Z camera position, based on movement value	***/
/**************************************************************************************/

VECTOR3 CD3dcam::MoveLeftRight(const float pAmount)
{
	mPosition += mRight * pAmount;

	return VECTOR3(mPosition.x, mPosition.y, mPosition.z);
}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
You're just adding two orthogonal vectors. So you'll see sqrt(walk * walk + strafe * strafe) total movement speed. If walking and strafing speeds are the same, this is sqrt(2) or about 1.4 times faster than normal.

If you want to limit your speed, instead of applying each movement directly to mPosition, make a temporary movement vector first, apply all your player inputs to it, then limit that vector's length just before adding it to mPosition.



movementVector = zero;
 
apply all input to the movementVector;
 
speed = length of movementVector;
 
if (speed > limit) // assuming limit > 0 to prevent divide-by-zero.
  movementVector = movementVector * (limit / speed);
 
mPosition += movementVector;
(edit) Note: You can also move the _timer.GetDelta() so that you only multiply it with movementVector on the final line. This way you won't need to multiply it with every vector, speed, or limit during your calculation.

I suspect you are correct, and your idea to fix it would work.

It's basically that velocity vector generated from strafe + movement is greater than the one generated by either one by itself.

Heh @Nypyren beat me to it, and explained it better =)

Thanks, this is really clear. I'll go implement it right away

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

That was easy, think I've got it.

Here it is, result looks as expected.

I've left the separate move forward/backward, left/right and up/down functions unchanged.

What do you think?


			Crealysm_math::VECTOR3 movement = Crealysm_math::VECTOR3(0.0f, 0.0f, 0.0f);

			if(_dinput.KeyDown(DIK_W)) movement.z =  _player.GetWalkSpeed();
			if(_dinput.KeyDown(DIK_S)) movement.z = -_player.GetWalkSpeed();
			
			if(_dinput.KeyDown(DIK_A)) movement.x = -_player.GetStrafeSpeed();
			if(_dinput.KeyDown(DIK_D)) movement.x = _player.GetStrafeSpeed();

			if(_dinput.KeyDown(DIK_PRIOR)) movement.y = _player.GetWalkSpeed();
			if(_dinput.KeyDown(DIK_NEXT)) movement.y = -_player.GetWalkSpeed();

			movement *= _timer.GetDelta();

			_player.SetPosition(_d3dcam.Move(movement));


VECTOR3 CD3dcam::Move(const Crealysm_math::VECTOR3 pMovement)
{
	MoveForwardBackward(pMovement.z);
	MoveLeftRight(pMovement.x);		
	MoveUpDown(pMovement.y);

	return VECTOR3(mPosition.x, mPosition.y, mPosition.z);
}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

That looks like it will work fine, but you will still have the ability to move faster than normal if you walk+strafe+climb at the same time.

It's totally optional if you want to limit the maximum movement speed or not. If you do, use the idea in my pseudocode just before your "movement *= _timer.GetDelta()" line if you want to limit your combined XYZ speed, or before the PRIOR/NEXT keys if you only want to limit the XZ speed and not the Y speed. For example, a game that allows the player to jump probably would do its speed limiting on the XZ movement and allow jumping/falling to let you move faster than what you can normally walk+strafe.

As a side note: Another thing you can do with your new code is to cancel out opposite movements when the player presses both forward+back, left+right at the same time: If you change each of the "=" to "+=" on each of your input checking lines, the opposite keys will cancel each other out when both buttons are pressed.

Thanks Nypyren, got it.

The movement actually 'feels' smoother now.

Here's the result:


		if(!_d3dscene.GetAnimated())
		{
			_player.Running(_dinput.KeyDown(DIK_LSHIFT));
			Crealysm_math::VECTOR3 movement = Crealysm_math::VECTOR3(0.0f, 0.0f, 0.0f);

			if(_dinput.KeyDown(DIK_W)) movement.z +=  _player.GetWalkSpeed();
			if(_dinput.KeyDown(DIK_S)) movement.z += -_player.GetWalkSpeed();
			
			if(_dinput.KeyDown(DIK_A)) movement.x += -_player.GetStrafeSpeed();
			if(_dinput.KeyDown(DIK_D)) movement.x += _player.GetStrafeSpeed();

			if(_dinput.KeyDown(DIK_PRIOR)) movement.y += _player.GetWalkSpeed();
			if(_dinput.KeyDown(DIK_NEXT)) movement.y += -_player.GetWalkSpeed();

			movement.limit(_player.GetWalkSpeed());
			movement *= _timer.GetDelta();

			_player.SetPosition(_d3dcam.Move(movement));
		}

// NEW VECTOR3 inline function

	VECTOR3 limit(const float pMax)
	{
		if(x > pMax) x = pMax;
		if(y > pMax) y = pMax;
		if(z > pMax) z = pMax;
		return *this;
	}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me



VECTOR3 limit(const float pMax)
{
   if(x > pMax) x = pMax;
   if(y > pMax) y = pMax;
   if(z > pMax) z = pMax;
   return *this;
}

The maximum operation applied on each dimension separately allows the diagonal speed still to be approx. 1.4 times greater than the maximum speed in a orthogonal direction. You have to limit the length of the vector, not its components. This is because the vector defines a velocity, i.e. a speed and a direction of motion, where the speed is its length.

Ok. I might need some help on this. Do you mean I have to:

- calculate the length of the vector (sqrt(x*x + y*y + z*z)) = length

- if length > 'pMax', then make the vector the length of pMax

An example:

Movement vector = 0.5, 0.8, 0.5

Length/ magnitude = 1.067

Then I want to limit the vectors length to 'pMax', say 0.7.

I'm not sure how to do that, is this it?

Movement = 0.5, 0.8, 0.5 (1.067)

Limited = x, y, z (1.067)

New vector = movement * (0.7 / movement.length()

new.x = 0.5 * (0.7 / 1.067)

new.y = 0.8 * (0.7 / 1.067)

new.z = 0.5 * (0.7 / 1.067)

New = 0.33, 0.52, 0.33

Voila, magnitude = 0.7 smile.png

Is this it?

If so, I'll make an optimized version of the above as a (inline) function in my vector struct.

Ps.: this really helps brushing up vector math :)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This is called vector normalization. (Make vector length one)

normalize(V) = V / magintude(V)

So in your case: Vmove = normalize(V) * movespeed

This topic is closed to new replies.

Advertisement