6 – Preliminary Use of 2d Physics based Collisions – Part II

This is a continuation of the tutorial started in Part I — of this example of how to use Defold’s 2d Physics and Collision features.  In this second part, we’ll be adding:

  • A Game Object Spawning Factory – which will create Crates at the cursor position when the mouse button is clicked; and
  • An Explosion for each Crate that hits the floor of the game window – deleting the Game Object.

Here we go…..


Creating a Crate Game Object Spawning Factory


The initial step in creating a factory for this project will be to develop a Game Object Prototype related to a Crate.  Within the Project Explorer, create a new Game Object named ‘cratePrototype’.  You can do this by: right-clicking on the ‘main’ folder | select ‘New’ | select ‘Game Object’.  Name that object ‘cratePrototype’.  We’ll be adding three components to ‘cratePrototype’.

Add a Sprite to the cratePrototype

By double-clicking on the newly created ‘cratePrototype’ within the Project Explorer – you place it in focus within the Outline Window.  Right-click ‘Game Object’ and select ‘Add Component’ | select ‘Sprite’.    Set the sprite properties as shown in the following figure:


Add a Collision Object to the cratePrototype

We’ll add a collision box to control how 2d physics impacts this crate.   Right-click ‘Game Object’ in the Outline Window | select ‘Add Component’ | select ‘Collision Object’ and set its properties as shown in this screenshot.


Then we’ll add a box shape to control when collisions occur: right-click ‘collisionobject’ | select ‘Add Shape’ | select ‘Box’….then set the box’s properties as shown below.


Add a Lua Script to the cratePrototype – boxScript.script

Create a new script (named ‘boxScript’) in the main folder within the Project Explorer (right-click the ‘main’ folder | select ‘New’ | select ‘Script’ | name the script in the dialog).   Replace the default Lua code with the following:


Syntax Explanations

script properties

As described within Defold Documentation,  in this project we establish a set of script properties which are used to control various characteristics of the game object; to include:

  • go.property(“fading”, false) — this property is used as a means to determine the state of the crate: initialized to ‘false’ upon it being spawned, and will be set to ‘true’ once it collides with the floor (a precursor to it being destroyed).
  • go.property(“start”, 0) — this property is set to the time at which the Game Object is spawned.  It can be used to control the timing of explosions, deletion from the game, etc.
  • go.property(“explodeStarted”, false) — this property reflects whether an explosion has been spawned related to this crate Game Object – initialized to ‘false’.


Within the update function – local variables are defined which reflect the x and y position of the crate Game Object — which will be used as locations for the message text that is shown above the Game Object during execution.


When the crate Game Object receives a message from the Defold Game Engine that reflects a collision — AND — that collision is associated with the floor (‘i.e., message.other_id == floorID) then a series of steps is taken:

  • The ‘fading’ script property is set to true;
  • The current position of the crate Game Object is captured into a local variable (boxPos);
  • An explosion Game Object is spawned at the boxPos location;
  • The crate Game Object is deleted; and
  • A Tally of how many crates have hit the floor is updated.


Create a createCrates GameObject and add a Factory to it

Once the ‘cratePrototype’ Game Object has been created,  we need to add a factory to the project.   With ‘main.collection’ in focus within the Outline Window, add a new Game Object – named ‘createCrates’.  Within this game object, add a Factory Object Component – you can retain the default id: factory.  Set the Prototype to ‘cratePrototype.go’ — the prototype you just developed at the beginning of this step — see below:


Add a Lua Script to the createCrates GameObject – spawnCrates.script

Using the Project Explorer, add a new script within the ‘main’ folder…name it: ‘spawnCrates’….and replace the default Lua code with the following:

The primary role of this script is to initialize the global variables: crateCount (how many crates have been spawned)….and crateHitFloor (how many crates have fallen all the way to the floor).   I’ve also captured the current game display width and height — to be used for a future project extension.


Adding a Cursor-Position Driver


Add a Cursor GameObject with a Collision Object

Similar to the procedure used in Tutorial #3, we are adding a cursor Game Object to this project, to include a Kinematic Collision Object with a sphere shape; a cursor label which will echo the current x/y location on the screen, and a script file.  With the ‘main.collection’ in focus within the Outline Window, right-click ‘collection’ | select ‘Add Game Object’ | name this GO as ‘cursor’ within the Properties Window.  Add a Collision Object to ‘cursor’ as shown below.  Note that the Collision Type is set to ‘Kinematic’ — aka, it will collide with other objects, but all logic related to that collision is performed in our script file.  Add a Sphere shape to this Collision Object. (right-click ‘collisionobject’ | select ‘Add Shape’ | select ‘Sphere’.


Add a label to the ‘cursor’ Game Object and set its properties as follows:


Add a Lua Script to the Cursor GameObject – cursor.script

Finally, within the Project Explorer — add a script file named: cursor.    Replace the default Lua code with the following:


Comments within the above script file explain the major actions taken — updating the cursor location and posting a message with user instructions during each frame cycle.  The on_input function captures the current machine cursor location and uses it as the position for spawning a new crate Game Object if the user presses the mouse button.  A count of the total number of crates created is also updated.


Within the Outline Window, add this script as a Component File to the ‘cursor’ Game Object.



Including an Explosion when a Crate hits the floor


To quickly complete the last step in this project, we’re going to create the factory which will spawn explosions.  A short summary of the actions to do this includes: (1) create a explosionPrototype Game Object, (2) add an explosion script, and (3) create a factory.


Create an explosionPrototype GameObject

Within the Project Explorer, add a new Game Object to the ‘main’ folder with the id: ‘explosionPrototype’.  Within the Outline Window, modify the properties as follows:


Add a Lua Script to the explosionPrototype – explosion.script

Add a new script to the ‘main’ folder of the Project Explorer named: ‘explosion’.   Replace the default code with the following:


With the explosionPrototype.go in focus within the Outline Window — add this script to that game object.


Add a createExplosion factory to the explosionPrototype

Double-click ‘main.collection’ to place it in focus within the Outline Window.   Right-click ‘Collection’ and add a new Game Object, changing the id of this GO to ‘createExplosion’.  Add a factory to createExplosion (right-click ‘createExplosion | select ‘Add Component’ | select ‘Factory Object’.   Set the properties for this factory object as follows — linking the explosionPrototype to this factory.



The Final Result

At this point, we should be ready to build the entire project.  Actually, I would strongly suggest that you save frequently as you build out each of the game objects/game components throughout the tutorial and build at each step — this will let you capture any potential syntax errors along the way.  The final project can be downloaded by clicking the following button.



Return to Introductory Tutorials