Make sure all screen sizes can see two points

Hey!

I’m trying to find an efficient way to make sure two points are visible on all screen sizes.

Heres an example:

Result should look something like this:

2 Likes

I’m not sure how exactly to do this but just off the top of my head maybe you can add the two CFrames and then divide them by 2 to get the middle. I’m not sure if this will work but you can try and let me know what you get.

1 Like

This would get the mid point between the two parts. I’m trying to position the camera to where the two positions are just visible.

If you’re wondering what this is used for, It’s to make sure all device sizes can only see within a certain area.

You can maybe find the mid point then move the camera back though the camera would look the same on all devices so you just have to find one fixed point really, unless the camera is constantly going to change and if the 2 points are going to be moved further or closer.

maybe worldtoscreenpoint both of them

Using the lerp function, we can simply get the midpoint of the two parts. We can then set it so the Camera is locked to the midpoint.

Note: have this as a client-sided script under StarterGui or something

local RunService = game:GetService("RunService");

local Part1 = workspace.Part1
local Part2 = workspace.Part2

local MidPoint = Instance.new("Part")
MidPoint.Parent = workspace
MidPoint.Anchored = true;
MidPoint.Size = Vector3.new(1,1,1)
MidPoint.CFrame = Part1.CFrame:Lerp(Part2.CFrame,0.5)

RunService.RenderStepped:Connect(function()
     workspace.CurrentCamera.CFrame = MidPoint.CFrame
end)

Just make sure that the front surface of the two parts are both facing the direction of the baseplate

2 Likes

A lot of people are not understanding my post, I’m going to rephrase it

You can simply just multiply CFrames.

workspace.CurrentCamera.CFrame = MidPoint.CFrame*CFrame.new(0,0,5) – something like that, you may want to change the offset

If these aren’t moving and you want to map the camera z position then you could either just use a closed loop (PID, with the error being the distance between say the bottom left part in screen space to (0,1) with a setpoint of 0) changing how far the camera is for their device (this would happen instantly and could be hidden behind a loading gui). The alternative is I feel you could find some way to mess with the variables of a view frustum equation.

Maybe this will work

local cam = workspace.CurrentCamera

local cf1 -- CFrame 1
local cf2 -- CFrame 2

local mid = cf1:Lerp(cf2,.5)
local offset = ((cf1.Position - mid.Position) * mid:VectorToWorldSpace(Vector3.new(1,0,1))).Magnitude
cam.CFrame = mid * CFrame.new(0,0,offset/math.tan(math.rad(cam.FieldOfView)))
1 Like

That’s probably the complex view frustum math I was talking about, as I know that there is a tangent part of it

I tried this out and:


This is actually similar to my current method, although its not very efficient which is why I’m looking for a better method.

Never heard of this. After a little research I’m not too sure how I’d apply this to what I’m doing…

Really? That’s interesting as I use PID a lot outside of Roblox on machines with 1 GHz processing power and 256 mb’s of ram and they seem to have no issues. I digress, there is this thing called view frustum culling and it basically determines if a part is inside a “frustum” volume, things the camera can see defined by two planes (you are interested in the far plane). One approach is to use the clip space approach. After about a 2 minute google search I also found a video that shows that method on some sort of car in a viewport frame aswell.

I changed something, can you try again.

local cam = workspace.CurrentCamera

local cf1 -- CFrame 1
local cf2 -- CFrame 2

local mid = cf1:Lerp(cf2,.5)
local offset = ((cf1.Position - mid.Position) * mid:VectorToWorldSpace(Vector3.new(1,0,1))).Magnitude
cam.CFrame = mid * CFrame.new(0,0,offset/math.tan(math.rad(cam.FieldOfView/2)))

Math time.
You are just calculating the 2 dimensional interpolation. Easy fix would be just to offset the camera by the distance of the 2 points

local mag = (Point1.Position - Point2.Position).magnitude
Camera.CFrame = Point1.CFrame:Lerp(Point2.CFrame) + Vector3.new(0,0,mag)

Make sure you check if its the right axis it can be x too (cant tell from the given info)

EDIT: if it wants to be calulated using the two points object space

local mag = (Point1.Position - Point2.Position).magnitude
Camera.CFrame = Point1.CFrame:Lerp(Point2.CFrame) * CFrame.new(0,0,mag)

There is just one problem with that, that only works for orthographic projection and not perspective projection like in roblox

1 Like

Ok you got me here

local mag = (Point1.Position - Point2.Position).magnitude
local point = Point1.CFrame:Lerp(Point2.CFrame) 

Camera.CFrame = CFrame.LookAt((point * CFrame.new(0,5,mag).Position, point.Position)

That should fix it

This kind of works? The only issue is that the camera is slightly too far zoomed out:


It also doesn’t work with upright mobile devices:

Basically, you want the equivalent of selecting a model containing these 2 parts and hitting the “F” key?