TODO there are some mistakes

Portals


Non-euclidian space! It’s cool! Lets program it!

But… How do you make non-euclidean space? To create my non-euclidian space, I started with 2 rooms, one green, and one blue. Then filled the rooms with walls and special portal-walls. Portal-wall is a wall that is connected with another Portal-wall. The player can see through it what’s behind the other connected wall.

layout sketch

You can see that the portals are in the same positions in both rooms. That is very cool because when the player goes through a portal, the room looks different but feels like the same place. It feels like the walls are disappearing and appearing as he’s walking throughout the room.

In such a space, you could make a path like this:

path image

And it would feel like walking around in a single room with walls appearing and disappearing as you walk. And that is just cool.

Implementation!

For my first attempt, I choose Unity. Mainly because I’m already familiar with it.

editor view

I created the rooms and a simple player control script with a cool feature.

To see what’s on the other side of the portal, each portal has its own camera. The camera moves around the other, linked portal and on each frame, disables the other linked portal’s screen, takes a picture of what’s behind, and reenables the other, linked portals screen. This picture is then cropped and applied to this portal’s screen as a texture.

To optimize this a little bit, I don’t have to render all Portals every frame. I can simply cast some rays from the camera to nearby portals and see if the portal is really visible. If not, just fill the portal screen with red.

working example

This looks quite cool from this angle. But if I move

broken example

This is of course because the red portals are not directly visible from the main camera. The solution is not elegant. Every test ray needs to be teleported correctly when it hits a portal to see if it hits another portal. But not only that.
When I find out that there is a second portal behind a portal, I can’t just call its render function and be done with that. Think about how the camera is positioned.

working example

First, I calculate the orange vector from the portal to the player. Then I apply the orange vector to the position of the other portal and place the camera there. But if there are 2 portals behind:

broken example

When calculating the position of the second portal, the main camera is in the other room. This results in a really long red vector that places the camera far away from where it should be. The solution is to subtract the Vector pointing from one side of the portal to another * 2.

fixed example

So after implementing this.

ingame view

So this seems to be working. Yay! Now I can walk the path I suggested at the beginning!

The path

But there are some weird artifacts when going through the portal. I don’t really know what to do with that. And what if I want to render multiple portals behind each other? Because I do. I would probably need to do a DFS search using rays and then return multiple lists of portals from each ray and somehow combine them and render them in the right order… And keep track of the portal offset to position the camera correctly… couldn’t it be done easier? Am I too deep in this code to just throw it away?
Yes! I can do it more easily and I can throw all this code away!

(This throw away code is here)

Implementation 2!

To be continued!