Acceleration - 2D Physics in PlayBasic

Acceleration

As you learned in a previous chapter, speed is the rate of change of position (in relation to pixel zero). Built upon this concept is acceleration.

  • Acceleration is the rate of change of speed.

The most important thing to remember is that acceleration affects speed, not position. The position of a game object itself is changed by it's speed. The traditional equation to express acceleration in physics is :

acceleration = speed change / time taken

For example, if a car accelerates from a speed of 0kmph (kilometers per hour) to 30kmph in 60 seconds, the actual acceleration is 30kmph / 60 seconds, or 2kmph per second.

The measure of time in game physics is loopins and as stated in the previous chapter, speed is measured in pixels per loopin (ppl). Acceleration measures change in speed per loopin. The acceleration above could be stated in game physics as 30ppl per loopin. However, when you're running at 60 frames a second, an acceleration in whole pixels is entirely too fast. Therefore, acceleration is almost always expressed in subpixels.

Acceleration in 2D games is a two-step process.

  1. The acceleration is added to the speed.
  2. The speed is added to the position.

The actual code commonly looks like this :

speed = speed + acceleration
position = position + speed

In the example code below, the dot is accelerated 1 centipixel per loopin (1 cppl, or 0.01 pixel per loopin) every loopin. As you can see when you run this code, this small acceleration quickly adds up to substantial speed by the time the dot moves off the right side of the window.

x# = 0
y# = 300
xAcceleration# = 0.01 ; 1 cppl 
xSpeed# = 0

SetFPS 60
Do
    ; speed = speed + acceleration
    xSpeed# = xSpeed# + xAcceleration#
    ; position = position + speed
    x# = x# + xSpeed#
    Cls 0
        Print "xSpeed : " + str$(xSpeed#)
        Dot x#, y#
    Sync
Loop

Deceleration

Deceleration is often thought of as the opposite of acceleration. However, in physics :

  • Acceleration is a change of speed, regardless if it is an increase or decrease.

This is illustrated in the code below. A couple of notes, "acceleration" is applied to the dot by pressing the LEFT and RIGHT arrow keys. Also, when you apply the minus sign as it has been to xAcceleration#, it makes the value negative.

x# = 400
y# = 300
xAcceleration# = 0.01 ; 1 cppl 
xSpeed# = 0

SetFPS 60
Do

    If LeftKey() Then xSpeed# = xSpeed# + -xAcceleration#
    If RightKey() Then xSpeed# = xSpeed# + xAcceleration#
    ; position = position + speed
    x# = x# + xSpeed#
    Cls 0
        Print "xSpeed : " + str$(xSpeed#)
        Dot x#, y#
    Sync
Loop

What you'll notice about this example is that not only can you start and stop the dot, you can get it moving in both directions. This is because acceleration is relative. If you accelerate in the direction of the speed then the speed is increased. If you accelerate opposite the direction of speed then the speed is decreased.

Therefore, for simplicity's sake, we can only define deceleration this way :

  • Deceleration is acceleration in the opposite direction of speed.

This page has been recommended for cleanup, integration, etc.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.