Physics in GameMaker

The following is an introduction demo of how physics can work in Game Maker Studio 2. It will give you an introduction of how you could implement it in your game.

Step 1: Create Sprites for Physics Objects

The sprites you need for this demo can be downloaded from here –
Here are the physics room sprites saved to a zip folder

  1. Download the sprites, extract the zip folder and create each individual sprite over on the Asset Browser Resources menu

  2. Name the sprites appropriately – see above image
  3. Set each origin point for these sprites to Middle Centreorigin point

Step 2: Create Parent objects

  1. Create two objects, but don’t associate them with any sprites.
    Name the 1st object objStaticParent and the 2nd object objDynamicParent
  2. Add 2 collision events to objStaticParent – a collision event with objDynamicParent and a collision event with itself – see below
    Add 2 collision events to objectDynamicParent – a collision with itself and a collision with objStaticParent.
  3. There is no code needed on these collision events
  4. Add an Outside Room event and in the code editor type the function (see below screenshot) :
    instance_destroy();

Step 3: Create physics objects

  1. Create 3 objects – objPhysicsSquare, objPhysicsCircle and objPhysicsTriangle and associate them with their relevant sprites.
  2. On each of these objects, check the box Uses Physics
  3. Click the Parent box of each of these objects and associate them with objDynamicParent, as these are going to be your dynamic objects – see below for screenshot
  4. Create another object called objPhysicsFloor, associate it with the red square sprite.
  5. Ensure you check the Uses Physics box
  6. On objPhysicsFloor the associate the Parent option with objStaticParent

Step 4: Set the physics properties of each object

  1. Open objPhysicsCircle, click on the Physics button in its window
  2. Change the Density to 0.1 and the Restitution to 0.8 – this is to make it lighter
  3. Under these properties, click Modify Collision Shape and change the shape to a circle in the top left of this window
  4. Open your triangle object and click Physics and Modify the Collision Shape for it.
  5. Change the shape to a Convex Shape
  6.  Click on each corner of the triangle to add three points to it. You should only see 3 sets of points in the X & Y co-ordinates under Convex Shape
    physics-triangle
  7. Open the physics properties for objPhysicsSquare and make sure the collision shape is box and has 4 points
  8. Open objPhysicsFloor and in its Physics properties change its Density to 0.
  9. Modify it collision shape and change it to a convex shape and click on the four corners of it to give it 4 points.
    You might also have to do this with objPhysicsSquare, but test first.

Step 5: Create a Control object to generate the physics objects

  1. Create an object called objControl
  2. Add an Alarm 0 event to it, there is no code needed for it.
  3. Add a Step – Step event and add this code to the Step events code window:
 /// @description Generate physics objects

if (mouse_check_button(mb_left) && (alarm[0] < 0))

{

instance_create_layer(mouse_x, mouse_y, layer, choose(objPhysicsSquare, objPhysicsCircle, objPhysicsTriangle));

alarm[0] = 5; //just creates a pause so you cannot hold down the mouse

}

Step 6: Create a blank room

  1. From the Resources menu, right click on Rooms and click Create Room
  2. Give the room the name PhysicsRoom
  3. On the left side, under Properties, Room Physics, check Enable Physics
  4. You can play with the gravity settings and see what different effects they will have
  5. Next, add some red objPhysicsFloor objects onto the Instances layer to the bottom of the room, and randomly around the room.
  6. Above these, add the objPhysicsSquare, objPhysicsCircle and objPhysicsTriangle objects
  7. Add objControl control into the room also

To test your room, make sure it’s dragged to the top of your rooms list, then run the game

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *