-
Notifications
You must be signed in to change notification settings - Fork 5
Description
How the engine class removes physics body's is currently wrong and crashes the game.
currently if you try to dispose a physics entity 'mid-game' the engine class will try to call world.destroyBody(b) on Body b there and then. Box2d cannot remove physics bodies mid step, the world will be locked and the game will crash. The solution[1] is instead of destroying immediately, add the body to a list and call world.destroyBody(body) on all bodies in this list after the world.step() function.
Also in the ColliderComponent class Body.destroyFixture(fixture) is called when the component is disposed however this is not necessary according to the comment of Body.destroyFixture():
If your entity has a ColliderComponent and you try and dispose the entity the ColliderComponent calls Body.destroyFixture(fixture) in its dispose method this seems to cause the game engine to crash as well. My solution to this problem was to just comment out physBody.destroyFixture(fixture) in ColliderComponent's dispose method however there may be a use case where someone wants to dispose just the ColliderComponent
[1] https://www.iforce2d.net/b2dtut/removing-bodies
if you want to see the bug in action you can go checkout 2021-studio-6 branch : S1T5-Task/Long-Range-Enemy this is my 'fixed' version