matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Calling setMass on an object which was previously setStatic results in inertia NaN

Open craigulliott opened this issue 3 years ago • 1 comments

Hello, thank you for this wonderful library. We use Matter extensively at Codeverse (we teach kids how to code using a proprietary language called KidScript)

We recently upgraded from v10 to v16 and noticed some unexpected behaviour. I have traced the issue to the setting of mass, after the object has already been set static. Doing this, will cause the value of inertia to be NaN. Here is a minimal example to reproduce

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/matter.js"></script>
  </head>
  <body>
    <script>
      // module aliases
      var Engine = Matter.Engine,
        World = Matter.World,
        Bodies = Matter.Bodies,
        Body = Matter.Body;

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

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

      // create body
      var platform = Bodies.rectangle(254.5, 384, 150, 49);

      // add all of the bodies to the world
      World.add(engine.world, platform);

      // calling setStatic will set mass, inertia and density to Infinity
      Matter.Body.setStatic(platform, true)

      // calling setMass, the first line of this method is `var moment = body.inertia / (body.mass / 6);`
      // this results in Infinity / Infinity / 6 which is NaN
      Matter.Body.setMass(platform, 1)

      console.log(platform.inertia) // outputs NaN
    </script>
  </body>
</html>

This result happens in Matter.js versions 14, 15 and 16. In versions prior to this, it does not result in NaN.

We're handling this by simply calling Matter.Body.setStatic(platform, false) before setting the mass again, but I wanted to share in case this was helpful.

Thanks very much!

Craig

craigulliott avatar Mar 23 '21 14:03 craigulliott

Hi @liabru. Hope you are well!

I was just wondering if you ever investigated this issue? It still exists in v18.

Thanks so much for an amazing library!

craigulliott avatar Jul 03 '22 16:07 craigulliott