Script on World Events

SimPhy allows to write Javascript code corresponding to events associated with world.

To open script editor open world setting by right clicking empty area on canvas when simulation is not running and open world setting.


Open world menu as shown, by right clicking topmost node in simulation panel , and select edit

A window pops up, now open script Tab in the window, now the window should look like this

Select the event type to add /edit script associated with this event.

For more about scripting look scripting reference.

Examples of World Events Scripting

The "OnStart" and "OnStop" Events

The "OnStart" and "OnStop" event are called by the physics world whenever the simulation starts and stops respectively. Both those properties are meant to hold some javascript code that it is going to be evaluated when the corresponding events occur.

Here's a simple example:

An object named "Disc" already added to world, which changes its text to "Start!" when the simulation starts and changes again to "Stop!!" when the simulation stops:

function onStart()


function onStop()


The "OnStart" property can be particularly useful to initialise pa scene with objects kept at predefined locations. Ex to add 10 objects on x-axis at separation 1m each.

for(var i=0;i<10;i++){
   var d=world.addDisc(0.2);
   d.translate (i,0)

The "OnUpdate" Event

The onUpdate() event is fired after every simulation step. If this property is not empty (i.e. it contains some javascript code) it will be executed by the program's script engine.

The argument passed to this event is instantaneous time since simulation is running.

Let's consider an example:
Say that you want to add discs to the simulation after every 100 steps

Step 1: Declare global variable t in onStart() event by adding script t=0; to onStart Script in world

function onStart()


Step 2: Create disc after every 100 steps

function onUpdate()


The "OnUpdate" property can also be useful to put constraint over object's motion.

To create simulation where three objects disc, disc1, disc2 follow each other. Create new simulation and add 3 discs (with default names) and arrange at vertices of triangle as shown

Now add following code to onUpdate() property of world script and save script (hopefully with no compilation error)

var r1= disc.getPosition();
var r2= disc1.getPosition();
var r3= disc2.getPosition();
var r12=(;
var r23=(;
var r31=(;

on running simulation you will observe that each disc follows its next disc which seems too Cool!!

The "OnCollision" Event

This property is meant to hold javascript code which is going to be evaluated whenever the body collides with another body. The argument is passed to the "OnCollision" property are body1, body2 and time where body1 and body2 are bodies colliding together and time is time when collision occurred.

Here's an example:

Say you want  to exchange brushes of two bodies on collision.

var brush = body2.getBrush();

The "OnKeyPress" and "OnKeyRelease" Properties

The "OnKeyPress" and "OnKeyRelease" properties are called whenever a key is pressed and released respectively. Those two properties are called with a single argument: the key that was pressed or released.
Here's an example:

An object named "Disc" already added to world, which changes its text when the simulation starts and changes again to "Stop!!" when the simulation stops:

function onKeyPressed (keyCode)

Disc.setText("Key Pressed=" + key );

function onKeyReleased (keyCode)

Disc.setText("Key Released=" + key );

You can check which key was pressed or released by comparing the variable key with KeyCode. So for example, if you would like to make an object "jump" whenever the UP arrow key is pressed, you'll have to set its "OnKeyPress" property to something similar to the snippet shown below:

if(keyCode == 65)
     Disc.translate(0.5,0); //keycode =65= 'A'