# How can I find objects behind my camera?

I have got trees in a world. However, these trees can be fairly CPU intensive. As you can see, the far-away island on the left has no trees at all - This is because trees currently have to only render in a short radius of the ship (or lag like hell).

My question is, if I have a single tree, how can I check if that tree is behind the camera? I have:

The location of the ship.

The location of the tree.

The direction of the Camera.

The size of the tree (sortof).

Basically, I would like to compare the direction (and location) in which the camera is looking and then compare it to the tree's location to see if the tree is in front of or behind the camera.

Thanks!

• Can this question be asked in math stack exchange community. – Jasser Oct 1 '14 at 16:25
• @Joehot200 Please reconsider the community you're posting your question. – Bruno Bentzen Oct 1 '14 at 16:29
• This is purely a mathematical question and is therefore 100% ontopic for this site. Asking it on any other site would be offtopic because they deal in programming and not math. – user2722083 Oct 1 '14 at 16:35
• To answer the mathematical part of your question. Let $X$ be the position of the camera (I am not sure if that is necessarily that of the ship. But I assume you can compute that position from the data.) Let $V$ be the vector representing the direction the camera is pointing. And let $Y$ be the position of the tree. The tree is in front of the camera if and only if the dot product $$(Y - X) \cdot V \geq 0$$ – Willie Wong Oct 2 '14 at 8:11
• @Joehot200: it is a bit of trigonometry. Check google.com/… and see if any of the links help you. – Willie Wong Oct 7 '14 at 7:30

Having the camera matrix $K$: $$\begin{bmatrix} f_x & 0 & w/2 \\ 0 & f_y & h/2 \\ 0 & 0 & 1 \\ \end{bmatrix}$$ where $w$ and $h$ are the projection image size, and $f$ is the focal length, and the camera rotation $R$ and translation $t$ then the projection matrix is formed by: $$P=K[R|t]$$ and a word point $X$ represented in homogeneous coordinates is projected in image space as: $$x=PX$$ In order to check if a tree is in front of the camera, all you need to do is to transform the tree (i.e. the center of the tree bounding box $Xt$) from world space in camera space: $$X_c=R*X_t+t$$ and check if the depth value $X_c.z$ is positive.