How to prevent fast moving objects going through other objects in matter.js?

1.1k views Asked by At

Has it been figured out yet? Does anyone know how to solve that problem? I was looking on the internet but there seems to be no solution for that. The only things I found were old an unfinished proposals.

I tried to stop the body if it starts moving too fast or is out of bounds:

var frame = () => {
    
    if(condition){
        this.Body.applyForce(body, position, force);
    }   
    
    window.requestAnimationFrame(frame);
}
   
frame();

But I don't know how much force to use to stop it. I tried to use velocity as an anti force but without the proper values for position argument I couldn't succeed.

Where do I take the right values for an anti force from and what would be values for position argument to apply it correctly?

And how to apply anti force on a collision with a certain object like a wall? Also does applying anti force on collision with the wall make any sense? Do object that pass through the objects that are walls even collide with them?

Kind regards, Slawek

1

There are 1 answers

0
Trystan Rivers On

Your best bet would be to either:

  1. Increase the size of the colliding objects.

or

  1. Use engine.positionIterations = x (where engine = Matter.Engine.create()) to increase the position calculations per iteration and engine.velocityIterations = y to increase the velocity calculations per iteration. The defaults are 6 and 4 respectively.

Note that changing these will affect performance, so a little tweaking is necessary.

These properties can be found at the documentation.