Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible restitution/force bug #394

Open
And-re opened this issue Mar 22, 2017 · 10 comments
Open

Possible restitution/force bug #394

And-re opened this issue Mar 22, 2017 · 10 comments
Labels

Comments

@And-re
Copy link

And-re commented Mar 22, 2017

I'm applying some force to 6 circles in a world without any gravity.
I'm rerunning the same code multiple times and I get 3 completely different results (the last one is at the end).
http://recordit.co/Amjrsw7ct9

Source code (you may need to adjust the value of defaultForceY):
http://codepen.io/and_re/pen/YZaBXV?editors=0010#0

@liabru is it because circles aren't normal circles but approximated using polygons?
http://brm.io/matter-js/docs/files/src_factory_Bodies.js.html#l121

@liabru
Copy link
Owner

liabru commented Mar 27, 2017

Strange. My guess is that it has to do with slight fluctuations in FPS for a few frames after page load (e.g. while the browser is doing stuff like JIT), which will cause different results because Matter.Runner has a dynamic timestep by default (something I'd like to improve, but it's very hard to eliminate totally).

Try starting the runner after letting the framerate settle for a second or two:

setTimeout(function() {
   Runner.run(runner, engine);
}, 2000);

Seems to give the same result every time for me after doing that. Alternatively use a fixed delta for the runner:

var runner = Runner.create({
  isFixed: true
});

Runner.run(runner, engine);

In fact this shows there's a pretty huge difference! I had to increase the force to make them reach the bottom, but it remains consistent every time.

I wonder if I should somehow integrate these findings into the runner or examples.

@liabru liabru added the improve label Mar 27, 2017
@And-re
Copy link
Author

And-re commented Mar 27, 2017

@liabru Thanks for the reply. This is just an example to present the problem using only restitution (no custom mass, friction, etc.). I have the same problem in my game that isn't using Matter.Runner but Matter.Engine only.

I tried playing around with slop, friction, setting restitution to static bodies, etc. but I couldn't find any solution.

I don't see such a problem in P2: http://codepen.io/and_re/pen/evrgLG but I had to set stiffness to Number.MAX_VALUE
Is there something like stiffness in Matter JS? Slop = 0 wasn't helping.

@liabru
Copy link
Owner

liabru commented Mar 28, 2017

@liabru Thanks for the reply. This is just an example to present the problem using only restitution (no custom mass, friction, etc.).

Can you explain exactly what the problem is then? Since with 0 restitution they should always just hit the wall and stop. Is it that they move differently?

I have the same problem in my game that isn't using Matter.Runner but Matter.Engine only.

How are you calling Matter.Engine.update? With a fixed value for delta?

@And-re
Copy link
Author

And-re commented Mar 28, 2017

I'm expecting circle bodies with restitution 0, 0.5 and 1 to bounce like this:
screen shot 2017-03-28 at 22 25 32
(1st case in this gif http://recordit.co/Amjrsw7ct9)
But they are randomly not bouncing at all (behaving as if they have suddenly 0 restitution)

I'm using https://github.com/sethvincent/gameloop on the server (delta in ms):

const gameloop = createGameloop({
    fps: 60,
})

gameloop.on('update', function (delta) { 
   Matter.Engine.update(engine, delta * 1000)
})

@liabru
Copy link
Owner

liabru commented Mar 28, 2017

Is delta guaranteed to be fixed or does it vary? Otherwise the issue exists there too and the same possible solutions apply as above I think.

@And-re
Copy link
Author

And-re commented Mar 28, 2017

From this file it looks like delta is always consistent (the same value): https://github.com/sethvincent/gameloop/blob/master/index.js

But is it really possible that even a small difference in delta could cause 3 completely different results as you can see on my gif?

@And-re
Copy link
Author

And-re commented Mar 28, 2017

I just updated my codepen according to your suggestions but the problem still exists:
http://imgur.com/a/qUzdo
I expect the 3rd and the 5th circle bodies to bounce more like 4th and 6th

@liabru
Copy link
Owner

liabru commented Mar 28, 2017

Thanks for the update! Looks like you are right and I think I've tracked down the cause. It appears to be because the resolver considers slow collisions to be 'resting' under a certain threshold. It's possible to lower the threshold and it seems to fix the issue:

Matter.Resolver._restingThresh = 1; // default is 4

It's entirely possible that the default is set a little too high, so maybe I need to review it. Tell me if that works for you.

@And-re
Copy link
Author

And-re commented Mar 29, 2017

@liabru Thank you so much :D
Using:

Matter.Resolver._restingThresh = 0.001

really helps.

If you are curious why it was so important for my game you can check it out here (it's still under development):
https://haxarena.herokuapp.com/ (EU server)
https://haxarena-us.herokuapp.com/ (US server)
You move with arrow keys and kick a ball with X or Spacebar.

@liabru
Copy link
Owner

liabru commented Mar 29, 2017

No problem, neat game! Send me a message when you release it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants