How to create physics shapes for Defold

Joachim Grill
Last updated:
How to create physics shapes for Defold

PhysicsEditor is a powerful tool for creating and editing collision shapes for 2D games. Here's a short tutorial on how to use PhysicsEditor with the Defold game engine:

Step 1: Install and set up PhysicsEditor

Download PhysicsEditor from CodeAndWeb and install it on your system:

Launch PhysicsEditor and select the Defold exporter from the Exporter dropdown on the right sidebar:

PhysicsEditor: select Defold from the Exporter combobox

Step 2: Import sprites

Click on Add Sprites or drag and drop your sprite images into PhysicsEditor's left sidebar.

PhysicsEditor: add sprites by clicking the toolbar button

Select a sprite and edit its collision shape using the provided tools. You can manually adjust the vertices or use the automatic shape tracer.

PhysicsEditor: Tools to create collision shapes

Step 3: Configure shape properties

Use the input fields on the right sidebar to set the shape type (Static, Dynamic, ...) and other properties like Mass, Friction, etc., according to your game needs.

PhysicsEditor: set fixture properties

PhysicsEditor will generate Game Objects containing the Collision Objects as well as the Sprite components. Use PhysicsEditor's Atlas path input field to specify the texture atlas of your Defold project which contains the sprite images.

Step 4: Export collision shapes

Set Shape path to the relative path of the directory to which you are going to export the physics shapes (relative to the Defold project folder, which contains game.project).

To export the collision shapes, click on the Publish button in the toolbar. Then select a subdirectory within your game project folder to which the shapes should be published.

PhysicsEditor: press publish to export the collision shapes

Step 5: Import to Defold

For each sprite, PhysicsEditor has generated a Game Object in your Defold project directory. Open your scene (e.g., main/main.collection) and select Add Game Object File in the context menu of the Outline view to add the Game Object generated by PhysicsEditor.

Defold editor: use context menu to add Game Object File

Defold doesn't allow adding both a circle and a polygon shape to one Collision Object, nor does it support multiple polygon shapes in one Collision Object. It also does not support setting per circle shape properties, if more than one circle is assigned to a Collision Object. That's why PhysicsEditor automatically creates multiple collision objects, one for each shape. This also applies to concave polygons, as they are automatically split into convex polygons.

Multiple Collision Objects in one Game Object work fine for shape type Static, i.e., as long as they are not moved by the physics simulation. For Dynamic objects this configuration cannot be used, as the simulation would result in very erratic shape movements.