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

Energy Lose Without Friction and Inertia is Infinity

Open Finrodx opened this issue 1 year ago • 5 comments

Hello, this is asked like a million times but; I am trying to make an object bounce infinitely in a no-friction system. The object supposed to bounce is loosing speed after a couple bounces. This is my bouncy object:

   matterCreateBallBody(posX, posY, radius)
   {
        let newBody = this.Bodies.circle(posX, -posY, radius, {
            friction: 0,
            frictionAir: 0,
            restitution: 1,
            density: 0.001,
            intertia: Infinity
        })
        this.movingBallBodies.push(newBody)
        Composite.add(this.engine.world, newBody)

        return newBody
    } 

My surrounding bodies are crated by following piece:

    createEdge(posX, posY, width, height, widthZ, addMesh)
    {
        let edge = this.Bodies.rectangle(posX, posY, width, height,{             
            density: 0.001,
            isStatic: true
        })
        Composite.add(this.engine.world, edge)
        if(addMesh)
        {
            this.addBoxMesh(posX, posY, width, height, widthZ)
        }
        
        return edge
    }

and this is how I setVelocity to object:

    throwBall()
    {
        this.movingBallBodies.forEach(ballBody => {

            MATTER.Body.setVelocity(ballBody, {x: 3, y: -3})
            //MATTER.Body.setAngularVelocity(ballBody, 0)
        });
    }

I am suspecting size of body might be issue? All examples I come across uses 10 to 100 times bigger sizes than I am using. I also messed with some playgrounds and I cannot reproduce the issue: This is what I am trying to achieve

Only difference I am seeing is body sizes. Sometimes body goes thru edges but I guess thats a stepsize issue and counts as an another issue.

Edit: codepen uses 0.11.0 my code using latest 0.18.0

Finrodx avatar Jul 18 '22 08:07 Finrodx

Adding this line seems like fixed the problem for me, for now. Beware this effects every object in the scene. Slower speeds snapping still is a problem.

MATTER.Resolver._restingThresh = 0.001;

Finrodx avatar Jul 25 '22 07:07 Finrodx

That could be it, the built in constants / thresholds are currently tuned for objects roughly at least 10 x 10 I think.

So you're likely best using that scale for bodies if you can, then scaling your rendering instead, unless tweaking the constants is working out.

liabru avatar Jul 27 '22 13:07 liabru

Hmm. After some tests making objects and entire screen definitely has effect and changes how physics work.

Well I am actually using three.js to render. Bodies and meshes are working like mapped.

btw Engine is amazing. thanks!

Finrodx avatar Jul 29 '22 07:07 Finrodx

I'd be interested to know if you were rescaling the bodies when you scale the screen before? I think the end effect of some properties like friction and slop can vary based on mass / scale. So rather than that you probably want to scale rendering instead.

liabru avatar Jul 29 '22 16:07 liabru

Well render engine gets positions from matter bodies. Matter render is off. So I dont think I am tested that. I am not setting any mass and I wasnt rescaling bodies. It was just an urge that come after I read other issues

Finrodx avatar Aug 02 '22 07:08 Finrodx