Is a closed-bubble, looping world possible in Unity?

And by possible, I mean “relatively easy without jumping through hoops.” :wink: The intention is something akin to a globe, where you can send something flying off to the right and it’ll some back in from the left, or shoot forwards and it comes up from behind having loop around the game universe. Viewed from top down, the appearance would be a 2D universe that loops, and ideally the universe would wrap to fill the screen, so an object travelling across the horizon would be drawn split in half, at the top and bottom of the screen.

In traditional 2D, you’d say if object y_position < 0, move to bottom of screen and such, but if you want to show the object straddling the screen borders, you’d draw it twice.

I’m looking at Unity as someone with diverse experience with plenty of languages including Android SDK, and it’s looking very promising, but I don’t want to commit to something that’s going to give me headaches like every other coding project I’ve undertaken. So instead of trying to create a vision specific in design, I want to design to the natural limits of Unity. PhysX is provided - great, I’ll use that. Can it do physics in a loop as I request? If so, I’ll design my game around that. If not, or if “yes, but you’d need to invest weeks coding and bug testing and growing your hair so you can pull it out”, I’ll design my game differently to make it easy. :wink:

What do you mean by “jumping through hoops”? Everything’s possible, it’s a matter of programming it. How easy it is depends on your programming ability.

–Eric

The question is how naturally supported is it? eg. The simplest solution is an engine-level flag “loop world.” The hardest solution is probably duplicating all world items four times, placing them around the player, and rewriting the physics engine to treat four objects as one.

Perhaps I should just ask how can it be done, and then decide whether the solutions are too complex for me or not? :wink:

1 Like

You could just move them to one side of the screen when it reaches another? But that should be possible on any engine, I doubt you would ever need to rewrite the Physics engine on any.

You could jump the object to the opposite side of the world, but that will look jarring. The obvious fix to that is to only jump the object when it’s outside the FOV, so it disappears off the left side of the screen before appearing on the right, but that would mean a ‘black area’ where the object is off-screen. Ideally, and I think necessary for this game-layout format to work, the graphic should be drawn in two positions, or even four if it straddles all corners. I suppose duplicating the object on boundaries is possible as reference to tags means it can easily be treated as the same object, but any physical impulses would have to be duplicated across all objects. That is, let’s say an asteroid is straddling top and bottom edges. If it’s collided by an asteroid at the bottom edge, that’d push it down (and through the top of the screen). If it’s shot by a cannon from below, that’d move it upwards. If both event happen at the same time, the result should be a physically accurate combination. If you are duplicating the object, the physics will work on two different objects, pushing one down and one up.

Could you create them with a fixed physics joint between them, and then destroy that joint when reverting to a single object away from the screen edge? Wouldn’t you need to create them with half the mass, and would centre-of-gravity mess up the physics result?

I’ve done this once before in another engine - basically, the key is to synchronize a clone an object that’s going to go off-screen with the actual one, then when it actually crosses over the border, swap the objects to the “real” one is on the other side.

In other words: you should see two copies of an object that’s close to the edges of the screen - one on each side.

Why not just add the duplicate as a child of the old one (with no rigidbody yet). Then all collisions would affect both (due to compound colliders) and there wouldn’t be any duplicated forces. Then just delete the mesh and collider out of the parent (original) when it’s fully out the screen.
Seems pretty simple :slight_smile:

Thanks for answering this in depth. Writing my own physics solver isn’t worth the hassle to me, so that’s the sort of ‘jumping thorugh hoops’ I’d avoid. But I do wonder if there are tricks that could be exploited from within Unity?

That’s the sort of work-around I’m wondering about. Or would it be possible to create cameras that have an overlapping FOV and render a combination, so there’s only one object but it’s drawn twice and composited across the screen edge? Hmm, that wouldn’t work with physics interactions around the screen wrap.

Except if the duplicate is a child of the parent, then wouldn’t any rotation of the parent will cause the dup to rotate ‘around’ the parent pivot point as opposed to around its own pivot point?

Of course you could fix the rotation of the dup yourself every frame, though then both colliders would need to be rigidbodies to avoid nasty static recalculations within the physics engine. I guess the dup could remain isKinematic, but then collisions with it wont give the desired results.

