How to determine camera is in front of mesh face direction

I would like to determine if the camera is in front of or behind the mesh with respect to its direction, how can I determine this? Currently I am trying to utilize code like mesh.getDirection(Babylon.Axis ~~ ), but I am having difficulty.

Hi dhdbtkd !

One way to do this is using the (Oriented) Bounding Box of your mesh and the class Plane.
First, you have to find the 4 points of the quad represented as “Face” in your drawing. The Oriented Bounding Box can give you that.
See

When you have these points, you can create an instance of the class Plane by using FromPoints or FromPositionAndNormal (beware of the points order if using From Points as the plane normal will be computed according to it).

You can use the centerWorld attribute of your BoundingBox to make sure you initialized your plane with the correct normal direction.

Now you just have to compute the vector between your camera and any points of the plane (one used to create it for instance).

And use the methode on your plane instance
isFrontFacingTo
with an epsilon 0 (aka the cosinus between the tested vector and the plane normal should

Then if tested vector returns true, it means that your camera is in the “true” area.

Here is a PG with a simple case (plane is very simple as it defines the default ground on the world origin)

I guess there must be other (and simplier ?) methods to do that, but I hope it will help you !

1 Like

Hi Amarth2Estel
Your answer is helpful, is there any way to determine this using mesh.getdirection()?? Thanks

Hello
you can get the angle between the camera direction and mesh direction to determine if the camera is facing the mesh direction

made this enemy field of view to illustrate

you can control the viewRadius, the one i have here is between [45, 135] degrees

3 Likes

I fixed the field of view, much cleaner now

5 Likes

Hi waverider404.
You totally saved my time.
How can you solve it in such a simple way? I was combining complex methods to solve this problem. I was not able to solve this problem for several days. I felt the power of the community again, thank you so much.
If you have a @buymeacoffee account, I’d love to send you a cup of coffee.

2 Likes

Haha you too kind!

Just glad i could help :smiley:

3 Likes

You gave me an answer that was worth more than that. Thank you again. Have a great day

This community is awesome !!!

1 Like

I came here to say that :smiley:

2 Likes