Physics GameMaker Tutorial
In this tutorial, we will make a platform game, that works only with physics. Click below, and play with the arrow keys to see what we are going to make:
1. Create a physics room
Create a new room, go to the physics pane, and you'll see these settings. The Y value is the amount of gravity. The power that the room pulls the object down. Adjust it to about 50, or whatever you'd prefer.
2. Create 4 sprites
We need the following sprites:
- Wall
- Small wall
- Player (as a ball, a perfect circle)
- Crate
3. Create a static wall
Create an object, and check "Uses physics" (1). This will enable physics for the object. I call mine obj_wall.
We need obj_wall to be static. Therefore, check "Kinematic" (4).
Kinematic means that it isn't affected by the force of other objects.
To make it completely static, set the density to 0 (2).
Set the collision shape (3). It tells the physics engine how to handle the object. It's like masks are used for normal objects.
An important thing: Set the collision group to 1, in order to make it collide with other physics objects that has collision group 1 as well.
We need obj_wall to be static. Therefore, check "Kinematic" (4).
Kinematic means that it isn't affected by the force of other objects.
To make it completely static, set the density to 0 (2).
Set the collision shape (3). It tells the physics engine how to handle the object. It's like masks are used for normal objects.
An important thing: Set the collision group to 1, in order to make it collide with other physics objects that has collision group 1 as well.
4. Duplicate obj_wall to obj_wall_small
Just for fun. You can skip this step if you want.
Remember to adjust the Collision Shape for the new wall.
Remember to adjust the Collision Shape for the new wall.
5. The player
Create a new object, and call it something like obj_player. The first thing to do, is to enable physics. Modify the collision shape to a circle, and set the collision group to 1.
To make the player move from side to side, when you press <Left> and <Right> key, you are going to use this code:
To make the player move from side to side, when you press <Left> and <Right> key, you are going to use this code:
This will apply a force of -100 to the x-axis of the player, which means it will move to the left. To make it move right, just replace -100 with 100.
The next thing the player should do is to jump. Add a keyboard press event to the up key, and use this code:
The next thing the player should do is to jump. Add a keyboard press event to the up key, and use this code:
If the place isn't free 1 pixel below the player's y position, then the player should jump with a speed of -100. - and that's what the code does.
One """very important thing""" is to make the player exhaust blue smoke (just joking, but we will do it anyways, it looks so pretty):
One """very important thing""" is to make the player exhaust blue smoke (just joking, but we will do it anyways, it looks so pretty):
6. Create the crate
Create a new object, call it something like obj_crate, enable physics, set collision shape, set the density to a little lower than normal, to make it weigh a little less than the player. Instead of making it static, this crate will be dynamic.
7. Make a nice room
Here's my setup, but it's all your choice