Jump to content

Calculating rotation from direction


DigiHz Data
 Share

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.

Link to comment
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=???
   */
}

 

Link to comment
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?).

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

18 hours ago, adam said:

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);

Link to comment
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.

 

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...