You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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 theColliderComponent
classBody.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
ColliderComponent
and you try and dispose the entity theColliderComponent
callsBody.destroyFixture(fixture)
in itsdispose
method this seems to cause the game engine to crash as well. My solution to this problem was to just comment outphysBody.destroyFixture(fixture)
inColliderComponent
'sdispose
method 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-Enemy
this is my 'fixed' versionThe text was updated successfully, but these errors were encountered: