# Viewportframe portal maths

Heya, I’m working on a Portal effect, and I’ve watched a video explaining that I need the camera of the second portal to be relative to the world camera of the first camera. It’s a little bit confusing, but I get what it means (here’s an image)

note: the green camera is the world camera and the yellow camera is the viewportframe camera.
I would like to know the maths that I need to use to get this simple effect.
I’ve tried a couple of combinations of this:

``````local localspace = portal1.CFrame.Position - Camera.CFrame.Position
localspace = portal2.Position - localspace
``````

but I can never seem to get it to work:
Any suggestions are appreciated.

1 Like

This is quite a complicated script, and is 800 line script for me
Do you need the whole script or if there is any error tell me that error

Well I need to know the mathematics behind it, so not the whole script but just what calculations need to be performed to get a realistic result.

You can try checking out this open-source demo by the amazing @EgoMoose Teleportation portal demo - Roblox

if you want to transform a vector in one coordinate space (A) to another one in another coordinate space (B) you need to find the basis vectors of coordinate space B then multiply it by the matrix transformation using those basis vectors and the origin of the coordinate space. Since your cameras cframe is technically its own coordinate space (lookat matrix) you just do transformedAToB = B*A

I was trying to do this portal thing too but I was wondering how to make the viewport frame only show what was in the portal, the viewport frame camera is behind the other portal at the same distance that your camera is to the portal so the camera is zoomed out and doesn’t look like you are looking out of the other portal but instead you are looking behind the other portal, so how would you crop it to only show the space in front of the other portal, if you can understand this if not I’ll show an image tomorrow