# Rotating around pivot so target is 25% away from top of screen

I have a camera which rotates around a pivot (green dot) that is always a constant X units away from that pivot. I want to find the pitch to rotate around that green dot so that the target (red square) is always 25% away from the top of the screen.

This is used for a lock on camera. I've solved for yaw and everything but I don't really know what math would be used to achieve this. Is it based on field of view? Does perspective have something to do with this? Any help would be appreciated.

edit: Was asked to provide the code so here it is:

local DISTANCE = 8

local function angleBetweenPointsXZ(a, b)
local deltaZ = b.X - a.X
local deltaX = b.Z - a.Z

return math.atan2(deltaZ, deltaX)
end

local function onUpdate()
local yaw = angleBetweenPointsXZ(pivot.Position, target.Position) + math.pi
local pitch = 0

camera.CFrame = CFrame.new(pivot.Position)
* CFrame.Angles(0, yaw, 0)
* CFrame.Angles(pitch, 0, 0)
* CFrame.new(0, 0, DISTANCE)
end


I was hesitant to include code because this is being done in Roblox. If you aren't familiar, a CFrame is just a vector for position and a rotation matrix. So that last piece where we set the camera's CFrame, it starts at the pivot's position with no rotation, applies a rotation around global Y for yaw, applies a rotation around resulting local X for pitch and than moves a DISTANCE away from the pivot in the negative lookVector direction. In this code I am trying to solve for the pitch variable.

• Could you provide the script you are using? A priori, I would suggest using the height of the camera's viewport projected at the distance of the target and use that to delimit how much the camera can displace (or even rotate) Jul 8 at 23:27
• Added some code as requested. Having a hard time understanding your response, could you give a more concrete example? Jul 8 at 23:38
• Are you able to use quaternions? Because a relative quaternion pointing from your camera to that point on screen should be a constant. Jul 9 at 10:29
• I can use quaternions (I have a custom library), but what do you mean by pointing to that point on screen? The camera should always be pointing at the pivot (green dot). Jul 9 at 20:40