# How to know mesh is behind another mesh from camera?

**URL:** <https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094>\
**Category:** Questions\
**Created:** [January 25, 2022, 10:00pm UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094 "2022-01-25T22:00:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pigga](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@pigga](https://forum.babylonjs.com/u/pigga)\
**Post date:** [January 25, 2022, 10:00pm UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094/1 "2022-01-25T22:00:52Z")

</div>

Is there any good way to know if one mesh behind another one so we can just make the one closer to camera show?

---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [January 25, 2022, 10:10pm UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094/2 "2022-01-25T22:10:45Z")

</div>

This is a 3D math question!

You can calculate dot of the position of the object and the forward vector of the camera. Whichever is smaller is closer. (assuming it is greater than 0) Let me see if I can get a working demo just to double-check my answer.

---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [January 25, 2022, 10:23pm UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094/3 "2022-01-25T22:23:34Z")

</div>

> **[Babylon.js Playground](https://playground.babylonjs.com/#5LRGVH%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Yep just as I thought!

Take a look at this example I made. You can change the positions of the spheres to see. Also for reference, the way we get the “forward vector” of the camera is taking the camera.target - camera.position.

Let me know if you have any follow up questions!

---

<div class="post-metadata">

**Author:** ![pigga](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@pigga](https://forum.babylonjs.com/u/pigga)\
**Post date:** [January 26, 2022, 12:21am UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094/4 "2022-01-26T00:21:59Z")

</div>

Thank you! Do you know why we need to take the camera.target-camera.position, what’s the different by just use camera.getDirection(Vector3.Forward())?

---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [January 26, 2022, 12:31am UTC](https://forum.babylonjs.com/t/how-to-know-mesh-is-behind-another-mesh-from-camera/27094/5 "2022-01-26T00:31:55Z")

</div>

That works too! More was explaining the math behind it I guess. 😃
