valepu

Angle between camera-mesh vector and mesh rotation

Recommended Posts

Hello,

To get the angle between a mesh rotation (or better, its "facing") and the current camera facing on the y plane i am using this code:

function getFacingAngle(camera, mesh) {
        var rotation = mesh.rotationQuaternion.normalize().toEulerAngles();
	var cameraRot = ((camera.alpha * 180 / Math.PI)) % 360;
	var objectRot = ((rotation.y * 180 / Math.PI) + 180) % 360;

	var rotDiff = cameraRot - objectRot;
	var rounds = 360;
	var roundsI = 1;
	//to avoid cases where rotDiff gets very high negative values
	while (rotDiff + (rounds * roundsI) < 0) {
		roundsI++;
	}

	rounds = rounds * roundsI;
	return Math.ceil((rounds + (rotDiff)) % 360);
}

 

Which i have extensively tested and it works, i'm sure it can be optimized though!

As you may have noticed this works only with ArcRotateCameras since it uses the alpha value.

How would you generalize this to work with any kind of camera? Or even to get a vector of angles for every axis on the plane?
Is there some way to get the camera type?
 

Share this post


Link to post
Share on other sites

Heya @valepu, how's it going?  I hope well.  Cool code, well done.  I don't have any juicy math/tech for you, but there is one thing you COULD consider - parenting your camera to an invisible plane or box (a cam gizmo).  Then, you have just one function to write and not so much camera type testing.  You need to calculate the angle between some "forward" face on your camera gizmo... and whatever you are calling the "front" of that torus knot.  :)

Y-axis facing camera, you say?  Direct overhead view of the mesh?  Or did I misunderstand that part?

All in all, I just wanted to remind you that all our cameras can be parented to an invisible plane or mesh, and knowing that might help with future "angling wrangling".  Others will comment soon, I'm sure.  We might need some playground examples to test with, so feel free to make us one, val. (thx)

Share this post


Link to post
Share on other sites

Thanks for your answers! And i am fine, thanks for asking :P

I swear i did not disappear, but i didn't have the time to test it yet, i'm pretty sure attaching an object to the camera will work  by comparing their rotation vectors but i'm trying to make a generic library. So I wanted to know the most generic way to compare the rotation between a mesh and any kind of camera

By the way no it's not overhead it's in front actually, the rotation is on the Y plane but the meshes are in front of the camera

Share this post


Link to post
Share on other sites

Hello again :)

Let me say sorry in advance for my noobishness, my english and because i'm probably going to use the wrong terms to describe things...

I finally got time to explain my situation. I am making a library that changes a plane's texture based on the difference between the camera rotation (alpha for ArcRotateCamera) and a mesh facing to make it look like it's a 3d-ish image even though it's a set of 2d sprites that "rotates" together with the camera

http://mjzone.net/game/game.html (this is an early alpha done while experimenting with babylon, i am heavily rewriting the code now) It uses https://github.com/andyhall/babylon-atlas

So, while i was using an ArcRotateCamera that had the character as target everything went fine because the vector between the mesh and the camera is the same that defines the camera facing, so comparing the alpha value and the mesh rotation did the job . When i tested with a FreeCamera (comparing camera rotation and mesh rotation) i realized that it wouldn't work because i need to get the angle between the mesh facing vector and the camera-mesh vector since a FreeCamera, like the name suggests, is free and can go whenever it wants to go :D Checking this angle should be camera independant as long as i can get the vector between the camera and the mesh, isn't it?
The attached image should clarify what i mean

So, truth be told, i suck at vectorial math and i am kinda clueless on what i have to do for this to work and i will really appreciate your help

camera.png

Share this post


Link to post
Share on other sites

If you know the direction or leading vector of your camera (maybe with getTarget() ), you could compute this angle with a dot product : http://doc.babylonjs.com/classes/2.4/Vector3#static-dot-left-right-rarr-number

say, in pseudo code :

camDirection = (camera.position - camera.getTarget).normalize()  // your green vector normalized

meshDirectiion = (camera.position -mesh.position).normalize()      // your red vector normalized

d = cos(angle) = dot(camDirection, meshDirection)  => angle = acos(d)

 

Share this post


Link to post
Share on other sites

 

On 8/6/2016 at 10:04 AM, jerome said:

If you know the direction or leading vector of your camera (maybe with getTarget() ), you could compute this angle with a dot product : http://doc.babylonjs.com/classes/2.4/Vector3#static-dot-left-right-rarr-number

say, in pseudo code :

camDirection = (camera.position - camera.getTarget).normalize()  // your green vector normalized

meshDirectiion = (camera.position -mesh.position).normalize()      // your red vector normalized

d = cos(angle) = dot(camDirection, meshDirection)  => angle = acos(d)

 

Hi again :)

I finally managed to work on this and your answer was very enlightening. I didn't really need the green vector, that was just for visual. What i needed was the angle of the red vector on the X axis (i said Y earlier but i got it wrong)

so what i did was

cameraPos = camera.position.clone()
cameraPos.y = mesh.position.y (as if they were at the same height)
meshDirection = (cameraPos - mesh.position).normalize()

angle = acos(dot(meshDirection, Vector3(1, 0, 0))

this (with some adjustments) gives you the same values of the ArcRotate's alpha but for any camera

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.