Finding the angle of camera to mesh

Hi again,

I am attempting to try to find the angle between the red face of the cube (which represents the face of the character) and the direction of where my camera is facing.

I’ve done a bunch of searching and am pretty sure there’s not a way to get the direction of a face on a mesh BUT maybe I can compare the direction of the camera to the rotation of the mesh? (meaning to me the face has moved in a certain direction)


I’m attempting to visually change the sprite in a direction depending on where the camera is looking as shown in this photoshopped image:

If someone could point me in the right direction would be very much appreciated.

if you have 8 pic for 8 direction you have 1 pic for 360/8 = 45 degree

so when you find your angle for rad you just look your rotation start

if that range start from -3.14159565

picNumber = (Math.ceil(alpha*(180./3.14159265)/45))%8+4;

if that start from 0.

picNumber = (Math.ceil(alpha*(180./3.14159265)/45))%8 ;

you can make path for img like this
“/images/char_”+picNumber +".jpg"


Building on the suggestion of @nasimiasl here is a PG showing his idea

using spritesheet from this page Sprite Manager | Babylon.js Documentation

Of course you have to use you own sprite images and cell count


Thank you guys so much for your replies. I haven’t tried that out yet but looking at it briefly, wouldn’t I have to consider the camera’s location in comparison to the rotation AND where my mesh is facing and not just the rotation? In example: If the mesh has not yet rotate but the camera has rotated around the mesh, the only thing that should change is the sprite based on where my camera is from the rotation of the mesh. We are rotating the camera and thus our sprite must match the orientation of it taking into account the direction it currently has. (Which we use rotation to represent where it’s facing)

What I’m having trouble is the math for that equation if it’s a valid theory

Best thing to do is replace the series of sprites in the PG with your series of sprites. If it doesn’t give the results you want post your PG and tell us what is the issue.

Sprites always point towards the camera by the way.

I apologize, this doesn’t work because what I’m trying to say is that what if the player is not rotating the mesh but rotates the camera? The sprite becomes disoriented from the their actual direction and mesh.

In a real instance of game situation, there would be multiple players or npcs in all different directions. we aren’t rotating the player’s mesh but merely changing what we display as their sprite because of the change in our camera’s location

(in this example, the red is always where the player is facing, meaning we change the sprite cell to match that direction depending on our camera’s horizontal shift)

Sorry if I’m not being clear, its hard to articulate myself sometimes :sweat_smile:

This works magnificently. How would I work Camera.rotation.y into this equation?

alpha = camera.rotation.y

I’m sorry I meant my mesh rotation*

It is the angle, in radians, you need to convert to image number. If you base the sprite rotation on the mesh rotation then it the angle the mesh makes. If you base the sprite rotation on the camera rotation then it is the angle the camera makes.

Of course if the sprite is rotating with the mesh and the camera is rotating you need to take into account relative rotations. In this case, except for the problem of creating the 3D figure, viewing a 3D figure would give you the correct view.

i will prepare a demo tonight


Amazing! I can’t even begin to wrap my head around how

Amazing collab @JohnK and @nasimiasl !!!