Easy Forces - 2D Physics in PlayBasic

Easy Forces

Before moving on to more real-world physics, I think a word should be said about the quick and dirty way of simulating forces in 2D games. Most often, these forces are wind and gravity. If you are more interested in real-world physics than 2D game physics, you can skip this chapter.

In the last chapter, you were shown how to simulate the forces of thrusters on a ship in space with no other forces acting on it. The only difference between these simple thrusters and wind or gravity is that the player is not in control of them. Because all forces add up to "Net Force", there is no complicated math to deal with. You can simply add another force to the speed of the game object.

In the below code example, I have added the force of gravity to dot :

x# = 400
y# = 300

VertThrust# = 0.01 ; 1 cppl 
HortThrust# = 0.01

gravity# = 0.005 ; define the force of gravity

xSpeed# = 0
ySpeed# = 0

SetFPS 60

Do

    ySpeed# = ySpeed# + gravity# ; add gravity to the speed
    x# = x# + xSpeed#
    y# = y# + ySpeed#

    Cls 0
        Print "xSpeed : " + str$(xSpeed#)
        Print "ySpeed : " + str$(ySpeed#)

        Circle x#, y#, 2, 1

        If RightKey()
            xSpeed# = xSpeed# + -HortThrust#
            DotC x#+3,y#, rgb(255,0,0)
        EndIf
        If LeftKey()
            xSpeed# = xSpeed# + HortThrust#
            DotC x#-3,y#, rgb(255,0,0)
        EndIf
        If UpKey()
            ySpeed# = ySpeed# + VertThrust#
            DotC x#, y# - 3, rgb(255,0,0)
        EndIf
        If DownKey()
            ySpeed# = ySpeed# + -VertThrust#
            DotC x#, y# + 3, rgb(255,0,0)
        EndIf

    Sync
Loop

Only two lines needed to be added to the previous code example for this :

gravity# = 0.005 ; define the force of gravity
ySpeed# = ySpeed# + gravity# ; add gravity to the speed

Adding the force of wind to the above example is not at all different from the above example. The only difference is that wind comes from a different direction.

x# = 400
y# = 300

VertThrust# = 0.01
HortThrust# = 0.01

gravity# = 0.005
wind# = 0.005 ; define the force of wind

xSpeed# = 0
ySpeed# = 0

SetFPS 60

Do

    ySpeed# = ySpeed# + gravity#
    xSpeed# = xSpeed# + wind# ; add wind to speed
    x# = x# + xSpeed#
    y# = y# + ySpeed#

    Cls 0
        Print "xSpeed : " + str$(xSpeed#)
        Print "ySpeed : " + str$(ySpeed#)

        Circle x#, y#, 2, 1

        If RightKey()
            xSpeed# = xSpeed# + -HortThrust#
            DotC x#+3,y#, rgb(255,0,0)
        EndIf
        If LeftKey()
            xSpeed# = xSpeed# + HortThrust#
            DotC x#-3,y#, rgb(255,0,0)
        EndIf
        If UpKey()
            ySpeed# = ySpeed# + VertThrust#
            DotC x#, y# - 3, rgb(255,0,0)
        EndIf
        If DownKey()
            ySpeed# = ySpeed# + -VertThrust#
            DotC x#, y# + 3, rgb(255,0,0)
        EndIf

    Sync
Loop

Using this quick and dirty method of 2D game physics and Net Force, you can make any number of pulling or pushing forces to your game object.

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