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

Matter.Body.applyForce changes based on Runner.delta

Open luveti opened this issue 1 year ago • 0 comments

Given the following example:

<html>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/matter.min.js"></script>
    <body></body>
    <script>
        const engine = Matter.Engine.create()

        const circle = Matter.Bodies.circle(20, 300, 10)

        Matter.Composite.add(engine.world, [
            circle,
            Matter.Bodies.rectangle(400, 5, 800, 10, { isStatic: true }),
            Matter.Bodies.rectangle(400, 600 - 5, 800, 10, { isStatic: true }),
            Matter.Bodies.rectangle(5, 300, 10, 600, { isStatic: true }),
            Matter.Bodies.rectangle(800 - 5, 300, 10, 600, { isStatic: true }),
        ])

        const render = Matter.Render.create({ element: document.body, engine })
        Matter.Render.run(render)

        const runner = Matter.Runner.create({
            delta: 1000 / 60,
            // delta: 1000 / 120,
        })
        Matter.Runner.run(runner, engine)

        Matter.Body.applyForce(circle, circle.position, { x: 0.01, y: 0 })
    </script>
</html>

When delta = 1000 / 60 the circle lands half way across the box. When delta = 1000 / 120 the circle lands quarter way across the box.

Is this the expected behavior? It seems like every force needs to be doubled, but I would expect the engine to handle this internally. My goal with lowering the delta was to add more stepping of the simulation to prevent objects from phasing through each other.

luveti avatar Jul 19 '24 05:07 luveti