Doesn’t seem that straight forward to me and still wouldn’t address the issues Meh11 brings up if both colliders are affected by different colliders in the scene simultaneously.

Its an interesting challenge though, i’m sure there must be ‘relatively’ straight-forward solution somewhere. Wouldn’t Valve’s Portal games have to address the same issue? Maybe you can find some articles where they discussed their solutions to the issue.

Edit: Bah, took too long thinking my reply through so Meh11 posted the rotation issue before I did :wink:

Edit 2:
Thinking about this some more I wonder if there may be some value in creating a physical wrap-around world so that you only need single instances of all objects, but might have to render them multiple times. I.e a sphere or maybe a torus. The problem then becomes one of how to map the world to a square screen, without distortion. A sphere would be the easiest to work with but I don’t think its possible to map that without distortion, a toriod should map exactly, but then you have fun and games with the physics, especially if you have gravity. Of course you could control gravity yourself every frame for each object, but in practice i’m unsure how robust that would be.

Just posted a longer reply but lost it, so the shortened highlights -

You could use a spherical world with the camera at its centre and FOV that matches the curvature, resulting in a perfect flat projection. Each game element could be orientated so its top faces the world centre. The tricky bit with this seems to be the physics simulation. We’d basically want physics operating in polar coordinates rather than Cartesian. Is that possible with PhysX?

Everything would be difficult, but that’s the fun of it :wink:
Yeah things get messy real quick with non euclidean geometry, but it was the only way I could think of doing it that didn’t require duplicate objects and the physics issue it creates. Of course then you just swap one system with difficult physics for another so i’m not sure there is ultimately anything to gain.

I was also toying around in my head the idea of having a single ‘real’ world and then N dummy worlds (N being the number of side wraps needed so 2 or 4 I guess). You then evaluate all the dummy worlds each frame and update the real world from that, combining all the forces happening on each object in each dummy world etc. But I think as you pointed out there may still be issues in such an approach, plsu you then have to update the dumy worlds with the results of the real world, so things quickly become convoluted I suspect.

I did a quick google search after replying and found various solutions, but none that went the whole way, some ignore physics, others ignored duplicating objects (they just teleported), and some that did physics, only did it in one direction and only for a single object. So i’m guessing its non-trivial to find a solution for this. Again as you said you may well end up writing you own physics system to solve it.

I was thinking of a select window view on the world. The world wraps around the camera, but the camera sees only a typical 16:9 view of it. The trick would be setting camera distance and FOV to define a curved viewport that matches the curvature of the play surface. Every point on the viewed surface would be equidistant from the camera, so rendered with no perspective. I guess you’d want a curved camera view for playing pieces, and a separate orthographic pass for 2D backgrounds and the like. Otherwise you’d need a huge sphere model in high enough detail to look curved, which would be way too costly and inefficient. Rendering separate passes is certainly something Unity seems capable of doing.

I tried a search for polar coordinates in PhysX and drew a blank. If you don’t sweat physics, it’s easy, but there was a game on the 16 bit systems that had wrap-around 2D physics, such that I’m surprised it remains such a dark mystery how to implement! Maybe those developers were just gods among coders who took their secrets to their digital graves?

It should actually be a toroidal world, not spherical… the rectangular camera viewport would work much better.

They weren’t using PhysX, bullet or box2d or anything… and back then you could just wrap the algorithms for pixel comparisons.

It isn’t possible for someone who lacks the knowledge and isn’t willing to devote some time to learn how to do it.

I’d propose this:

each object that needed to wrap would need:
-8 copies of the renderer of the object, set half a screen to the left, right, above, beneath of the object. (imagine a 3 x 3 grid with the main object in the centre)
-each of those 8 having a rigidbody set to “IsKinematic” + copies of the main collider.
-each of these having a fixed joint to the main object.
-Script on the object that references all 8 objects that
-Checks if object is out bounds of screen, if so, disables it.
-applies rotation of main rigidbody to each kinematic rb.
-checks if main object centre crosses the screen boundary, moves it’s position completely to the other side.

