How to use matter-js - 10 common examples

To help you get started, we’ve selected a few matter-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vibertthio / runn / src / renderer / physic.js View on Github external
for (let i = 0; i < pairs.length; i++) {
        const pair = pairs[i];

        if (pair.bodyA.label === 'avatar' || pair.bodyB.label === 'avatar') {
          // console.log('avatar collision start');
          this.onBlock = (pair.bodyA.label === 'avatar') ? pair.bodyB : pair.bodyA;
        }

        if (pair.bodyA.label === 'avatarChord' || pair.bodyB.label === 'avatarChord') {
          // console.log('avatar collision start');
          this.onBlockChord = (pair.bodyA.label === 'avatarChord') ? pair.bodyB : pair.bodyA;
        }
      }
    });

    Events.on(this.engine, 'collisionEnd', e => {
      const pairs = e.pairs;
      for (let i = 0; i < pairs.length; i++) {
        const pair = pairs[i];
        if (pair.bodyA.label === 'avatar' || pair.bodyB.label === 'avatar') {
          // console.log('avatar collision start');
          this.onBlock = null;
        }

        if (pair.bodyA.label === 'avatarChord' || pair.bodyB.label === 'avatarChord') {
          // console.log('avatar collision start');
          this.onBlockChord = null;
        }
      }
    });

  }
github vibertthio / runn / src / renderer / physic.js View on Github external
initCollisionEvents() {
    Events.on(this.engine, 'collisionStart', e => {
      const pairs = e.pairs;
      for (let i = 0; i < pairs.length; i++) {
        const pair = pairs[i];

        if (pair.bodyA.label === 'avatar' || pair.bodyB.label === 'avatar') {
          // console.log('avatar collision start');
          this.onBlock = (pair.bodyA.label === 'avatar') ? pair.bodyB : pair.bodyA;
        }

        if (pair.bodyA.label === 'avatarChord' || pair.bodyB.label === 'avatarChord') {
          // console.log('avatar collision start');
          this.onBlockChord = (pair.bodyA.label === 'avatarChord') ? pair.bodyB : pair.bodyA;
        }
      }
    });
github plinko-team / plinko / lib / client / client.js View on Github external
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// On click, add a chip at the mouse's x and y relative to canvas
document.querySelector('canvas').addEventListener('click', function (e) {
  e.preventDefault();

  var chip = new _Chip2.default({ x: e.offsetX, y: e.offsetY });

  chip.addToEngine(_engine2.default.world);
  chip.addToRenderer(_renderer.stage);
  chip.registerUpdateListener(_engine2.default);
  _renderer.renderer.render(_renderer.stage);
});

// Collision Events
_matterJs.Events.on(_engine2.default, 'collisionStart', function (event) {
  var pairs = event.pairs;

  for (var i = 0; i < pairs.length; i++) {
    var pair = pairs[i];
    console.log(pair.bodyA.sprite.tint);
    console.log(pair.bodyB.sprite.tint);

    if (pair.bodyA.label === 'peg') {
      pair.bodyA.sprite.tint = 0xff0000;
    } else if (pair.bodyB.label === 'peg') {
      pair.bodyB.sprite.tint = 0xff0000;
    }

    console.log(pair.bodyA.sprite.tint);
    console.log(pair.bodyB.sprite.tint);
  }
github cyclegtx / colyseus-iog-state-sync / Games / Example / Entities / Character.ts View on Github external
// if(this.body.velocity.x != 0 || this.body.velocity.y != 0){
        //     console.log(this.body.velocity)

        // }
        if(this.action == "dead"){
            return;
        }

        if(this.mlb === true && this.entityInHand != null){
            this.action = "hide";
        }else if(this.mlb === false && this.action == "hide"){
            this.action = "idle";
        }

        this.mouseDistance = Vector.sub(this.mousePosition,this.body.position);
        // Body.setAngle(this.body, Vector.angle(Vector.create(0, 1), this.mouseDistance));

        if(this.action != "hide"){
            if (this.dirX != 0 || this.dirY != 0) {
                Body.translate(this.body, Vector.mult(Vector.create(this.dirX, this.dirY), this.speed));
                // Body.setVelocity(this.body,Vector.create(this.dirX*20,this.dirY*20));
                this.action = "move";
            } else {
                this.action = "idle";
            }
        }

        if (this.mrb === true && this.entityInHand != null) {
            this.throw();
        }
github mbchang / dynamics / hockey.js View on Github external
function () {

    // module aliases
    var Engine = Matter.Engine,
        World = Matter.World,
        Bodies = Matter.Bodies,
        Body = Matter.Body;

    var Hockey = {}

    Hockey.create = function(env) {
        var self = {}; // instance of the Hockey class

        // these should not be mutated
        var params = {show: false,
                      num_obj: 3,
                      cx: 400,  // 640
                      cy: 300,  // 480
                      max_v0: 20,
                      obj_radius: 50 };
        self.params = params;
github Coder2012 / pixi / spaceshooter / src / js / matterConsts.js View on Github external
import * as Matter from 'matter-js'

// Matter.js module aliases
const consts = {
  Engine: Matter.Engine,
  World: Matter.World,
  Body: Matter.Body,
  Bodies: Matter.Bodies,
  Events: Matter.Events,
  Vector: Matter.Vector,
  Composite: Matter.Composite
}

export default consts
github mbchang / dynamics / main.js View on Github external
var Matter = require('matter-js')
var jsonfile = require('jsonfile')
var _ = require('underscore')
require('./demo/js/Examples')

// module aliases
var Engine = Matter.Engine,
    World = Matter.World,
    Bodies = Matter.Bodies,
    Body = Matter.Body,
    Example = Matter.Example;


// some example engine options
var engine_options = {
    positionIterations: 6,
    velocityIterations: 4,
    enableSleeping: false,
    metrics: { extended: true }
};

var env = {}
env.engine = Engine.create(engine_options);
github bastarder / vue-endless-h5-game / src / js / fight-wall.js View on Github external
// 木偶人对战画面, 效率太低, 废弃;
let Matter = require('matter-js');
 var Engine = Matter.Engine,
     Render = Matter.Render,
     Runner = Matter.Runner,
     Body = Matter.Body,
     Constraint = Matter.Constraint,
     MouseConstraint = Matter.MouseConstraint,
     Mouse = Matter.Mouse,
     World = Matter.World,
     Bodies = Matter.Bodies,
     Composites = Matter.Composites,
     Composite = Matter.Composite,
     Events = Matter.Events;

Composites.Person = function(x, y, ){
  let group = Body.nextGroup(true),
      Person = Composite.create({ label: 'Person'}),
      radius = 2;
  
  var options = { 
    collisionFilter: { 
      group: group 
    },
    chamfer : {
      radius: 4
    }
github Coder2012 / pixi / spaceshooter / src / js / matterConsts.js View on Github external
import * as Matter from 'matter-js'

// Matter.js module aliases
const consts = {
  Engine: Matter.Engine,
  World: Matter.World,
  Body: Matter.Body,
  Bodies: Matter.Bodies,
  Events: Matter.Events,
  Vector: Matter.Vector,
  Composite: Matter.Composite
}

export default consts
github Coder2012 / pixi / spaceshooter / src / js / matterConsts.js View on Github external
import * as Matter from 'matter-js'

// Matter.js module aliases
const consts = {
  Engine: Matter.Engine,
  World: Matter.World,
  Body: Matter.Body,
  Bodies: Matter.Bodies,
  Events: Matter.Events,
  Vector: Matter.Vector,
  Composite: Matter.Composite
}

export default consts