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

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

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

Pinging @jerome

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

1 Like

now i feel like a weenie. Thanks Pryme.

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

1 Like

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

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

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