06. Advanced PlayerBall Controls

Introduction

This tutorial will implement more advanced controls for the PlayerBall. Most games with first-person control (that is control where you directly direct an Entity, as opposed to third person control like RTS games – not to be confused with first person view) have fairly complex control logic. We’ll be implementing more advanced controls in our game, investigating the logic in detail along the way.

Velocity vs. Acceleration

Our current implementation provides immediate control over the PlayerBall’s velocity values:

In other words, if the input for moving right is held (whether that’s a keyboard key or an Xbox360 analog stick), the PlayerBall immediately moves at maximum speed. Similarly, when the movement input is released, the PlayerBall immediately stops.

We will modify our input code to gradually add to the speed of the PlayerBall when the input is held down. To do this, we’ll change our code to set the PlayerBall’s acceleration rather than velocity. Modify the MovementActivity in PlayerBall.cs as follows:

Notice that the code above still uses the MovementSpeed variable, which can be modified in Glue. This value can be increased to make movement more responsive.

Since we’re no longer modifying velocity values directly (acceleration values indirectly modify velocity), the ball continues to move even after releasing input. We’ll address this in the next section.

Reducing Momentum

We’ll use the Drag property to slow the PlayerBall. Drag modifies velocity proportionally and in the opposite direction of current Velocity. In other words, drag slows an object regardless of its movement direction. The faster an object is moving, the more Drag reduces its velocity.

Drag is applied whether an object is accelerating or not. Since it slows an object down by more when the object is moving faster, it will in effect create a maximum movement speed for our PlayerBall.

To add Drag to the PlayerBall Entity:

  1. Expand the PlayerBall Entity in Glue
    ExpandedPlayerBallInGlue.PNG
  2. Right-click on the Variables item and select “Add Variable”
    AddVariableRightClick.png
  3. Select the “Expose Existing” tab. Existing variables are variables which are normally always available in code. Therefore, these variables are usually functional variables – they require no custom code to implement.
  4. Select “Drag”
    ExposeDragBeefball.png
  5. Change the “Default Value” for Drag to 1
    DragSetTo1Beefball.png

The addition of Drag has changed the way our ball moves:

  1. The ball now has a maximum speed
  2. Releasing all input results in the ball slowing down
  3. The ball does not accelerate as quickly as it used to

We’ll increase the MovementSpeed to make up for the addition of Drag on the PlayerBall’s acceleration. Change the MovementSpeed value from 100 to 350.

Bouncing against the walls

Next we’ll change the collision code to implement bouncing. We can do so by calling the CollideAgainstBounce method instead of CollideAgainstMove. Modify the CollisionActivity as follows:

CollideAgainstBounce adds one more argument – elasticity. Elasticity controls how bouncy the wall is. A value of 1 results in no momentum being lost from the collision. A value of 0 absorbs all movement perpendicular to the collision surface. Feel free to change the elasticity value to see how it impacts collision.

Conclusion

Now we’re getting somewhere! The game is starting to feel pretty solid. Next we’ll add a Puck Entity which can be used to score goals.

<- Creating the Screen CollisionCreating the Puck Entity ->