Jump to content

  • Log In with Google      Sign In   
  • Create Account


Like
6Likes
Dislike

Building a First-Person Shooter: Part 1.3 Keyboard Inputs

By Christopher Vossen | Published Aug 15 2013 11:38 PM in Game Programming
Peer Reviewed by (jbadams, Josh Vega, Endurion)

fps leadwerks c++ native code tutorial

Having a player that just stands watching the world go by isn’t much of a game so let’s add in some movement. We will start by adding a few new variables to the constructor:

move = 0.0;
strafe = 0.0;
cameraypositionsmoothing = 3.0;
maxacceleleration = 0.5;
movementspeed = 3.0;

The player UpdateControls function will be added to the start of the player Update and will manage user inputs via the keyboard and mouse:

UpdateControls();

Inside UpdateControls we get the current window and detect if any of the W,A,S,D keys are currently pressed. We will track forward and backward movements in the variable called move and left and right movements in the variable called strafe. KeyDown() will return a Boolean value of true or false, but in C++ these values are essentially the integers 1 (true) and 0 (false). So if you look at the code for move pressing the ‘W’ key would translate to move = 1 – 0 which results in 1.

Window* window = Window::GetCurrent();
//Get inputs from the controller class
move = window->KeyDown(Key::W) - window->KeyDown(Key::S);
strafe = window->KeyDown(Key::D) - window->KeyDown(Key::A);

Now that we have values for move and strafe we need to normalize them so that moving while strafing doesn’t move the character faster than normal. Then after normalizing we scale the movement to the correct movespeed:

float maxaccel = this->maxacceleleration;
float movespeed = this->movementspeed;
normalizedmovement.z = move;
normalizedmovement.x = strafe;
normalizedmovement = normalizedmovement.Normalize() * movespeed;

We now call the entity SetInput with the normalizedmovement the z axis being forward and backwards movement and the x axis being left and right. We also make use of the maxaccel value set earlier:

entity->SetInput(0,normalizedmovement.z,normalizedmovement.x,0,false,maxaccel);

Finally we set the camera position to the same position as the entity and make sure to set it at the correct height of where a head would sit:

cameraposition = entity->GetPosition();
camera->SetPosition(cameraposition.x, cameraposition.y + cameraheight, cameraposition.z );

Running the resulting code will allow you to move around using the W,A,S,D keys:

#include "MyGame.h"
using namespace Leadwerks;
Player::Player()
{
//Create the entity
entity = Pivot::Create();
entity->SetUserData(this);

//Initialize values
move = 0.0;
strafe = 0.0;
cameraypositionsmoothing = 3.0;
maxacceleleration = 0.5;
movementspeed = 3.0;
standheight=1.7;
crouchheight=1.2;
cameraheight = standheight;
smoothedcamerapositiony = 0;

//Create the player camera
camera = Camera::Create();
camera->SetPosition(0,entity->GetPosition().y+cameraheight,0,true);
//Set up player physics
entity->SetPhysicsMode(Entity::CharacterPhysics);
entity->SetCollisionType(Collision::Character);
entity->SetMass(10.0);
//Player position
entity->SetPosition(0,0,0,true);

}

Player::~Player()
{
if (camera)
{
camera->Release();
camera = NULL;
}
}

void Player::UpdateControls()
{
Window* window = Window::GetCurrent();
//Get inputs from the controller class
move = window->KeyDown(Key::W) - window->KeyDown(Key::S);
strafe = window->KeyDown(Key::D) - window->KeyDown(Key::A);
}

//Update function
void Player::Update()
{
UpdateControls();
float maxaccel = this->maxacceleleration;
float movespeed = this->movementspeed;
//Make sure movements are normalized so that moving forward at the same time as strafing doesn't move your character faster
normalizedmovement.z = move;
normalizedmovement.x = strafe;
normalizedmovement = normalizedmovement.Normalize() * movespeed;

//Set player input
entity->SetInput(0,normalizedmovement.z,normalizedmovement.x,0,false,maxaccel);

//Set the camera position
cameraposition = entity->GetPosition();
camera->SetPosition(cameraposition.x, cameraposition.y + cameraheight, cameraposition.z );
}



License


GDOL (Gamedev.net Open License)




Comments

Perhaps you could include links to the prior article parts at the beginning?

 

:-)

the More Like This box does that well, it's just still manually triggered at the moment


Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS