# Closest facet to a sphere does not appear to be... closest?

**URL:** <https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869>\
**Category:** Questions\
**Created:** [February 28, 2020, 5:32pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869 "2020-02-28T17:32:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos.Mendieta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carlos.mendieta/32/124_2.png) [@Carlos.Mendieta](https://forum.babylonjs.com/u/Carlos.Mendieta)\
**Post date:** [February 28, 2020, 5:32pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/1 "2020-02-28T17:32:58Z")

</div>

Hello!

I was hoping someone could explain this to me.  
I have a box mesh and a sphere mesh some distance from it. I calculate the facets for the box mesh and determine the closest facet to the sphere. I then cast a blue ray to each facet, except the closest which gets a red ray.

Here’s the playground: [https://www.babylonjs-playground.com/#LQHH9A#1](https://www.babylonjs-playground.com/#LQHH9A#1)

You’ll have to rotate the camera some to see things (can someone explain that, too? why my rays seem to disappear when moving the camera around?).

I’ve used this code before and gotten what I thought was correct results, but then I saw sometimes things seemed off. This playground shows the red ray does not have the shortest distance between sphere and facet (so not the closest, right?) Is there a way to highlight the edges of the facets with something like the edgerenderer?

Thanks so much,  
Carlos

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 28, 2020, 5:45pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/2 "2020-02-28T17:45:02Z")

</div>

Pinging @jerome

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [February 28, 2020, 6:38pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/3 "2020-02-28T18:38:50Z")

</div>

There is an error in your logic I believe  
[https://www.babylonjs-playground.com/#LQHH9A#2](https://www.babylonjs-playground.com/#LQHH9A#2)

---

<div class="post-metadata">

**Author:** ![Carlos.Mendieta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carlos.mendieta/32/124_2.png) [@Carlos.Mendieta](https://forum.babylonjs.com/u/Carlos.Mendieta)\
**Post date:** [February 28, 2020, 6:45pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/4 "2020-02-28T18:45:36Z")

</div>

now i feel like a weenie. Thanks Pryme.

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [February 28, 2020, 6:46pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/5 "2020-02-28T18:46:27Z")

</div>

dude, I have done asked a billion weenie questions… don’t feel bad.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [February 28, 2020, 7:05pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/6 "2020-02-28T19:05:35Z")

</div>

If you don’t want your lines to appear/disappear, normalize the direction vector you pass to Ray:

[https://www.babylonjs-playground.com/#LQHH9A#4](https://www.babylonjs-playground.com/#LQHH9A#4)

@Pryme8 beats me for the solution, still here’s my updated PG so that I didn’t do it for nuts 😉 : [https://www.babylonjs-playground.com/#LQHH9A#5](https://www.babylonjs-playground.com/#LQHH9A#5)

---

<div class="post-metadata">

**Author:** ![Carlos.Mendieta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carlos.mendieta/32/124_2.png) [@Carlos.Mendieta](https://forum.babylonjs.com/u/Carlos.Mendieta)\
**Post date:** [February 28, 2020, 7:12pm UTC](https://forum.babylonjs.com/t/closest-facet-to-a-sphere-does-not-appear-to-be-closest/8869/7 "2020-02-28T19:12:09Z")

</div>

Ev, that’s awesomely cool. thanks! it was not for naught!
