Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
How do I make object fall on impact in Matter.js?

I'm using Matter.js for some graphics and want this rectangle

let title = Bodies.rectangle(w / 2.4, height / 1.8, 300, 100, {
  isStatic: true,
})

to get isStatic: false and fall when it's hit by some circles that are raining down on it. I've done some extensive Googling, but haven't really found anything else but this:

Events.on(engine, 'collisionStart', function (event) {
  event.pairs.forEach(function (obj) {
    console.log(
      'BodyA is static: ' + obj.bodyA.isStatic + '. BodyB is static: ' + obj.bodyB.isStatic
    )
  })
})

This gives me all the collisions happening, but I haven't figured out how to set isStatic: false when something hits. Appreciate your help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can call Matter.Body.setStatic(body, false) on the body in question to make it active.

Here's an example:

const engine = Matter.Engine.create();
const render = Matter.Render.create({
  element: document.body,
  engine,
  options: {width: 400, height: 400, wireframes: false},
});

const fallingBody = Matter.Bodies.rectangle(
  200, 0, 20, 20, {
    frictionAir: 0.1,
    density: 0.8,
    render: {fillStyle: "red"},
  },
);
const wall = Matter.Bodies.rectangle(
  200, 150, 400, 20, {
    frictionAir: 0.05,
    isStatic: true,
    render: {fillStyle: "green"}
  },
);
Matter.Composite.add(engine.world, [fallingBody, wall]);

Matter.Events.on(engine, "collisionStart", event => {
  if (wall.isStatic && event.pairs.some(e => Object.values(e).includes(wall))) {
    Matter.Body.setStatic(wall, false);
  }
});

Matter.Render.run(render);
Matter.Runner.run(Matter.Runner.create(), engine);
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.18.0/matter.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!