Taking control

We now have a framework to load and render game objects. But, we don't have any way to move our ship! The good news is that we already wrote an input class for RoboRacer2D, and we can reuse that code here.

Implementing input

Earlier in the chapter, I had you copy the Input class from RoboRacer2D into the source folder for SpaceRacer3D. Now, we have to simply wire it into our game code.

Open SpaceRacer3D. First, we need to include the input header. Add the following line of code to the headers:

#include "Input.h"

We also need to create a global pointer to manage the Input class. Add the following line just below the model pointers:

Input* m_input;

Next, we need to create an instance of the Input class. Add the following line of code to the top of the StartGame function:

m_input = new Input(hWnd);

Now, we have to create a function to handle our input. Add the following function just above the Update method:

void ProcessInput(const float p_deltaTime)
{
 Vec3 rotation;
 m_input->Update(p_deltaTime);
 Input::Command command = m_input->GetCommand();
 switch (command)
 {
 case Input::CM_STOP:
 {
  if (ship->GetVelocity() > 0.0f)
  {
   ship->SetVelocity(0.0f);
  }
  else
  {
   ship->SetVelocity(1.0f);
  }
 }
 break;
 case Input::CM_DOWN:
 {
  rotation = ship->GetHeadingRotation();
  rotation.x += -1.0f;
  if (rotation.x < 0.0f)
  {
   rotation.x = 359.0f;
  }
  if (rotation.x < 359.0f && rotation.x > 180.0f)
  {
   if (rotation.x < 315.0f)
   {
    rotation.x = 315.0f;
   }
  }
  ship->SetHeadingRotation(rotation);
 }
 break;
 case Input::CM_UP:
 {
  rotation = ship->GetHeadingRotation();
  rotation.x += 1.0f;
  if (rotation.x > 359.0f)
  {
   rotation.x = 0.0f;
  }
  if (rotation.x > 0.0f && rotation.x < 180.0f)
  {
   if (rotation.x > 45.0f)
   {
    rotation.x = 45.0f;
   }
  }
  ship->SetHeadingRotation(rotation);
 }
 break;
 case Input::CM_LEFT:
 {
  rotation = ship->GetHeadingRotation();
  rotation.z += 1.0f;
  if (rotation.z > 359.0f)
  {
   rotation.z = 0.0f;
  }
  if (rotation.z > 0.0f && rotation.z < 180.0f)
  {
   if (rotation.z > 45.0f)
   {
    rotation.z = 45.0f;
   }
  }
  ship->SetHeadingRotation(rotation);
 }
 break;
 case Input::CM_RIGHT:
 {
  rotation = ship->GetHeadingRotation();
  rotation.z += -1.0f;
  if (rotation.z < 0.0f)
  {
   rotation.z = 359.0f;
  }
  if (rotation.z < 359.0f && rotation.z > 180.0f)
  {
   if (rotation.z < 315.0f)
   {
    rotation.z = 315.0f;
   }
  }
  ship->SetHeadingRotation(rotation);
 }
 break;
 }
}

This code handles keyboard input. You will recall from RoboRacer2D that we mapped virtual commands to the following keys:

  • CM_STOP: This is the spacebar. We use the spacebar as a toggle to both start and stop the ship. If the ship is stopped, pressing the spacebar sets the velocity. If the ship's velocity is greater than zero, then pressing the spacebar sets the velocity to zero.
  • CM_UP: This is both the up arrow and the W key. Pressing either of these keys changes the heading rotation so that the ship moves up.
  • CM_DOWN: This is both the down arrow and the S key. Pressing either of these keys changes the heading rotation so that the ship moves down.
  • CM_LEFT: This is both the left arrow and the A key. Pressing either of these keys changes the heading rotation so that the ship moves left.
  • CM_RIGHT: This is both the right arrow and the D key. Pressing either of these keys changes the heading rotation so that the ship moves up.

Every directional command works by retrieving the current heading angle and changing the appropriate component of the heading vector by one degree. The heading angle is used by each object's Update method to calculate a heading vector, which is used to point the camera in the Render method.

Finally, we need to call HandleInput from the games Update function. Add the following line of code to the top of the Update method, before the object update calls. We want to handle input first and then call each object's update method:

ProcessInput(p_deltaTime);

That's it! Pat yourself on the back and run the game. You can now use the keyboard to control the ship and navigate through your universe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.209.98