I have 2 vector 3, A and B (this is to be used in 2D mode, so using vec3 or vec2 shouldn’t make much difference).
Together they form a line segment. I need to find how much the line segment is tilted. The image bellow should help understand my problem better.
I’ve tried using Vector3.Angle and Mathf.Atan2 but none worked.
For example, the first example (on the top left corner) on the image, the angle is 45º and of that I am sure, but neither Vector3.Angle or Atan2 gave me that result.
Any ideias?
Btw, although it looks like A is at (0, 0, 0), it isn’t. Both A and B can have any position, A is used as a reference point to be the angle from.
So… your problem, you’re missing a vector in your drawing.
That vector being the dashed line.
In 2D that usually is just considered the positive x-axis. It’s your angle off that. Which is what Mathf.Atan2 is going to give you. The vector we measure off of is implied (the information is maintained with out having to store it).
But in 3D, this becomes a little more difficult, because there is 3 dimensions. If you create a vector to represent a line between two points (as defined by two other vectors), and you measure the angle between it and some OTHER vector (such as the positive x-axis like we do in 2d). That angle is a 1-dimensional measure. It’s the angle between the two vectors on a plane that intersects BOTH vectors.
By taking this measure, we’ve lost information. We need new information to use it.
In 2D, the lost information is fine, because we know what plane it’s measured on. It’s the ONLY plane that exists in 2D. And we know the vector it’s measured off of, because that’s implied (the positive x-axis).
But in 3D there’s an infinite number of planes that exist. WHICH plane is this angle measured on? To know which plane, we must know the two points (vectors) the plane is defined as. If we imply one of the points by saying the x-axis, we still need to store the original line. The information isn’t as useful.
What I’m saying is if you take that lines measure off the 3d x-axis. What does that information really represent? How useful is that information to you?
To give you an example. Say we wanted to measure the gradient of a slope in 3d. We’re standing at the foot of gradient, so we have that position in 3-space. We also know the position at the top of the hill in 3-space. We subtract the two getting a vector from yourself to the top of the hill.
The grade will be what though? We’ll measure it as an angle off a vector facing from me in the direction I’m looking towards the hill, but with no incline.
Thing is that vector changes depending on which direction we face. If we turn southward, the ‘forward’ is different than when we face westward. So basically we get:
var pos1 = *my position*;
var pos2 = *top of the hill*;
var slope = pos2 - pos1;
var forw = slope;
forw.y = 0f;//remove the incline
var angle = Vector3.Angle(forw, slope);
Angle now has meaning. It’s the grade of the slope off the ground in the direction of the slope.
But it’s only useful in that context! If you try to apply that angle to anything other than that context, it’s not going to end up working the way you expect.
That’s the whole thing about geometry and angles. You need to know what you’re measure angles between, and what that means. Otherwise the angle doesn’t really mean much.
[edit]
note, in 2d… the dotted line would be the x-axis
Well, I don’t really know the meaning, but I can tell you what I’ll use the angle for.
I’m drawing a line segment on screen in 2D space.
Points A and B will be were I get the coordinates from.
A and B are dragable with the mouse, so the line will be changing angles constantly and I need to know it.
In order to make the line selectable/clickable I need to attach a PolygonCollider2D to it and create the colider points manually, like this:
Vector2[] points = new Vector2[4];
int index_pos = 0;
points [0] = new Vector2 (A.x, A.y - .5f);
points [1] = new Vector2 (A.x, A.y + .5f);
points [2] = new Vector2 (B.x, B.y + .5f);
points [3] = new Vector2 (B.x, B.y - .5f);
Line.GetComponent<PolygonCollider2D> ().points = points;
When I drag a point, I must update the collider as well, so I need to rotate all of the collider points Angle amount, for them to stay alligned with the line segment