How to Add Movement with Physics in GameMaker: Studio - dummies

How to Add Movement with Physics in GameMaker: Studio

By Michael Rohde

In GameMaker: Studio, you get your Object defined for physics, you might want to allow the player to move the Object around the Room during the game. The following two sections show how to add movement to an Object as well as make an Object turn left or right.

Example code for an up keyboard event to apply local force

To apply force to an Object to make it move, create an Up Keyboard Event as described in the following procedure. Just follow these steps:

  1. From the Resource tree, double-click the obj_player Object or another relevant Object.

    The Object Properties window appears.

  2. Choose Add Event→Keyboard→Up.

    An Up Keyboard Event appears in the Events section.

  3. Drag and drop an Execute Code Action from the Control tab to the Actions window.

    An empty code window appears.

  4. In the code window, type the following code:

    physics_apply_local_force(0, 0, 0, -50);
  5. Click the green check mark.

    The code window is saved and closed.

This code uses the local coordinate system to set a vector for propulsion. The local coordinates are based on the Sprite, where the X and Y axes meet at the Origin. So, no matter which way the Object is rotated in the Room, the origin of the Sprite never changes. In that way, X = 0, Y = -1 would always be just below the Sprite, as shown in this figure.

The first value in this function is for xlocal, the second value is ylocal, the third is xforce_local, and the last value represents yforce_local. By using the value -50 as the value for yforce_local, you’re placing force behind the ship that pushes the ship forward.

The amount of force is based on the value, so a larger number places greater force. Experiment with this by changing the value from -50 to -5000 to see what a difference it makes. If you use a positive number (say, 50) notice that the ship appears to move backward instead of forward.

Local coordinates are based on the Sprite’s Origin as denoted by the red circle.
Local coordinates are based on the Sprite’s Origin as denoted by the red circle.

Example code for left and right keyboard events

To enable the player to turn the Object, add Left and Right Keyboard Events. To have the player turn the Object to the left, follow these steps:

  1. From the Resource tree, double-click the obj_player Object or another relevant Object.

    The Object Properties window appears.

  2. Choose Add Event→Keyboard→Left.

    A Left Keyboard Event appears in the Events section.

  3. Drag and drop an Execute Code Action from the Control tab to the Actions window.

    An empty code window appears.

  4. In the code window, type the following code:

    phy_angular_velocity=0;
    phy_rotation-=10;
  5. Click the green check mark.

    The code window is saved and closed.

During the game, the player can now turn the Object to the left. This code adds or subtracts from the physics rotation, which is what controls the direction and image angle of the Object when the player presses the left-arrow key.

To have the player turn the Object to the right, follow these steps:

  1. From the Resource tree, double-click the obj_player Object (or another relevant object).

    The Object Properties window appears.

  2. Choose Add Event→Keyboard→Right.

    A Right Keyboard Event appears in the Events section.

  3. Drag and drop an Execute Code Action from the Control tab to the Actions window.

    An empty code window appears.

  4. In the code window, type the following code:

    phy_angular_velocity=0;
    phy_rotation+=10;
  5. Click the green check mark.

    The code window is saved and closed. This code adds or subtracts from the physics rotation, which is what controls the direction and image angle of the Object when the player presses the right-arrow key.

You could give the Object a more natural turning by using physics_apply_torque to rotate the Object using torque, which is the rotational force. Torque takes into account the mass of the Object and makes turning slower, much like a real-life object; however, in a game, you don’t always want it to be too real (it is a game, after all).

Another difference between the physics world in GameMaker: Studio and the traditional functions is that normally the direction and image angle are calculated counterclockwise, so adding 10 to the direction variable would rotate the Object to the left. However, when using physics, directions and angles are calculated clockwise, so adding 10 to the rotational value rotates the Object to the right.