Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Body properties are incorrect after switching from static to dynamic #1301

Open
0yz opened this issue Jun 24, 2024 · 4 comments
Open

Body properties are incorrect after switching from static to dynamic #1301

0yz opened this issue Jun 24, 2024 · 4 comments

Comments

@0yz
Copy link

0yz commented Jun 24, 2024

After updating to the latest version (0.20.0) i have an issue where when i switch my body from static to dynamic with Matter.Body.setStatic(ball, false); it disappears. When looking at the body it seems like the values of its properties are set incorrectly. The body seems to be still there because the collision logic still triggers. I think the body just shoots off outside of view.

When adding the body with isStatic: false it spawns and behaves as it should.

I really don't know where to go from here.

Here is a basic example of the issue:

        const Engine = Matter.Engine,
              Render = Matter.Render,
              Runner = Matter.Runner,
              Bodies = Matter.Bodies,
              Composite = Matter.Composite;
        const engine = Engine.create();
        const render = Render.create({
            element: document.body,
            engine: engine,
            options: {
                width: window.innerWidth,
                height: window.innerHeight,
                wireframes: false
            }
        });
        const ball = Bodies.circle(400, 200, 50, {
            isStatic: true, // change this to false and it works
            render: {
            	fillStyle: 'blue'
            }
        });
        Composite.add(engine.world, ball);
        runner = Runner.create();
        Runner.run(runner,engine);
        Render.run(render);
        document.addEventListener('click', function(event) {
            Matter.Body.setStatic(ball, false);
            console.log(ball);
        });
@0yz 0yz changed the title Body stops rendering when switching from static to dynamic Body properties are incorrect after switching from static to dynamic Jul 4, 2024
@ggorlen
Copy link

ggorlen commented Jul 7, 2024

See this Stack Overflow snippet for another reproduction of the issue. The snippet is currently running 0.19.0. When the box hits the static platform, the platform falls when struck by disabling static with Matter.Body.setStatic(wall, false). Behavior is as expected. But if you bump the version to 0.20.0, the platform disappears when struck, a clear bug.

@a-studio-named-desire
Copy link

a-studio-named-desire commented Jul 18, 2024

Just ran into the same issue with version 20. I saw that the Body.setStatic = function(body, isStatic) on line 1887 of matter.js v20 has a line of code that is causing the issue. After I removed if (!part.isStatic) { the shapes worked as before; if the shape changed to non-static it retains its properties, This avoids the conditional check that could skip setting isStatic properly.

@hylarucoder
Copy link

For users upgrading from version 0.19 to 0.20, some code may exhibit unexpected behavior and break.

You can try this

const textBody = Matter.Bodies.rectangle(
  pivotX + textWidth / 2,
  pivotY,
  textWidth,
  textHeight,
  {
    isStatic: true, // remove this
    friction: 0.8,
    frictionStatic: 0.9,
    restitution: 0.4,
    density: 0.001,
  }
);

Matter.Body.setStatic(textBody, true); // add this

@ggorlen
Copy link

ggorlen commented Dec 10, 2024

@hylarucoder Surprisingly, that does seem to make a difference! Nice find.

Nonetheless, the behavior of the falling body is still quite a bit different (and less desirable) in v.20 relative to v.19. It may be a different issue, but if you apply that setStatic approach to this snippet, the green bar falls much faster in v.20 than v.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants