Godot Version
v4.2.2.stable.mono.official [15073afe3]
Question
I’m trying to make mirrors for a game, as realistic as I can. I played around with reflection probes but too many of them were laggy (I need a lot of mirrors sometimes)
So I went back to the old classic: a camera pointing at the player.
Test scene is setup like this:
Level
- Ground
- Player
- MainCamera
- PlaneMirror
- Subviewport
- Camera
- Subviewport
Problem: to make it realistic the camera would need to move and rotate in opposed sync with the player, using the mirror as its “pivot”
Looking everywhere, the closest I could find to what I wanted was an old unity tutorial for portal making: tuto. I thought “that’s what I’m doing except the entry and exit portal are the same plane, I just need to adjust for that”
And using it I got a result where the camera rotation was a perfect match, but not the position. The mirror camera would go to the center of the mirror and not move to match the player mouvements.
So I tried to tinker with positions etc, and I kinda got somewhere? But not really. It’s still not fully accurate.
I feel like I made a mess of the whole matrix thing and if I could figure it out it would solve most of the issues but I’ve no idea where to start.
using Godot;
public partial class MirrorCamera : Camera3D
{
public Node3D playerCam;
[Export] public Node3D _mirrorMesh;
Transform3D _mirrorTransform;
public override void _Ready()
{
playerCam = GetTree().Root.GetChild(0).FindChild("Player", true, false).GetNode<Node3D>("MainCamera");
_mirrorTransform = _mirrorMesh.GlobalTransform;
}
public override void _Process(double delta)
{
//Calculate the reflection matrix
Transform3D reflectionTransform = _mirrorTransform * playerCam.GlobalTransform;
GlobalTransform = reflectionTransform;
float distanceZ = _mirrorMesh.GlobalPosition.Z - playerCam.GlobalPosition.Z;
float distanceX = _mirrorMesh.GlobalPosition.X - playerCam.GlobalPosition.X;
GlobalPosition = new Vector3(GlobalPosition.X + distanceX, playerCam.GlobalPosition.Y, _mirrorMesh.GlobalPosition.Z + distanceZ);
}
}
Thanks in advance to anyone that would take a look.