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

Way to stop engine and renderer when simulation is complete #343

Closed
platinumsink opened this issue Jan 7, 2017 · 3 comments
Closed

Way to stop engine and renderer when simulation is complete #343

platinumsink opened this issue Jan 7, 2017 · 3 comments
Labels

Comments

@platinumsink
Copy link

platinumsink commented Jan 7, 2017

I'm relatively new to functional programming style. I have successfully set up a simulation on the web using matter js. It works well in its current state. The simulation reaches a state when all bodies are sleeping and will never interact again.

What I want to do is determine that all bodies are sleeping and stop the engine and renderer when this occurs.

I see in #313 that there is a filter for determining if all bodies are sleeping. What is the best way to use this filter? Would it be best to attach it to one of the events? I have tried afterTick and afterRender but I'm not sure what is the best way to do this and I'm honestly a little confused about the provided events.

I also want to draw text and other shapes on the canvas after the simulation completes. The text and shapes do not need to interact with the bodies, they are only an overlay. It seems that I have to use the Runner to stop the engine before I am able to draw anything else to the canvas outside of matter. This is why I want to stop the simulation when all bodies come to rest, halt the renderer, and then draw the things I want to the canvas.

Or perhaps I am mistaken about how to draw to the canvas. Why is it that I can only draw to the canvas when I stop the renderer? I was originally working with the default Render engine. I saw your recommendation in several places that I should copy it and make modifications to it. I have done this so I can modify how the background is set up. I'd like to figure out how to successfully overlay things now and I'm very stuck.

Thanks for this awesome project. It is great work!

@liabru
Copy link
Owner

liabru commented Jan 10, 2017

I see in #313 that there is a filter for determining if all bodies are sleeping. What is the best way to use this filter?

Yeah you should use it in afterTick and have it call Render.stop(render) when it returns true.

Why is it that I can only draw to the canvas when I stop the renderer?

Are you drawing once or on every frame? Because the renderer clears itself before every frame even if there's nothing happening, so what ever you draw externally will get cleared unless you draw it every time in the afterRender event. Maybe that's the issue?

@platinumsink
Copy link
Author

Thanks for the response. I was drawing before starting the renderer.

After looking into the source for the renderer I realized that it was clearing itself every frame. I ended up modifying the renderer so it would also redraw my other items after the other physics stuff. Would it be considered best practice to put this in an afterRender event?

@liabru
Copy link
Owner

liabru commented Jan 17, 2017

Yep, exactly since at that point the renderer has finished its own work.

@liabru liabru closed this as completed Mar 7, 2017
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