# How can I programmatically tell a camera where to point?

I don't have a particular camera in mind right now, I'm just curious how this is done, programmatically/mathematically.

I have a 3D space, a rectangle, with a camera up in one corner looking inwards.
I have a moving object in that rectangle that's transmitting (x, y, z) coordinates of its current position.
I want to take those coordinates and translate them into instructions telling the camera to point at that position.
How is this translation typically done?

• You may want to look at how game developers handle their in-game cameras. It's the same principle and they've had to deal with everything under the sun, so to speak. – Harabeck Mar 30 '18 at 15:36
• It may be worth noting that the object doesn't have to transmit its coordinates in literal 3D space, but rather, could transmit by being in the camera's FOV. You would then use pattern recognition to find where in the frame the object is located, and then shift the camera to center the object (or otherwise identify a motion vector in 2D by tracking the change in position across multiple frames and move in that direction). For example, my PTZ camera on the porch scans the porch looking for motion not due to the panning motion, and then locks on and follows the moving item until motion stops. – dannysauer Mar 31 '18 at 13:55
• My use case is a sports analytics system, each player is wearing a tag that transmits current position, speed, etc on the pitch. So we already have the position information. I'll definitely take a look a pattern recognition though. Sounds useful. – bot_bot Apr 1 '18 at 7:27

# Trigonometry !

My camera is a DLink 5020-L and has pan/tilt commands which can be given through an API. It also has predefined positions to set and can also be triggered through API

## Pre-init

• Define a position of your camera to a 0° Pan and a 0° Tilt in your referential => we will call this position Position 1

## Init

• Move your camera to Position 1
• Store somewhere the pan/tilt of your camera, either in 0-initialized variables or through your API

## Look at object

• Locate your object in two planes, the X,Y and the Y,Z planes
• You can get then the pan (left/right) angle (omg mathematics formulae in an IoT SE!)

• You can get then the tilt (up/down) angle

• Don't forget to save/update the new pan/tilt value since you might work with relative movement...

You might negate the previous results depending on how your camera is placed

(I'll add some schematics when I have time)

• Beaten by @hardillb ;) And he has better looking formulae... – Goufalite Mar 30 '18 at 9:58
• Unfortunately the LaTeX support isn't available on this site. You can export it from somewhere like CodeCogs though into an image as a substitute if you like. (I've done this for you; feel free to edit as required or remove it if you don't want it!) – Aurora0001 Mar 30 '18 at 10:07
• Thank you both for your help. This is exactly what I was looking for. – bot_bot Mar 30 '18 at 12:31
• I think you forgot to take into account the fact that the arctan for the tilt angle has to be with respect to the z component over the hypotenuse: putting it over the y component could raise / lower you camera insufficiently since the camera is going to be pointed along the hypotenuse of a right triangle between the y-component and the x-component, not along the y axis. Correct me if I'm wrong. :) Great answer, though. – anonymous2 Mar 30 '18 at 16:48
• @anonymous2 that was my thought, too. I don't think any of the answers presented yet are actually correct but I don't have the bandwidth to actually show that right now - it seems you need two triangles and all three coordinates to get either variable (pan or tilt). You can prove this to yourself by visualizing two different extreme examples: x,y,z of 1,1,999 is going to give much different different pan and much different tilt than 1,1,1. Goufalite's answer gives the same pan for both. – dwizum Mar 30 '18 at 17:14

This is normally done with basic trigonometry.

Start by working on a single 2d flat plane with the camera at the origin (0,0) and the object at (x,y)

Given that the x distance will be the adjacent side of the triangle and the y distance will be the opposite you get:

so the pan angle can be found with

You can also work out the straight line distance (the hypotenuses) between the camera and the object with:

Giving:

Now you can use the h distance with the z height to calculate the tilt angle in the same way.

Once you have the angles you can feed these to what ever is controlling the pan/tilt on the camera.

Great answers already, I'd just like to add a few other things that you should take into consideration. Like hardlib and Goufalite have already mentioned, the way to do this is trigonometrically. I've drawn out a 2-d depiction of the camera and the IoT object:

As you can see, the camera's field of view is going to be larger than the object - if not in close range, when the object moves further away.

Now, you may want the camera always centred on the object. In that case, you can simply take the calculations that hardlib referenced:

ϴ = arctan(y/x)


...which will be the angle counterclockwise from the x-axis, per convention. You'll also need the angle away from level:

α = arctan(z / ((y^2+x^2)^1/2))


Obviously, you'll have to calculate based off of the camera position being at the origin in all three axes.

On the other hand, you may prefer to not make the camera move more than necessary, that is, to make the camera only move once the object appears to be about to move out of the frame. In that case, you'll probably want a "pressure" variable which will make the camera more likely to change its angle based on how close the object is to the edge of the frame.

If you go that route, you'll need to know the angle of the camera's field of view in both fields of view, so that you can determine where the object is compared to the camera's field of view.

• This is great! Thanks, at the moment I'll want to keep the object centred in the cameras field of view. – bot_bot Mar 30 '18 at 12:36
• When I pan/tilt my camera I have some latency (~0.5 seconds) between each order, beware of that when moving your camera – Goufalite Mar 30 '18 at 13:41
• Good point - that's definitely something to consider. – anonymous2 Mar 30 '18 at 14:22