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
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 callworld.destroyBody(body)on all bodies in this list after theworld.step()function.Also in theColliderComponentclassBody.destroyFixture(fixture)is called when the component is disposed however this is not necessary according to the comment ofBody.destroyFixture():If your entity has a
ColliderComponentand you try and dispose the entity theColliderComponentcallsBody.destroyFixture(fixture)in itsdisposemethod this seems to cause the game engine to crash as well. My solution to this problem was to just comment outphysBody.destroyFixture(fixture)inColliderComponent'sdisposemethod however there may be a use case where someone wants to dispose just theColliderComponent[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-Enemythis is my 'fixed' version