I am creating an XNA game but am a little bit confuse working differently from the standard Cartesian coordinate when it comes to angle rotation.
Let say I have the following and I want to find the angle between those vectors
Vector2 p1 = new Vector2(111, 0);
Vector2 p2 = new Vector2(223, 110);
float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
Debug.WriteLine(MathHelper.ToDegrees(angle).ToString());
I would imagine that angle should be close to 90 degree but it gives 44.48384.
Come to think of it, am I correct to assume that it's the angle between the horizontal line i.e Vector2(0,0) and Vector2(111, 0) and Vector2(0,0) and Vector2(223, 110)?
However if I calculate the angle using tan x = 110 / 223 the angle I get is 26.25. What am I missing?
The angle is 44.48384°, if you are measuring the angle around the origin (as you would expect, since "angle between two vectors").
Maybe you have your definition of which angle you wish to calculate envisioned incorrectly as I'm struggling to see how these two points could form the angle of 90°.
Here's a quick sketch I did on paper of these two points (please excuse the scruffiness!):
I have the Y coordinates going in the wrong direction but the problem is the same.
Related
I am trying to code Canny edge detection in C#. I am confused about finding the Direction of the edge. I know The Direction is the angle between the gradient vector and the x-axis. When finding the gradient vector's Direction (D), you do inverse tan of the y/x ratio.
I understand that part. The part that I am lost at is some examples online seem to add PI out of nowhere, and I do not understand. So is it :
` double angle = X/ Y;
double radians = angle * (180/Math.PI);
Angle = Math.Tan(radians) + Math.PI;`
or this:
double angle = X/ Y;
double radians = angle * (180/Math.PI);
Angle = Math.Tan(radians);
I have also seen :
orientation[index] = Math.Atan2(dy, dx) + Math.PI;
but this is not even inverse tan or is it ?
Both your first examples don't make any sense. X / Y is surely not an angle but the tangens of an angle.
To get the angle from the tangens you must use the one of the two inverse functions Math.Atan or Math.Atan2.
Always prefer Math.Atan2 over Math.Atan if you have got both components (X and Y) of the direction vector because Math.Atan2 returns an unambiguous angle in the range for -Pi to +Pi, while Math.Tan only gives you a result in the range form -Pi/2 to +Pi/2. Then you have to decide if in a certain case you have to add or subtract Pi (ATan doesn't "know" whether the angle is in the first or in the third quadrant if the result is positive or whether it is in the second or fourth quadrant if the result is negative).
Try to learn how to work with trigonometric functions and when you understand how to apply them and the corresponding inverse trigonometric functions come back to your programme.
And read the Microsoft documentation concerning the trigonometric functions.
I am currently using Atan2 to calculate the player heading angle.
however after some trial and error I discovered that the in-game angle's are rather different to that of a "normal" lay out :
ReturnedAngle = Math.Atan2(Y2 - Y1, X2 - X1); /// ArcTan2 the difference in our Y axis is always passed first followed by X
ReturnedAngle = (180 / Math.PI) * ReturnedAngle; /// Converting our radians to Degrees the convervion ends at 358 not the full 360 degrees.
ReturnedAngle = Math.Round(ReturnedAngle + 360, MidpointRounding.AwayFromZero) % 360; /// MOD and round our angle.
Above is the C# code I am using to calc the heading angle. My questions is how would I go about converting this angle from the "normal" angle system to the in-game one.
I think this is your situation. You have a right-hand coordinate system, but you are measuring a clock-wise angle, which is inconsistent.
In any case, draw a small positive angle from 360 (red below) to form a right triangle (purple below) with positive sides.
To measure the angle θ of the triangle, measure the short side Δx and the long side Δy and compute.
var θ = Math.Atan2(Δx, Δy);
This would work for any positive or negative values for the two sides. For example, if the angle goes above 90° then Δy would flip signs, as your target point is going to be below the origin. But the beauty of Atan2() is that you don't need to worry about these cases as it works on all four quadrants if you make it work for a small positive angle.
In reverse you have
var Δx = R*Math.Sin(Θ);
var Δy = R*Math.Cos(Θ);
where R is the distance between the target and the reference point.
Math.Atan2(Y2 - Y1, X2 - X1) computes the angle anticlockwise from the x axis. Math.Atan2(X2 - X1, Y2 - Y1) computes the angle clockwise from the y axis, which is what you want.
The 'clockwise from north' convention is used in navigation and mapping. Over the years I've found that it easiest to think in terms of vectors having components north, east. This means that atan2 is called the same way, that is, to get the direction of q from p:
dirn = atan2( q[1]-p[1], q[0]-p[0]);
If you are thinking of p and q as x,y vectors this gives you the angle anti-clockwise from the x axis. If you are thinking of p and q as n,e vectors it gives you the angle clockwise from north.
It also means that the formulae for a rotation matrix is the same. To rotate through an angle a, you use the matrix
R = ( cos(a) -sin(a) )
( sin(a) cos(a) )
Again, if you are thinking of the vectors as being x,y then applying R rotates through and angle a, anti-clockwise from the axis, while if you think of vectors as being n,e applying R rotates through an angle a, clockwise from north.
I have read some of the duplicate answers about angle between two vectors, but I'm still stuck with my problem. I have two vectors and I want that the angle between them to always be 90 degrees. To achieve that I need to find the angle between them, so that I can subtract or add the correct amount of degrees so that the angle between them always is 90 degrees.
The picture illustrates a sprite and two vectors. How do I find the angle A between them two? I have tried to use this code to get the angle between two vectors, but I must have missed something out, because I don't get the correct results:
public float GetAngleBetween (Vector2 A, Vector2 B)
{
float DotProd = Vector2.Dot (A, B);
float Length = A.Length () * B.Length ();
return (float)MathHelper.ToDegrees ((float)Math.Acos (DotProd/Length));
}
Any input is welcome and thank you in advance for any answers.
The actual angle in radians is
Math.ACos(Vector2.Dot(a, b));
Make sure that a and b are normalized vectors or the results can get pretty weird.
I think you may be looking for the Vector2.Dot method which is used to calculate the product of two vectors, and can be used for angle calculations.
For example:
// the angle between the two vectors is less than 90 degrees.
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) > 0
// the angle between the two vectors is more than 90 degrees.
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) < 0
// the angle between the two vectors is 90 degrees; that is, the vectors are orthogonal.
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 0
// the angle between the two vectors is 0 degrees; that is, the vectors point in the same direction and are parallel.
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 1
// the angle between the two vectors is 180 degrees; that is, the vectors point in opposite directions and are parallel.
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == -1
Is this what you're looking for, or do you need the exact angle?
If I understand your question diagram and comments, the Dot product and Acos are not the only bits of info you need. You also need to account for when the sprite is not located at (0,0).
float angleInRadians = (float) Math.Acos(Vector2.Dot(Vector2.Normalize(vector1 - spritePosition), Vector2.Normalize(vector2 - spritePosition)));
int angleInDegrees = MathHelper.ToDegrees(angleInRadians);
I have a Max Angle and a Min Angle, and also a unit vector pointing in some direction (2D).
How do I find out if this normal vector is between the two angles?
I don't know c#, but I know math:
Suppose the vector's coordinates are (x,y), and it is a unit vector, so |(x,y)| = 1.
The angle a between the positive x-axis and the vector is
a = atan2(y,x)
where atan2 is the four-quadrant arctangent. You can then check if this angle is between your max and min angles (provided they are also defined with respect to the positive x-axis).
Note that a is in radians; if your angles are given in degrees, you should first compute
a = a*180/pi;
where pi = 3.1415..... of course.
Does this help at all?
Calculate the angle from the dot product (this is easy in 2d) and then compare to your angle range.
I have to find the axis and angle of rotation of a camera with an UP and Direction vector(They both are perpendicular to each other). I have the initial and final positions of the UP and direction vectors of the camera that is rotated. I want to find the axis and angle of the rotation for the camera. I am using C# for my project. I am new to this 3D rotation. So pardon my questions if you find them silly.
From the direction (forward) vector f and up vector u you can get the side vector s by performing a vector cross product (s = f x u). All three vectors are now orthogonal. You should also make them orthonormal by normalizing each one of them. Taken together, these vectors form an orthonormal basis.
You now have two such basis: the one from your initial camera orientation and the one from your final camera orientation. Both basis can be represented as a rotation matrix. A rotation matrix is simply a 3x3 matrix where the 3 rows are respectively:
The forward vector
The up vector
The side vector
For example, the matrix:
[[1 0 0]
[0 1 0]
[0 0 1]]
could be your initial camera orientation at start-up with its forward vector, up vector and side vector pointing towards the positive x axis, y axis and z axis, respectively.
You can now convert these two basis (M1 and M2) to two unit quaternions (Q1 and Q2) using this algorithm which takes care about potential problems like divides by zero.
At this point, you have two unit quaternions representing your initial and final camera orientation. You must now find the quaternion qT that transforms Q1 into Q2, that is:
q2 = qT * q1
q2 * q1^-1 = qT * (q1 * q1^-1) = qT
=> qT = q2 * q1^-1
Knowing that the inverse of a unit quaternion is equal to its conjugate:
q1^-1 = q1* iif ||q1|| = 1
qT = q2 * q1^-1 = q2 * q1*
There is a single step left: extracting the axis and angle from quaternion qT:
angle = 2 * acos(qw)
x = qx / sqrt(1-qw*qw)
y = qy / sqrt(1-qw*qw)
z = qz / sqrt(1-qw*qw)
The angle is, of course, given in radian. Beware of the divide by zero when calculating x, y and z. This situation would happen when there is no rotation or a very small one, so you should test if angle > epsilon where you would choose epsilon to be quite small an angle (say 1/10 of a degree) and not calculate the vector if that is the case.