# Calculating rotation from direction

## Recommended Posts

Hello everyone.

I need the math/code for calculating the rotation from a light's direction.

Example:

I have a spotlight with a direction.

I want to show a cone mesh at that light's position with the rotation that the light is facing.

I not use parent for anything!

I only need a code example of how to get the rotation values in x,y and z from the ligh'ts x,y,z direction.

So in short.....When the light is changing direction, i get the rotation values back so that i can apply that to the cone mesh.

##### Share on other sites

Can you give me a code example plz?

##### Share on other sites

I have this function i am working on:

lightType is "spotLight" OR "pointLight".

light is the actuall light itself.

pseudoMeshLight is either a cone mesh or a spere mesh.

``````function updatePseudoMeshLight(lightType,light,pseudoMeshLight){
pseudoMeshLight.position=light.position;
/*
HERE i want to get the direction of the light and transform it into a rotation so that the pseudo mesh is rotated
pseudoMeshLight.rotation=???
*/
}``````

##### Share on other sites

I think you can do this:

var quat = BABYLON.Quaternion.Identity();

light.getWorldMatrix().decompose(BABYLON.Vector3.Zero(), quat, BABYLON.Vector3.Zero());

mesh.rotationQuaternion = quat;

##### Share on other sites

And if i not want to use Quaternion ?

I am used to only use rotation.

mesh.rotation

NOT

mesh. rotationQuaternion

##### Share on other sites

quat.toEulerAnglesToRef(mesh.rotation);

I give it a try

##### Share on other sites

Sorry @Adam, but i don't get this to work.

I am working on a PG.

Maybe you can test it there later so you see what i mean.

I notify here when the PG is up.

##### Share on other sites

ok. my PG:

it is messy code right now, but you get the idea.

Only put in code between row 46 to 49.

##### Share on other sites

Updated my PG:

Still need the rotation to work!

##### Share on other sites

I been trying and trying to get tis to work, but i can't get it to work.

Is it not mathematically possible to get a light's vector3 direction and turn it into a rotation value?

Is it only mathematically possible to do the other way around? (a rotation value from a mesh to a light's vector3 direction?).

##### Share on other sites

assume that L1 is the vector direction of the light and X the X axis vector (1, 0, 0)

C1 = cross(L1, Y)  gives you a vector orthogonal to L1 and X

C2 = cross(L1, C1) gives you a vector orthogonal to L1 and C1, so (L1, C1, C2) is now an orthogonal system. Let's call it the target system

rotationFromAxis(L1, C1, C2) should return the rotation triplet to be applied to mesh to align it on this target system... If I'm wrong just swap C2 and C1

##### Share on other sites

Hi @jerome

Can you try to put the code in my PG?

Put it in between line 46 and 49.

##### Share on other sites

If we all could solve this...

Then people could do some more cool stuff with lights.

Imagine for example a moving texture material in a pseudo mesh for the light

Imagine a flashlight(having a alpha texture for the pseudo mesh)

Imagine a pulsating point light with a pseudo mesh growing and shrinking

Edit: well point lighs are no problem because  they cast light in every direction.

##### Share on other sites

I think you can do this:

var quat = BABYLON.Quaternion.Identity();

light.getWorldMatrix().decompose(BABYLON.Vector3.Zero(), quat, BABYLON.Vector3.Zero());

mesh.rotationQuaternion = quat;

This example that you gave me @adam returns 0,0,0

console.log(quat);

console.log(mesh.rotationQuaternion);

console.log(mesh.rotation);

##### Share on other sites

I try to strip that code down then, and see if it works.

Edit:

I still looking for code to put in between row 46 and 49 in my PG.

##### Share on other sites

I stripped  your PG down to this:

But i don't see how to affect spot.direction.z

##### Share on other sites

In my PG......the user sets the direction in x, y and z!

Why don't anyone understand what i try to accomplish?

i made my PG for getting help from someone to implement code between row 46 to 49 to make it work.

I am not good in this kind of math/calculations.

##### Share on other sites

6 minutes ago, DigiHz Data said:

In my PG......the user sets the direction in x, y and z!

Why don't anyone understand what i try to accomplish?

i made my PG for getting help from someone to implement code between row 46 to 49 to make it work.

I am not good in this kind of math/calculations.

Look at lines 71 and 72.

You need to have better forum etiquette.

##### Share on other sites

I did look at those lines.

And i not mean to be rude or anything like that.

I do appriciate any help from anyone.

I just don't know how to make it work.

Maybee it is not possible to get a light's x,y,z direction and get a rotation value back?

##### Share on other sites

Did you try using the getRotation function that I wrote for you?

##### Share on other sites

yes i did.

But when i use that function, the pseudo mesh jumps around when directions go over or under 0.

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.