matter-js
matter-js copied to clipboard
Energy Lose Without Friction and Inertia is Infinity
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
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;
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.
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!
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.
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