3
$\begingroup$

I have got trees in a world. enter image description here

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?

enter image description here

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!

$\endgroup$
  • $\begingroup$ Can this question be asked in math stack exchange community. $\endgroup$ – Jasser Oct 1 '14 at 16:25
  • 1
    $\begingroup$ @Joehot200 Please reconsider the community you're posting your question. $\endgroup$ – Bruno Bentzen Oct 1 '14 at 16:29
  • 2
    $\begingroup$ 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. $\endgroup$ – user2722083 Oct 1 '14 at 16:35
  • 6
    $\begingroup$ 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$$ $\endgroup$ – Willie Wong Oct 2 '14 at 8:11
  • 1
    $\begingroup$ @Joehot200: it is a bit of trigonometry. Check google.com/… and see if any of the links help you. $\endgroup$ – Willie Wong Oct 7 '14 at 7:30
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.