How to make ball bouncy in matterjs

Hi I adapted this snippet of code from here However, despite the restitution, the objects aren’t bouncy at all. Is there anything I need to change?

// This example is based on examples from: http://brm.io/matter-js/

var Engine = Matter.Engine;
var Render = Matter.Render;
var World = Matter.World;
var Bodies = Matter.Bodies;
var Composite = Matter.Composite;

var engine;

var boxA;
var boxB;
var ground;

function setup() {
  createCanvas(800, 600);

  // create an engine
  engine = Engine.create();

 let options = {
    isStatic: true,
    restitution: 0.8
  }
  // create two boxes and a ground
  boxA = Bodies.rectangle(400, 200, 80, 80);
  //boxB = Bodies.circle(490, 50, 80, 80);
  boxB = Bodies.circle(490, 50, 20, 20);
  ground = Bodies.rectangle(400, 610, 810, 60, options);


  World.add(engine.world, [boxA, boxB, ground]);

  // run the engine
  Engine.run(engine);
}

// Using p5 to render
function draw() {

  // var bodies = Composite.allBodies(engine.world);

  background(51);

  var vertices = boxA.vertices;
  fill(255);
  beginShape();
  for (var i = 0; i < vertices.length; i++) {
    vertex(vertices[i].x, vertices[i].y);
  }
  endShape();

  // boxB vertices
  var vertices = boxB.vertices;
  fill(255);
  beginShape();
  for (var i = 0; i < vertices.length; i++) {
    vertex(vertices[i].x, vertices[i].y);
  }
  endShape();

  // Ground vertices
  var vertices = ground.vertices;
  beginShape();
  fill(127);
  for (var i = 0; i < vertices.length; i++) {
    vertex(vertices[i].x, vertices[i].y);
  }
  endShape();
}
1 Like

if you go
https://github.com/shiffman/p5-matter and

  • download the ZIP
  • unzip it to any directory
  • go down to
    …\p5-matter-master\01_basics\
  • double click index.html

i think you get what it is supposed to do??
( test under win7 / 64bit // firefox browser )

so can you tell us what/HOW you try to do it,
there is much more info about your working environment needed!

shouldn’t options be applied to the active bodies as well? this works with restitution set on both actives

2 Likes

Some very old posts about “matter.js” library: :link:

Thank you! It’s really helpful.