# GM:S 1.4 Setting Up Box2D Physics

Discussion in 'Tutorials' started by Supercoder, Jul 8, 2016.

Tags:
1. ### SupercoderGuest

GM Version: Gamemaker: Studio

Target Platform: All

Summary:
This tutorial will explain how to set up the basics of a physics world. Getting started was one of the hardest steps for me, and I hope this helps ease the process a bit.

Tutorial:

Step 1:
The first step is to create your sprites. You’ll only need two: a ground sprite and a box sprite. Nothing too fancy

Step 2:

Create a ground object and a box object. Check the “Uses Physics” box on both of them. The only setting you have to change right now is the density of the Ground object. Set it to 0. Setting the density to zero means the object will be solid and unmovable.

You should also press that Modify Collision Shape button and create a “fixture” for your object. This will be used for all physics collisions, and should be as accurate as possible within the constraints (no concave, only 8 points max, etc…)

In the box object, add collision events with the Box and Ground objects. Throw in empty comments so the events aren’t automatically removed.

Step 3:

The next step is the room. Create a room and place the solid ground and movable box tiles however you want. The most important thing to remember is the “physics” tab of the room editor. The "Room is Physics World" box has to be checked for anything to happen. As for the gravity and pixels to meters values, I prefer the gravity at 30 and the pixels to meters at 0.05.

Step 4:
If you run your game right now (go ahead, I’ll wait for you), you’ll notice that the boxes drop and don’t do anything after that. This is where you can add stuff. There are many ways to make things move in the physics world, but I’m going to demonstrate physics_apply_impulse (). In the step event of the box, place this short bit of code:

Code:
```if keyboard_check_pressed(vk_space)
{
physics_apply_impulse (0,0,0,-1)
}```
Now when you run the game you’ll be able to move the boxes around with the space bar.

That’s it! Following these instructions you should be able to set up a basic physics room. The documentation is your friend beyond here. Go through it, test things, and learn!

Last edited by a moderator: Jul 8, 2016
Aura likes this.
2. ### chancepredictably randomForum StaffModerator

Joined:
Apr 22, 2016
Posts:
804
This is such a basic introduction, I was hesitant to accept it. To be honest, there's not much more here than in the manual. But I suppose some beginners are intimidated by the manual, so perhaps this example can get them started.

One comment I'll make, however, is that using dedicated collision events for the ground objects and box objects isn't necessary in this case. And it can sometimes be inefficient.

Instead of separate collision events for each object, box2D has "collision groups" to specify which objects collide with each other. In your example, change the collision group to a non-zero number (say, 1) for both objects (ground and box object). The selection can be made in the "physics property" tab of the objects. Or you can set it in code. (All this is discussed in the manual.)

Now you can remove the collision checks and let box2D handle the collisions automatically, as it's designed to do.

Last edited: Jul 8, 2016
3. ### SupercoderGuest

Yeah, it is pretty basic. I just thought it might be helpful for someone really unsure about the code.

As for the collision groups you're talking about, I've never actually used them. I've used physics for a while, but I never tried them. I'll look into that

4. ### RichHopefulComposerMember

Joined:
Jun 20, 2016
Posts:
1,399
I think it's a good tutorial for beginners. Also, your avatar is awesome. Good job! =D
Edit: Also, your signature is awesome. Terrifying dog, right there! Look how it expertly blocks in its victims!

Last edited: Jul 10, 2016
Supercoder likes this.
5. ### NewOxygenGuest

Actually, the manual specifically states to use collision groups as a last resort. So you may need to elaborate on what inefficiencies not using collision groups actually are.

Quote from manual:
• Note: Using collision groups dramatically increases the processing required by the physics system and should be used only when absolutely necessary, and you should have as few groups as possible!

6. ### chancepredictably randomForum StaffModerator

Joined:
Apr 22, 2016
Posts:
804
Notice I was careful to say dedicated collision events "can sometimes be inefficient". Because it obviously depends on the relative number of each.

One example where collision groups are efficient, is when creating complex physics objects comprising multiple fixtures. Using collision groups, you can bind multiple fixtures to a single object -- but only have some of those fixtures collide with particular objects. Whereas dedicated collision events affect every fixture bound to that object. That's where collision groups are useful.

Alternatively, you can associate each fixture with a separate object, and then use dedicated collision events for each object in the structure. But for complex structures, collision groups are more efficient. And you seldom need more than 2 groups.

As always, members can test which approach works best for them. But I felt it was important to mention "collision groups" in the context of Supercoder's tutorial.