3
\$\begingroup\$

What is the best way to implement a wraparound world with wraparound horizontally physics in Unity?

That means that if I have an object and move it so that a part of it sticks off to right (so that the camera does not see it) I would like the part of the object to be shown on left. The same goes for the left side.

The best (even though I do not like it at all) implementation I was able to find is to have three objects. One of them is visible for the camera and two others become visible when the middle object goes off the camera.

Any other ideas on this?

Here is my explanation with images of what I want to happen: enter image description here

enter image description here

Thank you.

\$\endgroup\$
  • \$\begingroup\$ It's unclear to me what you are asking, could you ellaborate? Perhaps with some images? \$\endgroup\$ – Niels Jan 29 at 8:07
  • \$\begingroup\$ Have you tried simply changing the position of that object when it leaves the screen? \$\endgroup\$ – TomTsagk Jan 29 at 17:18
  • \$\begingroup\$ @TomTsagk, it won`t work because I want to make an object appear on left when it goes out of camera view on right even partially. Thank you. \$\endgroup\$ – iamyourfriend Jan 29 at 18:12
  • \$\begingroup\$ Do you want actual physics? As in "rigidbodies pushing each other with kinetic collisions" physics? \$\endgroup\$ – Philipp Jan 29 at 19:37
  • \$\begingroup\$ To be honest, this is a great question. I think my edited answer expands on the nuance of having a unique coordinate system that is not Cartesian, and how you render it in a Cartesian way. \$\endgroup\$ – Tim Holt Jan 31 at 3:58
0
\$\begingroup\$

Can you set up multiple camera views that are side by side and seamless?

Imagine one view that represents the right most 95% of your game window, and the left view is the other 5% of what's off the right side. Then with two views (and a single camera) you can render the same item twice, and create the wraparound effect. Basically when the object is half off the right, it's also being rendered on the left, because the left view represents what's off the right side of the screen.

Here's a pic that might help...

enter image description here

In the above, the green lines are the views, and the black line is the game screen.

Note that it wouldn't be 95/5 really, but based on the size of your object - specifically half the maximum width. If it is 100 pixels wide, your left side narrow view needs to be at least 50 pixels wide.

Edit and Refinement

As pointed out in comments, this does not solve the problem of physics and wrap around worlds. To solve that problem, what you need to do is not think of your world (and its physics) using 2D Cartesian coordinates. Instead, you need to model your world with the coordinate system of a cylinder. A cylinder has no "edge" and as such, there is no concept of "wrapping around the edge" at all. Yes, there is one if you try to go off the top, but if all you are concerned about is horizontal (X) wrap, a cylinder solves your problem.

Here's a picture to show the idea of an object on a cylinder, and how you render it (via something like my 2 view idea above) in a 2D space.

enter image description here

Your only challenge (which I think is valid as a separate question) is, "How do I render a cylindrical world space onto a flat, 2D view port?"

Note 1: If if you want to make things wrap around the top, it's going to get more complex.

Note 2: I am curious how the old asteroids game (which has wrap around on all sides) handles collisions in this (literal) edge case.

\$\endgroup\$
  • \$\begingroup\$ That is a very great idea. You are genius, thank you a lot! I will mark your answer as the best in a while. I want to wait for other answers (if they will happen shortly). \$\endgroup\$ – iamyourfriend Jan 30 at 7:44
  • \$\begingroup\$ This still leaves the problem of handling physics interactions between an object on the far left and an object on the far right, beginning to wrap around, does it not? \$\endgroup\$ – DMGregory Jan 30 at 8:13
  • \$\begingroup\$ @DMGregory Exactly, that's still an issue. What about duplicating the entities that move out of the screen? \$\endgroup\$ – Ignatiamus Jan 30 at 11:53
  • \$\begingroup\$ @iamyourfriend Simply duplicate the entity and set it's position to newobj.x = oldobj.x - screenWidth or similiar. \$\endgroup\$ – Ignatiamus Jan 30 at 11:56
  • 1
    \$\begingroup\$ Here is the continuation. Thank you. \$\endgroup\$ – iamyourfriend Jan 31 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.