2D asteroids style map wrapping

I am working on an asteroid style 2d side scroller. I’ve got movement and the camera is static as well as a background image and projectiles that shoot with sound. the only thing I cannot figure out or find an answer to anywhere is how to make it so the player loops back around to the other side of the visible map when at the edge, bullets as well…

I had an idea about using an hitbox however everything I try either crashes or breaks or goes into an infinite loop…


This would be easiest if the center of your map is at (0,0,0), and then in the Tick of the ship Actor just keep track of the X and Y coordinates (or whatever 2D axes you’re using).

When one of tracked axes is a unit outside your bounds (and possibly some units for the resolution size of your ship so it goes off-screen)…

  1. Get the ship World Location
  2. Break it into a vector
  3. Multiply the axis out of bounds by -1 and set it (along with the other 2 axes) as a new vector as the World Location for the ship Actor.

It’d look something like this snippet (which is for one of the axis):

If you’re doing the movement on the Tick, do the bounds check after so you don’t get stuck on an infinite loop warping back and forth.

If you’re not doing in the tick, have a float variable that only does the bounds check every XYZ seconds as needed so you can warp and have a second or two to let the velocity push the ship – using [GetGameTimeinSeconds][2] will be useful for tracking time.

Beware Crappy Drawing Incoming xD

Time to explain that one I guess. The Green rectangle is your Cameras view and the red indicated rectangles are your “Fake” Worlds. So you See the Green guy moving to the left and the the Red Guy a “Copy” does exactly what the green Guy does. If he Moves he Moves too If he attacks he Attacks too etc. at some point he reaches the trigger Box teleports to the Oposing side of the Screen. Now thats a Math Calculation you can base it on the Camera Position or the Trigerbox+Halfsize or the Global Axis or Screensize thats up to you how you want to Handle that. Grab a Calculator and run the Numbers =)

So than the Transition happens wher Fake Guy and Real guy are both in Camera View. If you would base it on Boarder touch you would never get a result like that. And than the Cycle goes on Green Guy moves into the Trigger->Teleports and the Story repeats itself. You do the same thing for Bullets and Enemys. You can do the Exact same thing if you want Up and Down transitions too than you would have 3 Guys teleporting around.

Thats basicly it in Theory. I can warn you off a Pitfall you maybe trap into early. The greenbox is not always the same Size! Depending on the Resolution and Aspect Ratio. Also you might Trigger Hit Events twice for something like Bullet Hit Events. So make sure you flag the Copys to ignore them.

1 Like

Hehe yeah thats the oldscool way like Super Mario World did the Player vs Player mini Battle. Its a simple Solution but it has no Transition and relies on a well defined known Screensize. Also the Character Width and Height takes a significant role in the Calculation because that decides if you get a Teleport effect or have to walk Completly out of the Screen before reapearing on the other screen.

But hey its a Solution after all =) if that is what he needs.

I was trying something like @SparkyMcSparks but kept getting errors

My only question is if this is left and right and I do the same for up and down, do I just make have the red box surround the entire box? or only place top and bottom boxes that are similar to the left and right ones and simply have teleports on the sides as well as bottom/top and place teleports on the top and bottom of the side boxes…

I really hope that makes sense.

This also doesn’t rely on a specific screen size either does it? like the other solution does. I could see some errors if it does,

You put 4 separeate Trigger Boxes on each side stretch them out until they overlap. I also just noticed you need 4 Copys not 3 if you want Up-Down-Left-Right looping (think of the Corners)

And hey you are right thinking of it a bit more you need the Screensize(playable area) at least to calculate the Teleport Location. If you using a Orthograpic Camera thats no big deal you can read out the Viewport Size directly. If you using a Normal Camera you can get away by moving the Camera closer or farther away in most cases. Depending on the Ressolution. Otherwise you need some additional math to calculate the Frustum.

As far the other stuff goes it stays the same.

I know I accepted this as answered, however I’ve been trying for nearly a week to get this working and I can’t get anywhere… I’ve created 3 pawns of the same pawn and cannot move them together… I’ve searched all over for controlling multiple pawns and nothing seems to be specific for what I’m trying to do. I managed to get the other answer to work… one time, going left… then it crashed every other times… I cannot for the life of me figure out why neither of these will work, looking up event ticks and locations for the second problem on google does nothing as it gives a bunch of garbage, I’m starting to get pretty frustrated that something so simple would end up being so difficult.

Sry to hear that you struggle. But Im not sure what exactly you struggle with so I guess its the Input? Thats because Pawns have a 1to1 Controller relation.

But there are multiple ways to overcome that Problem. You can give you Copys a fake AI Controller that Mimics your Inputs. You can Attach your Actors to your Original and the will keep a relative Offset so you have to manage just the Animations. Or you keep refferences of your Copys and send the Input directly trough a custom event that hooks into the same Movement Logic (assuming you dont use Controller for Movement)

Also Consider using Actors instead of Pawns it comes with downsides and upsides.

If you need a example that does not use Multiple Pawns there is a Astroids Template on the Forums from Elhoussine Mehnik its for 4.7 but the general basics should still apply.

I managed to get the other method working properly finally, and since I’m using an orthograpic display with a set size it shouldn’t matter. Now to get AI of some sort working… I’d rather be making a bullet hell but I’ve gotta do this for my final apparently, even though we were told it was just for fun and not graded.

thank you for the help. I’ll definitely checkout the link you gave.