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

PhysicEngine body disposal bug #8

Open
yourlow opened this issue Aug 20, 2021 · 2 comments
Open

PhysicEngine body disposal bug #8

yourlow opened this issue Aug 20, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@yourlow
Copy link

yourlow commented Aug 20, 2021

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

@yourlow
Copy link
Author

yourlow commented Aug 20, 2021

@henry-burgess

@a-coox
Copy link
Contributor

a-coox commented Aug 24, 2021

Interesting bug! I think this'll only show up if you're destroying a physics body in response to a collision event, since this is the only custom code that runs during a physics timestep. This is a common use-case when destroying an entity as a result of it getting attacked, so I think it warrants fixing.

Trying to delete an entity mid-game in the update() loop can cause a similar issue since the entity list is changed while it's being iterated over. A nice solution could be to add an Entity.Destroy() method which, similar to your solution, maintains a list and clears the entities after the world step and updates run.

@applebyter applebyter added the bug Something isn't working label Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants