Advertisement Jump to content
Sign in to follow this  

How do you convert World Position to Field of View angle.

This topic is 1981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing some camera calculations and would like to convert the target's world position into Offset angles from the center of the screen, based off of the veritcal and horizontal Field Of View.


this picture explains what I am after:




I tried getting spherical coordinates by converting cartesian to spherical, but it isnt exactly right, since i want them to be offsets from the center of the screen and those angles are not. I AM able to convert any viewport point to offset angles, and could convert the target to a viewport point as well, but that would get weird results when the character is not in the field of view.


Maybe i can add an offset to theta of the spherical coord to get its orgin at the center of the screen?


Here is code i have so far which i know is wrong but will provide context with what i am attempting to do.

Vector2 zeroOffset = new Vector2(CameraGame.HorizontalFov / 2, Camera.main.fieldOfView / 2);

        Vector3 charPosViewSpace = Camera.main.worldToCameraMatrix * target.position;
        charPosViewSpace += Camera.main.transform.position;

        //Convert View Space Postion to Spherical Coordinate.
        float r =
            Mathf.Sqrt((charPosViewSpace.x * charPosViewSpace.x) +
                       (charPosViewSpace.y * charPosViewSpace.y) +
                       (charPosViewSpace.z * charPosViewSpace.z));

        float S = Mathf.Sqrt((charPosViewSpace.x * charPosViewSpace.x) +
                             (charPosViewSpace.y * charPosViewSpace.y));

        float phi = Mathf.Rad2Deg * Mathf.Acos(charPosViewSpace.z / r);
        float theta;
        if (charPosViewSpace.x < 0)
            theta = Mathf.Rad2Deg * (Mathf.PI - Mathf.Asin(charPosViewSpace.y / S));
            } else
            theta = Mathf.Rad2Deg * Mathf.Asin(charPosViewSpace.y / S);

        Debug.Log("xAngle: " + theta + " yAngle: " + phi);
        Debug.Log("SafeZone: " + SafeZone);

Here is how i get viewport coords to FOV angles:

public void calcAngles()

        Vector2 zeroOffset = new Vector2(CameraGame.HorizontalFov / 2, Camera.main.fieldOfView / 2);

        minRange = new Vector2(CameraGame.HorizontalFov * x1 - zeroOffset.x,
                                       CameraGame.HorizontalFov * y1 - zeroOffset.y);
        maxRange = new Vector2(CameraGame.HorizontalFov * x2 - zeroOffset.x,
                                       CameraGame.HorizontalFov * y2 - zeroOffset.y);
Edited by AverageJoeSSU

Share this post

Link to post
Share on other sites
The function you want to use to convert a 2D vector to an angle is atan2. If you have trouble using it, post a particular example of coordinates and I'll show you how to use it to compute the angles. But you can probably figure it out by yourself.

Share this post

Link to post
Share on other sites

Hmm, i have never used that before but the concept makes sense....


I have two examples: lets say in view space, Coords (-5,5,10) (z is into the screen)


and then behind the camera 5,0,-10

Share this post

Link to post
Share on other sites
To compute the angle along the x axis, use atan2(x,z). In this case, atan2(-5,10) = -0.463648 or, if you prefer, -26.5651 degrees.

Similarly for the angle along the y axis, use atan2(y,z). atan2(5,10) = 0.463648 = 26.5651 degrees.

Does that make sense?

EDIT: Oh, in the case of (5,0,-10) you get angles of 153.435 degrees and 180 degrees. Is that what you expected? Edited by Álvaro

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!