How to get angle of tube?

Lets say we have these 3 tubes https://www.babylonjs-playground.com/#165IV6#854

The tubes are positioned in x,z axis with y always 0.

How do I get their angle, or direction?

When I check the inspector the rotation is always (0,0,0) no matter how I place them.

Is there a better mesh I could create to better get the rotation?

The tubes are supposed to represent walls in 2D.

The direction is a vector. In your case, this is vector2 - vector1 of each tube path.
Each of your tubes has no rotation because you created it in a different direction from the other one in its own local space.

If you want to give a rotation, then (for instance) create a tube along a given path (say, along X axis), then rotate it around Z for example.

1 Like

In my app the user has full control of the tube direction so I cant set a rotation from before hand.

Can’t I get the euler angle along the world y axis? Or something similar? Because you are talking about the local axis yes?

Because you are talking about the local axis yes?

Yes, as Jerome said the difference between the end points will give you the tube direction.

1 Like

Oh I didnt get that.

Do I subtract the 2 path endPoints then with subtract() ?

Also, can I convert the vector to radians or degrees? Because I want to be able to do different other actions based on the direction. And the direction can be any direction in a full circle (360)

You can use the dot product with the vector (1, 0, 0) to get the angle in radians from the x axis

direct = end.subtract(start);
direct.normalize(); //unit vector
xvec = new BABYLON.Vector3(1, 0, 0);
angle = Math.acos(BABYLON.Vector3.
Dot(direct, xvec));

Must use unit vectors and result is between -pi and +pi if I remember correctly.

2 Likes

@JohnK your above equation is basically a refactor of the below?

03-16-14-07

When cos a moves to the other side of the equation it becomes arccosine?

So it ends up as this?

a = arccosine(||P|| * ||Q||)

Which ||P|| * ||Q|| is the DOT product?

Just making sure I understand how this works :slight_smile:

Not quite correct.

arccosine is the inverse of cos, ie it is what you apply to undo the cos function.

When cos α = X then arccosine(cos α) = α = arccosine(X);

when P is a vector ||P|| is the length of P

The dot product of P and Q is written as P.Q

You calculate the dot product using ||P|| ||Q||cos α i.e. multiply the lengths of P and Q and the cos of the angle between them.

P.Q = ||P|| ||Q||cos α since ||P|| ||Q|| is a scalar it follows that

cos α = P.Q / (||P|| ||Q||) and then

α = arccosine(P.Q / (||P|| ||Q||))

In Babylon.js

angle = BABYLON.Vector3.Dot(P, Q).scale(P.length() * Q.length())

When P and Q are unit vectors ||P|| = ||Q|| = 1 and this simplifies to

angle = BABYLON.Vector3.Dot(P, Q)

1 Like