So, unlike SimEthereal where an object can be in multiple zones at once, in MPhys, objects can only be in one zone at a time. After all, it’s the easiest way to make sure you only integrate an object once.
In MPhys, I use the term “bin” instead of “zone” because this is separate from SimEthereal and to me a “bin” signified one bin per object better than zone did. But anyway. Bins. In a BinIndex of active bins… and each bin has a list of its active/loaded neighbors. (Bins are loaded/unloaded as needed.)
Physics generally works in phases, so in my case:
So generateContacts() is the interesting one. It goes through each active bin’s active objects and generates static and sleeping contacts against the objects in that bin and the static and inactive objects in the neighbor bins.
It does a second pass through the active bins and their active objects to generate the active -> active contacts. This also checks active objects in the neighbors (Keeping track of which bins have already been visited to avoid duplication.)
(Note: the code is available as a preview to curious Patreon donors who shoot me their github IDs.)
The whole neighbors/bin thing is the first complicated bit. Loading/unloading bins is the second complicated bit… especially for ES integration. In a normal physics engine you just tell it “Hey, here are my objects”… but with MPhys objects are loaded on demand when the bin is loaded so adding a new object is more like a notification “get the bin at this location and tell it there is this object ID”. The bin will load the object using a factory as needed.
Tricky tricky. So happy it’s all working now. This is like a 2 year project off-and-on, I guess… yeah, wow:
…and that’s not counting all of the stuff that led up to that. I mean, I had a working non-zoned physics engine for a long time. So it’s just two years of my spare time spent thinking about this and moving it along.
Edit: oh, the third tricky bit was making the collision stuff 100% pluggable. You can define any kinds of collision shapes you want… in my case one of the pluggable collision types is block objects/block world. The simple demo just has spheres and boxes.