I've been having a bit of trouble with this small piece of code. To set my relative PortalCam position, I first take the distance and angle from the Portal and Player Cam. Then apply the distance and rotation to my OtherPortal to get the PortalCams position.
float newX, newY, newZ;
float d; // Distance between Portal and PlayerCam
float phi; // Angle between Portal and PortalCam
// Angle between Portals
float delta = Quaternion.Angle(portal.rotation, otherPortal.rotation);
d = Mathf.Sqrt( (playerCam.position.x - portal.position.x)^2 +
(playerCam.position.z - portal.position.z)^2 );
if(playerCam.position.x >= portal.position.x)
d = -d;
phi = Mathf.Atan( (playerCam.position.z - portal.position.z)
/ (playerCam.position.x - portal.position.x) );
newX = otherPortal.position.x - d*Mathf.Sin(phi - delta);
newY = playerCam.position.y + otherPortal.position.y - portal.position.y;
newZ = otherPortal.position.z + d*Mathf.Cos(phi - delta);
transform.position = new Vector3(newX, newY, newZ);
My problem is that, while this works perfectly when the portals are at 90degrees to each other. The code seems to fail when the portals are in-line.
Is there a mistake in my math? In my code? How can I best fix this bug?