I'm writing a simulation for an AI project that involves learning. To enable learning, I need to re-start the simulation over and over, with slightly different body configurations. Is there a way of destroying the old engine instance and re-create a new engine instance from scratch?
calling
engine = Engine.create(document.getElementById('canvas-container'));
keeps on adding new engines / renderers without removing the old one.
currently I am trying to reset the world w/o creating a new engine instance as follows:
// create a fresh world to simulate
function simulate() {
World.clear(engine.world);
Engine.clear(engine);
... re-create all my physical objects ...
// bind a callback function to invoke my ai stuff on every frame
Events.on(engine, 'afterUpdate', function() { ... });
}
the problem is I don't know how to clear the callback functions that are bound to the afterUpdate event. what happen is each time I call simulate(), the world and the objects are cleared, but a new call-back function is being added to the "afterUpdate" event, and the number of callback functions keep growing as long as I keep calling simulate.
Is there a way of clearing the event handlers of the engine?
Is there a way of clearing the event handlers of the engine?
Sure if you just want to remove _all_ events on the engine or any object, just do engine.events = {}.
If you need to only remove certain callbacks but and leave others, take a look at the code from Demo.reset.
The solution is to keep track of the event callbacks (e.g. like in Example.events by pushing them into an array, then to reset use Event.off(obj, callback) for every object and callback you have bound. See the Events docs for more info.
In fact, forking the Matter.Demo might be a good place to start since it already has a lot of stuff for experimenting, including resetting.
Also for the record, you can actually have multiple engines just fine, but if you've used Engine.run on them you will need to use Runner.stop(runner) where runner = Engine.run(engine) otherwise they will still be bound to the browser render event.
I'm writing a simulation for an AI project that involves learning
Sounds cool, tell me if you release it somewhere :)
worked the charm, thanks!! I'll definitely update you on the project once I make more progress. much love!
Hello, I have question about this.
I've used the code above, and it seems to work, except that when I recreate the physics objects, the simulation seems to run at twice the speed.
And, each time I reset it, the simulation speed doubles again.
Is there's something I need to do to prevent this?
@kittykatattack
I have not touched matter.js for over two years, but you can look into my repository because I think I have had your problem before. I cannot remember how I resolved it, so this is the most I can help you with.
good luck!!
https://github.com/evanthebouncy/compromise/tree/master/jumpingBox
@kittykatattack sounds like you're not properly stopping your runner and creating new ones? Make sure you only create one and stop / start it as necessary.
you're not properly stopping your runner and creating new ones?
Thank you!
What specifically does this refer to?
(Sorry, I'm very new to Matter JS.)
Most helpful comment
Sure if you just want to remove _all_ events on the engine or any object, just do
engine.events = {}.If you need to only remove certain callbacks but and leave others, take a look at the code from Demo.reset.
The solution is to keep track of the event callbacks (e.g. like in Example.events by pushing them into an array, then to reset use
Event.off(obj, callback)for every object and callback you have bound. See the Events docs for more info.In fact, forking the Matter.Demo might be a good place to start since it already has a lot of stuff for experimenting, including resetting.
Also for the record, you can actually have multiple engines just fine, but if you've used
Engine.runon them you will need to useRunner.stop(runner)whererunner = Engine.run(engine)otherwise they will still be bound to the browser render event.Sounds cool, tell me if you release it somewhere :)