I propose a new PhysicsWorld
component that could be used to control the global physics properties of the Box2D World class.
This would be similar to having multiple camera components. There would be a default PhysicsWorld component that would have focus, but we could change the PhysicsWorld by sending a message like acquire_world_focus
to one of the components in my collection.
The PhysicsWorld would also support a set_world
message to set Box2D World properties, such as the gravity
(as a vector) and velocity_threshold
(as a number).
PhysicsWorld components wouldn’t actually represent different Box2D World instances, since reparenting bodies would be a major hassle. Instead, they could simply represent a list of properties. When switching between two PhysicsWorld objects, the effect would simply be to instantly change the associated properties (gravity and velocity threshold for now, maybe more later).
Stretch Goal
Inspired by Pkeod’s request, would be the ability to set the context of Collision components to different PhysicsWorld components (rather than a single global World), essentially running separate Worlds simultaneously.
Of course, collision objects could only belong to one World at a time, and you probably wouldn’t want to support reparenting a collision object to a different world at runtime. I imagine there would be serious issues with speed, and confusion over why collisions aren’t detected between objects belonging to different worlds, so it’s probably not very practical, but it would enable some cool background effects, etc.