How to do horizontal screen wrapping with nphysics2d?

It, uh, seems like this discourse server doesn’t get a whole ton of traffic… but I’ll ask anyway I guess :slight_smile:

I’m making a 2d platformer w/ nphysics, and I want to have the world wrap horizontally. In a previous iteration of the game where I implemented my own physics engine this wasn’t too hard to achieve – if an object is close to the left or right “world bounds”, in addition to having them collide with objects normally, translate them for the “wrap” and have them collide with things on the other side.

Is there a way to do this in nphysics? The best solution I can think of is, when an object gets close to the border, add a “shadow body part” that is the wrapped version, so that any forces on that body part also apply to it. But I think that might get weird with angular velocity & torque.

Hi! Yeah, there is more traffic on our discord server than on discourse but I tend to read/answer some questions here about once a week though. Sorry for the delay!

What you are trying to achieve does not seem quite easy. I searched bit how people would do this with other physics engines like Box2d, but did not find any ideal solution. I guess some things that could be tried are:

  • Replicate part of the world’s ground on the right and on the left of your scene. That way, collision will work as expected and all you have to do is to teleport your player’s body to the other side of the screen when it goes beyond a certain point.
  • Perhaps you could use two bodies as you suggested and link them with a CartesianJoint to synchronize their orientations.
1 Like

Thanks so much for taking the time to look into this! :slight_smile: I guess I’ll stop by the discord server with future questions, thanks for the tip!
This feature does seem quite difficult to implement :sweat_smile: so I think I’ll shelve it for the moment while I try to get other things working.