Angle between X axis of object and a 3D point on plane with Three.js

Just remove the value of the rotation

Now you know how to get the angle between the blue axis and the blue sphere. Just adapt it to what you exactly want.

This example just shows you that the principle with Vector2.angle() works and how you can use it.

No I still donā€™t know how to do it because I need to take the account of the rotation. I can not remove the rotation itā€™s my requirement, lol. I need the angle between red axis and the black line.

in your last fiddle the angle between the red and black line is PI/2

Just add PI/2 to v and the black line will ā€œbecomeā€ the red line

Letā€™s only talk about only this fiddle to get rid of any confusion. I think what you are suggesting is different from what I am chasing behind.

Edit fiddle - JSFiddle - Code Playground

I need the angle between Box local X-Axis(red line) and black line connecting Sphere and Box Position which are respectively P and O.

As you can see the fiddle that I have rotated the box by 30 degree around its local Z-Axis. This 30 degree angle is not fixed it can be anything this is just to explain you.

And the requirement is, after the rotation around itā€™s local Z-Axis I want to get the angle between the X-Axis(red line) of the box and the black line.

Yeah you are right the angle is PI/2. We are finding it by manually observing it(or by reading the code). But we need to calculate programmatically that this angle is PI/2. It can be any angle. I need the formula to calculate it.

the red line is fixed relative to the box? It is the z axis of the box, right? Since it is fixed relative to the box, its angle relative to the x axis is PI/2. Since now you have your angle v between the box x-axis and the sphere. You have all you need right?

The red, green and blue lines are the local X, Y and Z-Axis of the Box respectively. I have rotated the box by -PI/2 around itā€™s local X-Axis just after adding the box in the scene. Now if I rotate the box around Z-Axis(the blue line pointing towards up) then the direction of X-Axis(the red line) keep changing and will form different angle between the X-Axis(the red line) and the Black line. And I need to find out the angle value.

In the fiddle at line number 27 you can see I have added
box.rotation.z = 30* Math.PI/180;
which makes the angle between the black line and X-Axis(the red line) around PI/2
But if I change it to
box.rotation.z = -30* Math.PI/180;
Then the angle between the X-Axis(the red line) and the Back line changes. Right?
Please try adding (-) sign in the fiddle before 30 at line 27.
You can see the difference between the angle in both cases. So my question is how find that value of angle?

We have already done that

Honestly I am not sure we have done it. May be I could not understand.

Would you please let me know if we change the line 27 to

box.rotation.z = -30* Math.PI/180;

Then what will be the value of the angle shown in the following image? How you are finding out. If you have the logic to find it out then that will be the answer.

The angle you are discribing there is discribe by 2 angles v and v2 (or relative to v and v2)

You mean that the red and black lines are not on the same plane.

No they are in same plane.

I mean they are both parallel to your yellow floor?

Yes they both are in same plane, the yellow floor.

You are right. Now I get your point. Answer is in this fiddle. I have derived the answer from your solution.

Edit fiddle - JSFiddle - Code Playground

The solution is,

  1. first calculate the vector angle like you have shown as v and v2,
  2. then add the rotation angle around the local Z-Axis to v then it will give the angle between X-Axis(the red line) and the Black line.

Thank you so so much for your time and patience. I am really sorry for wasting so much of your time.But I am glad to see such a helpful person like you around us.

Ok glad you got it right. It told you ā€œjust remove the value of the rotationā€ it was ā€œaddā€, okā€¦

The thing is I just gave you indications (post #6) and a way to use them. You helped yourself at the end

2 Likes

Thank you for your kind help. :slight_smile:

1 Like