Matter-js: Slow Simulation Down After Engine.update()?

Created on 6 Jun 2019  路  5Comments  路  Source: liabru/matter-js

I have had success speeding up my simulation using Engine.update(engine) but I cannot figure out how to slow it down again after I speed it up. I've tried to create a copy of my original engine and replace the sped up one with original using Engine.merge(), but that deletes all of my event handlers for collisions making it worthless. As pointed out in issue #303 when you try to use the timeScale feature the physics engine breaks down, so I would prefer not to use it. If there is a good way to slow down time once it has been sped up with Engine.update(), or just skip forward in time I would love to know about it - I want people to be able to speed up my simulation but also slow it back down after some time has passed.

need-more-info question

All 5 comments

I'm not sure I follow what you mean, Engine.update(engine, delta) lets you control time as you need by changing delta?

Allow me to elaborate - using Engine.update(engine, delta) only seems to update the delta for one frame. Using setInterval() or something mentioned here will properly speed up the simulation, and each time this function is called the simulation will get faster and faster. My issue originally was figuring out how to reset these setInterval() calls - what I ended up doing was keeping all of the intervals in a list and then calling clearInterval on each interval in that list when I want to reset the speed. I can provide some code if that would help to explain better.

setInterval() schedules a repeating callback. If you call it multiple times, you are running many simultaneous, repeating callbacks. This will appear to speed up your engine because you are running it two or more times per frame!

I think that in your program, you need to call clearInterval() first to destroy the old callback before creating a new one.

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

I figured out a solution that seems to work for me similar to this, I apologize for not mentioning it earlier. Every time a user wants to speed up the simulation setInterval() is called making the simulation appear to run faster with each interval id being stored in a list, and when they want to slow it back down again clearInterval() is called on each interval id in the list. I think clearing the interval each time would also work.

@jonahrosenblum I noticed there is a demo for time scaling
https://brm.io/matter-js/demo/#timescale
combined with a hardware independent simulation parameters adjustment like the issue I link, I think that could be a more homogeneous way of speeding up simulations in all devices as it is from the hardware loop.
not sure if this helps you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

probityrules picture probityrules  路  4Comments

koko236 picture koko236  路  3Comments

mrspeaker picture mrspeaker  路  3Comments

liabru picture liabru  路  3Comments

drachehavoc picture drachehavoc  路  4Comments