How to create a wrapping world?

Hi,

My world is about 30 high by 1000 wide, the camera can see a section of the world about 80 wide. I need the world to effectively 'wrap' - eg objects which go off the right hand end show up on the left hand end seamlessly. I've tried a few tricks with teleporting objects back and forth but is there a better way?

Cheers.
Why don't you use a boundary listener?
how would that work? If an object wraps back onto the screen it will appear to pop into existence...
You mean a really seamless torus world? That would be a bit complicated. You also have to think about your camera movement. Maybe you can add a short margin that is at least the width of your largest dynamic object (that you want to wrap). A piece of pseudo code says more than 1000 words:

Code: Select all

if(obj.x < world.x + margin) {
world.createObject(obj, vec2(world.width - (world.x - margin - obj.x)));
duplicatedObjs ~= obj; // append
}
// same with other side (or y. but be careful: you may have up to 4 copies at a time)
...
//somewhere else:
foreach(obj; duplicatedObjs) {
if(obj does not collide with any margin) {
duplicatedObjs.remove(obj);
}
}


But you may want to make the copies proxies for the original so that collision affects all of them.
We actually talked about this a while back (like over a year ago, and I can't find the post now), and IIRC it looked like in order to do it right, we'd really need to have a joint that locks two (or a set of) bodies to move in lock-step with each other. You'd then clone your bodies and maintain clones on the domains neighboring the fundamental one, adding/deleting/re-cloning them as necessary to keep the fundamental domain and neighbors populated.

Even that's not 100% perfect, as it relies on the contact solver to impose periodicity and it might still fail on very large objects, but it would be pretty darn close, at least as good as Portal's physics was through the portals. Pretty much any simpler solution is likely to fail because stacks and islands won't solve properly across the boundaries.

Nobody's set this up, to my knowledge, though it's a very straightforward joint to write if you know how to do that sort of thing.
ewjordan wrote:We actually talked about this a while back (like over a year ago, and I can't find the post now), and IIRC it looked like in order to do it right, we'd really need to have a joint that locks two (or a set of) bodies to move in lock-step with each other. You'd then clone your bodies and maintain clones on the domains neighboring the fundamental one, adding/deleting/re-cloning them as necessary to keep the fundamental domain and neighbors populated.


Wouldn't this cause problems, in that the effective mass of a cloned object would be scaled by the number of copies currently locked together? If a body is locked to a duplicate of itself, it will feel twice as heavy to the solver.

Really you don't even want 4 different bodies, you should need only a single body state, interpreted in four different ways -- and "interpreted" means simply that the position is shifted. Orientation, velocity, etc could all be left alone since wrapping should have no effect on them.

Of course I'm not clear on how this could be nicely implemented, aside from adding code in all places where you calculate the vector from one position to another (to make sure you always consider the shortest-path to be the actual delta). You'd probably also want to use a grid/hash for collision since this can handle wrapping spaces naturally.

I really hope someone figures this out one of these days 🙂
raigan wrote:Wouldn't this cause problems, in that the effective mass of a cloned object would be scaled by the number of copies currently locked together? If a body is locked to a duplicate of itself, it will feel twice as heavy to the solver.

My thought was that since each body will be locked to the same number of duplicates (a constraint which would have to be very carefully imposed), the effective mass ratios will still come out to be equal. But even so, we'd have redundancy in the solver, for sure, and I don't know off the top of my head what unforeseen effects that might cause...

There will always be certain situations that no bounded number of duplicates could account for, as well - imagine one really long toothpick sitting in the world at an angle, it could wrap around the torus as many times as we want, and up to a constant, the number of wraps would be the number of domains/copies we'd need to consider to get the dynamics right.

Really you don't even want 4 different bodies, you should need only a single body state, interpreted in four different ways -- and "interpreted" means simply that the position is shifted. Orientation, velocity, etc could all be left alone since wrapping should have no effect on them.

Of course I'm not clear on how this could be nicely implemented, aside from adding code in all places where you calculate the vector from one position to another (to make sure you always consider the shortest-path to be the actual delta). You'd probably also want to use a grid/hash for collision since this can handle wrapping spaces naturally.

Yes, that would be ideal. The problem would be, as you mention, figuring out where to get this done.

I'm kind of half wondering whether modifying the vector math ops might be able to account for this; the issue there is that Box2d doesn't distinguish between vectors in position space and vectors in the tangent space, and the equivalence should only apply to those in position space.

I think ultimately to get something like that to work it would probably take a lot of internal edits, so it might not be plausible without someone really interested/committed/knowledgeable stepping in to see it through; further, I don't know that Erin would want so much special purpose code getting in to the trunk, as it would complicate later additions quite a bit.

Another possibility I'm considering is to subclass b2World and b2Body and bake in all sorts of special handling code to deal with wrapped worlds, manually updating the broadphase and patching up the contact graph as necessary. This would require re-writing a lot of the solver routines and whatnot, and manually checking collisions over the boundaries, but it just might be possible to do it with some work, or at least do it well enough to approximate the right behavior (let's ignore cases like the really long toothpick).

In the end, after all, the most common reason for needing something like this is probably just to have a simple asteroids-like world. I don't think most people are looking for perfect solver behavior across the boundaries, or stacks that wrap around the world several times.
I don't know if this is helpful or not, but Jumpman:
http://msm.grumpybumpers.com/?p=24
has wrap-around physics implemented with Chipmunk. I haven't looked into how Chipmunk works though.
ewjordan wrote:each body will be locked to the same number of duplicates


Ah, I didn't realize that the duplicates were permanent -- I figured they would be created/destroyed when the objects entered/left the region around the boundary.

ewjordan wrote:In the end, after all, the most common reason for needing something like this is probably just to have a simple asteroids-like world. I don't think most people are looking for perfect solver behavior across the boundaries, or stacks that wrap around the world several times.


Yeah, I agree that it would be best to just impose limitations like "no object larger than half the level size" in order to avoid headaches.
The problem with a full physics engine is you need solutions that don't break the physics. Simple wrap around of objects doesn't fufill this (as something may be in contact with shapes on both sides). And yet, for the vast majority of games it's sufficient, providing the designer is aware. You see the same pattern for a lot of things that *could be* implemented in Box2D - the lightweight, sensible solutions have to be left out, or given second class status.

Original thread: https://box2d.org/forum/viewtopic.php?t=3174