This should work for physics, will handle all wrapping cases (including corners, where in theory you could see the object in 4 places simultaneously).

I’d love people to pick apart this suggestion- as I intend to do exactly this in a game of my own :slight_smile:

Hey folks. Okay so I’m not so sure on the physics side of things but I’ve been working on a 2D defender-like shootemup game which wraps around horizontally. The game world is several screens wide. Obviously there are lots of objects moving through space and across the edges of the world so I had to figure out how the heck to make it work.

Just for the case of horizontal wrapping, you need 3 cameras. You assume that a world scroll position of X=0 is aligned to the left edge of the screen, positive coordinates (and the rest of the game world) are to the right. To render this, you need a regular camera. The camera’s going to be centered on the screen so you need to add half a screen’s width and height to its position. This camera moves in order to implement scrolling.

Next you add two more cameras. They’re basically set up the same. However, the 2nd camera should be positioned at a negative WorldWidth offset, ie to the left. The 3rd camera should be positioned with a positive WorldWidth offset to the right. So you’ve got three cameras, one in the middle, one an entire world’s width to the left of it, and one an entire world’s width to the right. The 2nd and 3rd cameras must be child objects of the main one so that they move together.

With this setup, you only need one instance of each game object. The main camera clears the screen and draws anything visible at the main scroll position. When the camera gets too far to the right it will cover the case of objects overlapping the right edge. You still have to use a script to detect when an object’s position becomes >=world width and then subtract world width from it to wrap around. Similarly, when the camera is at the left side, e.g. if you put camera X=0, it will show some world that overlaps the left edge, and that’s captured by the third camera. The 2nd and 3rd cameras need to not clear the screen at all otherwise they will wipe out what the first camera drew. Similarly if an object’s X coord becomes <0 then you must add world width to it to wrap around.

This works perfectly fine but does not incorporate physics. Technically you only actually need 2 cameras to do this when an object’s position (and world scroll position) are always aligned to the top-left edge of the object… but I found that when adding e.g. a second player and you want them to show up properly to the left of the first player, it needs that 3rd camera to cover all the bases.

Non-rotational physics can be done by giving a single game object multiple colliders. E.g. add a spaceship with its own collider, then add two child objects each with a collider but not a renderer/mesh. The 2nd and 3rd collider objects are positioned at a world’s width to the right and to the left, similar to the camera. That accounts for odd wraparound effects at the edges of the world where the collider kind of needs to be in two places at once.

I presume this would all work vertically as well, although in that case I think you’ll need 9 cameras to do X and Y wrapping. Same principle, cameras above and below by the height of the world. Similarly for physics you’d need to have 9 colliders per object to capture all cases.

The remaining issue as mentioned in other posts above is rotation of the game object’s collider, for which I have no comment :wink: The colliders really need to rotate in-place and not around the main object. Is there any physics setting regarding local rotation/object rotation versus world? Or perhaps constrain the main game object to have no Z rotation and somehow handle local Z rotations? Actually I think if you have separate game objects for the extra colliders they will be able to rotate around their own local center, and then if you attach them to each other by a physics constraint/joint (ie don’t make the game objects hierarchical, but tie their position to each other), then you will get local rotation… but this will still require copying any rotation between all of the colliders in the set.

there was a tutorial video i followed along with once making a space invaders clone, and if i recall, he had a pretty simple way of doing it.

I think my suggestion should handle rotational physics. Bear in mind each of the copies is kinematic and just mirrors the rotation of the central rigidbody. The joints to the centre object ensure that any impulses hitting the copies affects the central object correctly.

How would local rotation from one object be automatically copied to local rotation of another, when they aren’t rotating around each other?

You could perhaps add a second constraint/joint between one corner of one object and the same corner of the other object, so that if one rotates it causes the other to rotate, somewhat like on steam trains where a bar follows the wheel around as it turns? You’d need a separate child object for each child object, lol.

My approach would be to tile the map, e.g. in 5x5 parts and have a view sight of 2 tiles in each direction. As soon as you move to another tile, the furthest tiles are moved to the edge you are walking towards.

Would look decent on small maps regarding the view, but a good compromise to duplicating objects.