Unexpected loss of energy in frictionless perfectly elastic setting
See original GitHub issueI’m using a modified version of the beachBalls example, with no friction, no air friction, and restitution of 1. When I let it run in the demo window, the balls eventually stop bouncing and come to a stop. I would expect them to keep bouncing indefinitely.
(function() {
var World = Matter.World,
Bodies = Matter.Bodies,
Composites = Matter.Composites;
Example.frictionlessBeachBalls = function(demo) {
var engine = demo.engine,
world = engine.world;
// need random initialization
var stack = Composites.stack(0, 100, 3, 1, 20, 0, function(x, y) {
return Bodies.circle(x, y, 75, { restitution: 1, friction: 0, frictionAir: 0, frictionStatic: 0 });
});
World.add(world, stack);
};
})();
I also tried setting the restitution of the world boundaries to 1 (the code below is modified from the Demo.reset
, and that did not help.
World.add(world, [
Bodies.rectangle(400, -offset, 800.5 + 2 * offset, 50.5, { isStatic: true, restitution: 1 }),
Bodies.rectangle(400, 600 + offset, 800.5 + 2 * offset, 50.5, { isStatic: true, restitution: 1 }),
Bodies.rectangle(800 + offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true, restitution: 1 }),
Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true, restitution: 1 })
]);
I thought this might be an approximation issue when computing the effects of collisions and set high numbers for positionIterations
, velocityIterations
, and constraintIterations
.
demo.engine.positionIterations = 1000
demo.engine.velocityIterations = 1000
demo.engine.constraintIterations = 1000
This did not help either.
Is this loss of energy due to numerical approximation errors, or can this energy loss be explicitly controlled? At the moment beyond adjusting restitution and friction I am unable to find ways to make energy conservation perfect.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:24 (8 by maintainers)
Top GitHub Comments
@karimshalapy thanks for sharing!
This is still on my list of improvements I’d like to make (quite a long list though!)
For now my suggestions are the following as discussed earlier in the thread:
body.friction = 0
andbody.frictionAir = 0
body.inertia = Infinity
body.slop
Engine.update(engine, 1)
and call multiple updates per frameengine.positionIterations = 20
andengine.velocityIterations = 20
Body.setVelocity(body, Vector.mult(Vector.normalise(body.velocity), speed))
I wanted to do a collision simulation, and a Brownian motion simulation but the energy loss in a fully elastic world is recognizable and the system becomes idle fairly quickly especially when a ball hits a stationary wall. In order to get the desired outcome, I had to implement the physics logic in the “collisionStart” event on the engine.
And here’s what I did to achieve the Brownian motion…
Add some walls
Then I add balls at random locations and set a random initial velocity to each one.
And here are the Helper functions:
The function
calcElasticCollision
calculates the final velocity after an elastic collision according to the equations: For more information search Elastic collisions or check the equations for a brief explanation on the wiki hereDoing this will provide you with a fully elastic collision system that never loses energy, this would work with one-dimensional collision too, but too complicated stuff I have no guarantees it’ll work. Anyway, this is my workaround and I hope this helps someone who needed the same thing I did, and I hope this issue is solved and closed soon enough.
Regards,