Skip to content

Engine configuration

xam4lor edited this page Jun 10, 2020 · 12 revisions

This page describes how setEngineConfig and addObjects should be used, and what are all of their parameters. To understand were to use them, please read the corresponding wiki page.


The setEngineConfig function

Inside the runSimulator(simulator) function (more infos in the pSEngine basics wiki page, you can use the simulator.setEngineConfig function to change the engine configuration based on what your simulation requires.

To explain how to do that, lets admit that there is a parameter called plotter.scale.x and that to set his value to 4. To do that, you will need to use the following function:

function runSimulator(simulator) {
   simulator.setEngineConfig((engineConf) => {
      engineConf.plotter.scale.x = 4;
   });
}

Here below, you can usefull configuration parameters of the engine, sorted in function of their use. For more in-depth engine configuration, you will find every parameters in the Simulator.js file, in getDefaultEngineConfig() function.

The default parameter of the configuration is written in front of the : character. For example, if you don't change the runner.simulationSpeed parameter, it will be at value 1.

  • runner
    • divId : 'simulationContent' (canvas will be drawn inside the div with this id)
    • DRAW_FPS : 60 (maximum FPS of the engine. 1/FPS gives you the minimum time (in seconds) between two draw() function calls)
    • simulationSpeed : 1 (speed multiplier of the simulation)
  • window
    • proportions
      • isRelative : true (are units relative to screen, or in pixels ?)
      • width : 0.99 (width of the canvas in the document)
      • height : 0.99 (height of the canvas in the document)
  • plotter
    • scale
      • x : 10 (simulation size is 20 meters width long (10 right + 10 left from origin) )
      • y : 10 (simulation size is 20 meters height long (10 top + 10 bottom from origin) )
      • squareByX : true (true : keep proportions based on X value)
    • offset
      • x : 0 (X offset from the origin in meters)
      • y : 0 (Y offset from the origin in meters)
    • backgroundColor
      • draw : true (draws background each time draw() function is triggered)
      • color : { r : 0, g : 0, b : 0 } (color of the background)



The addObjects() function

Inside the runSimulator(simulator) function, you can call the addObjects() function to add all of your objects to the simulation. Lets say that you have the following class and you want to add it to the simulation:

 class DummyObject {
    constructor(a, b) {
       // a should be a random number bewteen 0 and 1 for each dummy object
    }

    // you MUST implement these two functions, even if they does nothing
    update() {}
    draw  () {}
 }

We want to create 10 dummy objects to our simulation, and give each of them a unique random number between 0 and 1. We also know that the p5 function random(0, 5) returns a random number between bewteen 0 and 5.

So, the corresponding code is the following:

function runSimulator(simulator) {
   simulator.addObjects(
      DummyObject,  // The class of your object (note that the class shouldn't be instanciated)
      10,           // We want to create 10 dummy objects
      [
         "_RUN_F",  // On each object, the following function will be executed
         random,
         // Below are the list of every function parameters
         0,
         5
      ]
   );
}

You will find an example of the implementation of this method in the Boucing Ball example.


Previous (Using animations) - Back to wiki home

Clone this wiki locally