In the game I'm trying to build, I have a flame gameplay effect. Its semantics are quite simple: Apply damage to all entities that are inside the flame.
Assume that the flame is described as a circle and never moves or changes in any way once created (other than being destroyed at the end of its lifetime). The only thing the flame cares about are all bodies/fixtures that are overlapping with the circle. What is the best way to get this information?
My first attempt was to simply perform queries every frame to see what is overlapping with the circle and apply damage to all those entities found during the query. This works, but the performance becomes quite slow once many flames overlap each other (flames have no effect on other flames). My second attempt was to make the flame a sensor, set a collision filter such that it ignores collisions with other flames, and each flame maintains a list of game entities that are overlapping. To maintain this list, I utilize the BeginContact() and EndContact() callbacks to add/remove from the list. However, I've found that there is a rare event which causes a serious logic error (and occasional invalid memory access) which I suspect is due to a race condition in my handling of the BeginContact() and EndContact() events. If this race condition does indeed exist and is the cause of my problems, I'm not even sure how to fix it without going to extraordinary lengths to rearchitect my code to avoid it.
Does Box2D already maintain a list of all overlapping bodies/fixtures for a particular body? It seems like Box2D would already have such a list and would be available for inspection by the client.
Assume that the flame is described as a circle and never moves or changes in any way once created (other than being destroyed at the end of its lifetime). The only thing the flame cares about are all bodies/fixtures that are overlapping with the circle. What is the best way to get this information?
My first attempt was to simply perform queries every frame to see what is overlapping with the circle and apply damage to all those entities found during the query. This works, but the performance becomes quite slow once many flames overlap each other (flames have no effect on other flames). My second attempt was to make the flame a sensor, set a collision filter such that it ignores collisions with other flames, and each flame maintains a list of game entities that are overlapping. To maintain this list, I utilize the BeginContact() and EndContact() callbacks to add/remove from the list. However, I've found that there is a rare event which causes a serious logic error (and occasional invalid memory access) which I suspect is due to a race condition in my handling of the BeginContact() and EndContact() events. If this race condition does indeed exist and is the cause of my problems, I'm not even sure how to fix it without going to extraordinary lengths to rearchitect my code to avoid it.
Does Box2D already maintain a list of all overlapping bodies/fixtures for a particular body? It seems like Box2D would already have such a list and would be available for inspection by the client.