More complicated = yes.
But in a pretty modular way.
I was hoping to tidy things up and send you a demo - both for sharing with whomever, and also to hopefully get some feedback on the method.
There are only a couple of minor changes I made to the existing code.
The rest hinges around:
- a pair of new constraint classes
- 4 sensors that form a boundary around your world
- callbacks that handle collisions between your world objects and these sensors
create/delete the appropriate mirror objects
link mirror objects with new constraints
Given a space with some objects in it, I would like to make the world wrapping as clean as possible
makeSpaceToroidal(my_space, parameters)
This adds the appropriate sensors and sets up the callbacks (which are based on collision_type).
Regarding the 2D Portal clone, looking around at existing demos, it seems that all of them implement this as a "teleportation". That is, when a certain criterion is satisfied, the object is removed and replaced somewhere else. This is different that what I am doing. When the object "peeks" through the first portal, I want that part to "peek" out of the other portal.
Advantages:
- objects are not teleported on top of something
- dense systems (or ones that have larger objects) are better simulated as bulk
- looks nice
Here is a screenshot of this wrapping in action for 10 balls.
red = normal
green = paired horizontally
blue = paired vertically
gray = both horizontal and vertical
brown = non interacting - about to be deleted
And 1000 balls is more fun...

- 1000 balls
- Picture 45.png (92.52 KB) Viewed 174 times

- 10 balls in wrapped world
- Picture 39.png (25.8 KB) Viewed 174 times