This project has moved and is read-only. For the latest updates, please go here.

Handling physic bodies for a "wrapping" world

Topics: Developer Forum
Oct 14, 2007 at 4:35 AM
I previously posted this question as a follow up to another discussion - but thought I might have better luck starting a new discussion and I had some ideas since.

The scenario: A top-down space game where the player can travel around in a seemingly infinite space, but in reality, the defined space just wraps upon itself after a defined distance.

The tricky part is to get the right collision response correct for the case where a physics Body is straddling the edge of the world or universe (and hence wrapping to the other side). Worst case, the Body straddles where the 4 points of the universe meet.

My idea is this: Create some "shadow" Bodies that are typically disabled. When a Body is on the threshold of the universe edge, a "shadow" object is enabled and set-up on the other-side of the universe (worst case, you have 3 shadow objects plus the original). The "shadow" bodies are initialized with the correct AngularVelocity, LinearVelocity, etc.

When physicsSimulator updates, the "shadow" bodies may affect and be affected by other bodies. At this point, we take all the shadow objects and somehow composite the results for the single body.

Finally, when the body completely enters the universe again (ie, not on an edge), all the "shadows" are disabled and the "real" body is in the correct position, etc.

Could this work? Is it a good method for handling this, err... edge case? And what are the changing variables / parameters of the "Body" object that should I worry about (LinearVelocity, AngularVelocity, etc.?)

Thanks a lot and in advance!
Oct 16, 2007 at 3:50 AM
So if anyone is interested, I got this to work after some hacking. As a test, I took the second demo and modified it so that the universe-space wraps with the screen.
Here is what I did:

In the demo itself, I added code to:
  • Create disabled "shadow" geometry.
  • Detect when the AABB of the "real" geometry gets near the edge of the universe (in this case, the screen). In this case, its "shadow" geometry is enabled on the other side and initialized with all of the current physics parameters (except position of course)
  • Detect when the AABB of the geometry completely leaves the universe. In this case, the "shadow" geometry is disabled and the real object jumps to where the "shadow" was. Now things progress as normal.

In the physics, I had to hack a few things. In a nutshell, I made the CollisionHandler pass everything in by "ref" so that I could "swap" out "shadow" geometry when it was encountered with the "real" geometry. (This required some changes to the Arbiter as well). I also made sure the contacts were positioned to the real object.

The real object drives the shadow object during the border transition.

Anyway, it seems to be working - but I'll need to test with more objects and clean up some code so that it isn't so hackish.
May 29, 2008 at 2:55 PM
I've noticed that this question has come up before (  I too am in the same boat, and would LOVE to see what you did sonofdaedalus if you have a link.  Has anybody else tried something and gotten it to work? 

I think I might not even deal with this in my current project unless somebody has an easy solution to implement, or a soon to be released future version of farseer has this built in.

I've had a few ideas of my own, but I just don't understand everything that Farseer does to modify it on my own...