PhySics 2d Integration


 Comparison of physics engines 

COMPARISON OF PHYSICS ENGINES



vs



  • version 0.6.0
  • 2D Physics only
  • Good documentation
  • Good Demos and Tutorials
  • No Nodejs support
  • Basic properties
  • More different  approach

  • version 0.6.0
  • 2D physics only
  • Pure documentation
  • Good Demos and tutorials
  • Nodejs support
  • Lots of properties
  • Compatible structure


COMPARISON OF PHYSICS ENGINES

AOR Framework

Main operation principles


  • AOR is simulation framework written in javascript
  • Works in browser
  • Uses Canvas Browser API for drawing 2D scenes
  • Simulation is described via XML
  • Java Codegen tool generates executable javascript code from given XML
  • Generated Javascript manages simulation objects, their behavior and user interaction on each simulation step

P2.js

main operation principles


  • Written in Javascript
  • Creates physical environment called World
  • Bodies are first class World citizens - Javascript objects that represent physical entities 
  • Shapes are served as visual representation of Bodies
  • All P2 objects and their relations are described via Javascript
  • There is a World step as a smallest undivided unit of time


how aor and p2 work together?

initialization


  • AOR creates Objects/Agents and all related behaviors/events
  • These objects have some initial properties (position, etc)
  • These properties are defined on "simulationInitialize" event and then are being changed depends on simulation logic during simulation steps
  • After all AOR properties are initialized, P2.js creates corresponding "wrapper" objects called bodies and related shapes, and adds all these objects to a P2 World
  • Now we are ready for simulation steps!

HOW AOR AND P2 WORK TOGETHER?

simulation steps


  • AOR system notifies about each simulation step via "notifySimStepStart" and "notifySimStepEnd" event
  • P2 World step will be executed on "notifySimStepStart" event and required property changes of P2 bodies will be made
  • After that all changed properties are propagated to the corresponding AOR objects 

HOW AOR AND P2 WORK TOGETHER?

properties propagation

  • Utils object called Synchronizer.js manages properties propagation
  • It has a mapping table, which contains properties are required to be updated during simulation step

         mapTable = {
            id: 'id',
            position: ['x', 'y'],
            velocity: ['vx', 'vy'],
            mass: 'm',
            angle: 'rotZ'
        }
  • methods fromAorToP2() and fromP2ToAor() realize properties propagation

basic integration requirements


  • Setting physical World based on AOR simulation
  • Properties propagation
  • Events propagation
  • Collision detection
  • Perception realisation
  • Acceleration

ACCOMPLISHED integration requirements  


  • World creation on simulation initialization
  • Creating P2 bodies and shapes based on AOR objects
  • Propagation of some basic physical properties (from P2 to AOR and vice versa)
  • Detection of collision event and its propagation to AOR system
  • Thoughts about how to realize a perception

Improvements

Gravitation

It is possible to select different space model gravitation in scenario.xml. This value will be used during creating P2 world on simulationInitialize AOR event (please see ../Physics2D/script).

Code example:

    ...     this.simulationInitialize = function (initialState) {
        this.state = initialState;
        this.spaceModel = initialState.spaceModel;
        this.gravity = this.spaceModel.gravity ? -this.spaceModel.gravity : -9.81;
        this.world = new p2.World({gravity: [0, this.gravity]});     ...

Default gravitation value is -9.81

Improvements

corresponded P2 Object shapes creation

AOR Simulation Framework supports different kinds of object shapes. E.g. Rectangle, Circle, Polygon.

It is possible now to create corresponded P2 shapes. 

Supported shapes are:

  • Rectangle
  • Circle

Additionally, Plane is supported in P2 as well, but it requires AOR xml scheme improvements. Now physical engine takes the name of the AOR object and if it is equal to 'Plane' creates Plane P2 shape.

Improvements

CORRESPONDED P2 OBJECT SHAPES CREATION

Code example (../Physics2d/script):

     this.createP2Shape = function (object) {
        var name    = object.name,
            width   = object.width,
            height  = object.height,
            type    = this.getShapeType(object.shape2D),
            shape   = new p2[type](width, height);

        // create Plane shape if the name of the object equal to "Plane"
        if (object.name === 'Plane') {
            shape = new p2.Plane();
        }

        // NOTE: now for Rectangle, Circle and Plane only
        this.p2shapes.push(shape);
    };

Improvements

Perception Support

Each AOR PhysicalAgent can have perception properties such as:

  • perceptionRadius
  • perceptionAngle

If such properties are given to AOR agent in scenario.xml then corresponded P2 object will be created. That object will have sensor property set to true and will be able to perceive the world around it. Perception radius and angle will be applied.

Improvements

PERCEPTION SUPPORT

Realisation:

  • Proper sensor shape creation

     this.createP2Shape = function (object) {
        ...

        // create sensor shape if perception is presented
        if (object.perceptionRadius) {
            shape.sensor = true;
            shape.boundingRadius = object.perceptionRadius;
            shape.perceptionAngle = object.perceptionAngle;
        }

        ...
    };

Improvements

PERCEPTION SUPPORT

  • Listening to "postBroadphase" event, that happens before each step

 this.world.on('postBroadphase', this.postBroadphaseCallBack.bind(this));
  • On each P2 step start collision of the sensor perception radius and another object will be checked
  • If collision happens collision "pairs" object will be provided by P2 system as the argument of "postBroadphase" event callback
  • Pairs object provides information about object collided
  • Calculation if object is in perception angle of the sensor
  • If object is in sensor perception angle, sensor property will be set to false to avoid real collision and vise versa if collision is already avoid

Improvements

PERCEPTION SUPPORT

"postBroadphase" callback example:
        var sensorBody                  = data.pairs[1],
            sensorShape                 = sensorBody.shapes[0],
            sensorPerceptionRadius      = sensorShape.boundingRadius,
            sensorPerceptionAngle       = sensorShape.perceptionAngle < 180 ?
                                            sensorShape.perceptionAngle :
                                            180,
            sensorPerceptionAngleRad    = sensorPerceptionAngle * (Math.PI / 180),
            sensorX                     = sensorBody.position[0],
            sensorY                     = sensorBody.position[1],
            sensorSectorLeftBorder      = sensorX - Math.sin(sensorPerceptionAngleRad / 2) * sensorPerceptionRadius,
            sensorSectorRightBorder     = sensorX + Math.sin(sensorPerceptionAngleRad / 2) * sensorPerceptionRadius,

            collisionTargetBody         = data.pairs[0],
            collisionTargetShape        = collisionTargetBody.shapes[0],
            targetBoundingRadius        = collisionTargetShape.boundingRadius,
            targetX                     = collisionTargetBody.position[0],
            targetY                     = collisionTargetBody.position[1],

            collisionX =
                ((sensorX * targetBoundingRadius) +
                (targetX * sensorPerceptionRadius)) /
                (sensorPerceptionRadius + targetBoundingRadius),

            collisionY =
                ((sensorY * targetBoundingRadius) +
                (targetY * sensorPerceptionRadius)) /
                (sensorPerceptionRadius + targetBoundingRadius);

        if (collisionX > sensorSectorLeftBorder &&
            collisionX < sensorSectorRightBorder &&
            collisionY < sensorY){

            sensorShape.sensor = false;
        } else {
            sensorShape.sensor = true;
        }



DEMO

PhySics

By workslon

PhySics

Integration of Physics 2d engine in Simulation Framework (BTU university task)

  • 350