• 08/16/13 05:38 AM
    Sign in to follow this  

    Building a First-Person Shooter: Part 1.3 Keyboard Inputs

    General and Gameplay Programming

    ChrisVossen
    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: [CODE] move = 0.0; strafe = 0.0; cameraypositionsmoothing = 3.0; maxacceleleration = 0.5; movementspeed = 3.0; [/CODE] The player [tt]UpdateControls[/tt] function will be added to the start of the player [tt]Update[/tt] and will manage user inputs via the keyboard and mouse: [CODE] UpdateControls(); [/CODE] Inside [tt]UpdateControls[/tt] 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 [tt]move[/tt] and left and right movements in the variable called [tt]strafe[/tt]. [tt]KeyDown()[/tt] will return a Boolean value of [tt]true[/tt] or [tt]false[/tt], but in C++ these values are essentially the integers 1 (true) and 0 (false). So if you look at the code for [tt]move[/tt] pressing the 'W' key would translate to [tt]move = 1 - 0[/tt] which results in 1. [CODE] 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); [/CODE] Now that we have values for [tt]move[/tt] and [tt]strafe[/tt] 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: [CODE] float maxaccel = this->maxacceleleration; float movespeed = this->movementspeed; normalizedmovement.z = move; normalizedmovement.x = strafe; normalizedmovement = normalizedmovement.Normalize() * movespeed; [/CODE] We now call the entity [tt]SetInput[/tt] with the [tt]normalizedmovement[/tt] the z axis being forward and backwards movement and the x axis being left and right. We also make use of the [tt]maxaccel[/tt] value set earlier: [CODE] entity->SetInput(0,normalizedmovement.z,normalizedmovement.x,0,false,maxaccel); [/CODE] 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: [CODE] cameraposition = entity->GetPosition(); camera->SetPosition(cameraposition.x, cameraposition.y + cameraheight, cameraposition.z ); [/CODE] Running the resulting code will allow you to move around using the W,A,S,D keys: [CODE]#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 ); }[/CODE]


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    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


    jbadams

    Report ·

      

    Share this review


    Link to review
    Rifts605

    Report ·

      

    Share this review


    Link to review
    Ectara

    Report ·

      

    Share this review


    Link to review