Building a First-Person Shooter: Part 1.3 Keyboard Inputs

Published August 16, 2013 by Christopher Vossen, posted by ChrisVossen
Do you see issues with this article? Let us know.
Advertisement
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 ); }
Cancel Save
0 Likes 2 Comments

Comments

jbadams

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

:-)

July 09, 2013 07:59 AM
Gaiiden

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

July 11, 2013 01:15 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

In this tutorial we will continue building the foundations of a FPS game by adding in keyboard movements that will allow the player to move around via W,A,S,D inputs.

Advertisement
Advertisement