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.

Example image: Green dot is centered in the screen and red dot is 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)

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)

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.

  • \$\begingroup\$ 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) \$\endgroup\$ Jul 8 '21 at 23:27
  • \$\begingroup\$ Added some code as requested. Having a hard time understanding your response, could you give a more concrete example? \$\endgroup\$
    – Nezuo
    Jul 8 '21 at 23:38
  • \$\begingroup\$ Are you able to use quaternions? Because a relative quaternion pointing from your camera to that point on screen should be a constant. \$\endgroup\$
    – Weckar E.
    Jul 9 '21 at 10:29
  • \$\begingroup\$ 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). \$\endgroup\$
    – Nezuo
    Jul 9 '21 at 20:40

You must log in to answer this question.

Browse other questions tagged .