I'm building a small Physics engine and I'm having trouble converting my Radian value to Degrees using atan, as I need an angle to output in Degrees only.
Firstly, I have an x and y value, and I need to find an angle using atan, so I divide y by x like so:
angleDivide = yN / xN;
Then, before putting this value into tan, I attempt to convert it to Degrees like this:
angleToDegrees = angleDivide * (3.14 / 180);
Then I place angleToDegrees into atan:
angle = atan(angleToDegrees);
But when I'm displaying angle, I'm, still getting radian values.
Please could you tell me what is wrong with my code and how to fix this?
You want to calculate radians=tan(y/x) first.
Then you can convert it to degrees:
radians = atan(y/x)
degrees = radians * (180.0/3.141592653589793238463)
See the reference here for atan:
On a side note, you also have to take into account what quadrant you are in to get the correct answer (since -y/x is the same number as y/-x)
Related
For example I have a value 0.70, and it is a sin() for 45 degrees. I need a function, that will calculate me an angle from a sin. What function from C++ can help me?
You can use std::asin. This returns a value between [-PI/2,+PI/2], to get degrees you multiply by 180 and divide by PI.
result = std::asin(value) * 180 / PI
PI = 3.1415926535
I am trying to optimize the simulation function in my experiment so I can have more artificial brain-controlled agents running at a time. I profiled my code and found out that the big bottleneck in my code right now is computing the relative angle from every agent to every agent, which is O(n2), minus some small optimizations I have done. Here is the current code snippet I have for computing the angle:
[C++]
double calcAngle(double fromX, double fromY, double fromAngle, double toX, double toY)
{
double d = 0.0;
double Ux = 0.0, Uy = 0.0, Vx = 0.0, Vy = 0.0;
d = sqrt( calcDistanceSquared(fromX, fromY, toX, toY) );
Ux = (toX - fromX) / d;
Uy = (toY - fromY) / d;
Vx = cos(fromAngle * (cPI / 180.0));
Vy = sin(fromAngle * (cPI / 180.0));
return atan2(((Ux * Vy) - (Uy * Vx)), ((Ux * Vx) + (Uy * Vy))) * 180.0 / cPI;
}
I have two 2D points (x1, y1) and (x2, y2) and the facing of the "from" point (xa). I want to compute the angle that agent x needs to turn (relative to its current facing) to face agent y.
According to the profiler, the most expensive part is the atan2. I have Googled for hours and the above solution is the best solution I could find. Does anyone know of a more efficient way to compute the angle between two points? I am willing to sacrifice a little accuracy (+/- 1-2 degrees) for speed, if that affects anything.
As has been mentioned in the comments, there are probably high-level approaches to reduce your computational load.
But to the question in hand, you can just use the dot-product relationship:
theta = acos ( a . b / ||a|| ||b|| )
where a and b are your vectors, . denotes "dot product" and || || denotes "vector magnitude".
Essentially, this will replace your {sqrt, cos, sin, atan2} with {sqrt, acos}.
I would also suggest sticking to radians for all internal calculations, only converting to and from degrees for human-readable I/O.
Your comment tells a lot: "I am simulating a 180 degree frontal retina for every agent, so I need the angle". No, you don't. You just need to know whether the angle between the position vector and vision vector is more or less than 90 degrees.
That's very easy: the dot product A·B is >0 if the angle between A and B is less than 90 degrees; 0 if the angle is precisely 90 degrees, and <0 if the angle is more than 90 degrees. Calculating this takes 3 multiplications and 2 additions.
i think it's more a mathematical problem:
try
abs(arctan((y1-yfrom)/(x1-xfrom)) - arctan(1/((y2-yfrom2)/(x2-xfrom2))))
Use the dot product of these two vectors and at worst you need to do an inverse cosine instead:
A = Facing direction. B = Direction of Agent Y from Agent X
Calculating the dot is simple multiplication and addition. From that you have the cosine of the angle.
For starters, you should realize that there are a couple of simplifications that can reduce the calculations a bit:
You need not calculate the angle from an agent to itself,
If you have the angle from agent i to agent j, you already know something about the angle from agent j back to agent i.
I have to ask: what does "agent i turn to face agent j" mean? If the two surfaces are looking right at each other, do you have to do a calculation? What tolerance do you have on "looking right at each other"?
It'd be easier to recommend what to do if you'd stop focusing on the mathematics and describe the problem more fully.
I want to ask what would be the best formula to convert mouse X,Y position into one of 16 directiones from player position.
I work in c++ ,sfml 1.6 so I get every position easily, but I dont know how to convert them based on angle from player position or something. (I was never good on math and for more than 4 directions if statements looks too complex).
Also I want to send it to server which converts direction back into delta X,Y so he can do something like:
player.Move(deltaX * speed * GetElapsedTime(), ...Y);
The "easiest" way would be to convert your two sets of co-ordinates (one for current player position, one for current mouse position) into an angle relative to the player's position, where an angle of 0 is the line straight ahead of the player (or north, depending on how your game works). Then each of your sixteen directions would translate to a given 22.5 degree interval.
However, since you said you're bad at math, I imagine you're looking for something more concrete than that.
You could use atan2 to get the angle between the mouse position and the positive X axis:
#include <cmath>
float player_x = ...;
float player_y = ...;
float mouse_x = ...;
float mouse_y = ...;
float angle = std::atan2(mouse_y - player_y, mouse_x - player_x);
The angle returned by std::atan2() is a value between -M_PI (exclusive) and M_PI (inclusive):
-M_PI Left (-180°)
-0.5 * M_PI Down (-90°)
0 Right (0°)
0.5 * M_PI Up (90°)
M_PI Left (180°)
You can transform this value depending on how you want your mapping to "one of 16 directions", i.e., depending on what value you want to assign to which discrete direction.
Given the angle, getting a unit vector to represent the X/Y delta is quite easy, too:
float dx = std::cos(angle);
float dy = std::sin(angle);
Hey so i'm doing some graphics math and inserting what i believe to be a float (i'm pretty sure the ways i manipulate the float beforehand is messing something up somehow...) into the function and getting strange, negative results back.
for instance when doing the following opperations when Angle initially equals 350.0, test ends up being
-.99. Why?
Angle= (float)(Angle-(int)Angle)+(float)((int)Angle%90);
// calculates x and y based on angle and Hypotenuse
float test= sin(Angle);
float test2= 1/(Speed*Time);
float test3= test/test2;
buffX= sin(Angle)/ (1.f/(Speed*Time));
buffY= sin(Angle-90)/ (1.f/(Speed*Time));
trying to keep Angle a float by putting (float) before everything didn't work... please help! Thanks!
That's because the C/C++ runtime function sin() expects the argument in radians, not degrees.
Convert to radians with:
float test= sin(Angle / 180 * M_PI);
etc.
sin takes its arguments in radians, not degrees. You need to take you number and multiply it pi/180
I'm not very good at math or geometry, but I want to draw some line segments at increasing angles. What I want to draw is something like when you hold your hand up and spread your fingers apart: lines that start at a common point and expand out at angles that have an equal difference between them.
I have tried this:
len = 300;
angle = 10;
for (i = 0; i <= 5; ++i) {
endPointX = 50 + len * Math.cos(angle);
endPointY = 50 + len * Math.tan(angle);
draw.Line(50, 50, endPointX, endPointY);
angle += 10;
}
However, that's totally wrong and produces something like this
http://i.stack.imgur.com/taX40.png
But I want something like this (bad mspaint, sorry):
http://i.stack.imgur.com/8xfpp.png
What's the right math for this?
There are two separate issues in your question, I will cover each.
Here's an ASCII picture of your situation:
B
+
/|
/ |
/ |
/ |
len / | y
/ |
/ |
/ |
/ __|
/ θ | |
+----------+
A x C
This is a right triangle. It has three sides:
The diagonal side in the picture opposite to the 90° angle is called the hypotenuse and has a length len. The hypotenuse is what you're trying to draw.
The vertical side is the side opposite to the angle θ and has a length y.
The horizontal side is the side adjacent to the angle θ and has a length x.
Given the above illustration the following equations are true:
cos(θ) = x/len
sin(θ) = y/len
These equations are another way of saying:
The cosine of an angle is equal to the length of the adjacent side divided by the length of the hypotenuse.
The sine of an angle is equal to the length of the opposite side divided by the length of the hypotenuse.
When solving the equation for x and y, you get:
x = len * cos(θ)
y = len * sin(θ)
So you want sin() and cos(), not cos() and tan(). If the point A is not at the origin, you would need to offset x and y by addition, like so:
x = len * cos(θ) + 50
y = len * sin(θ) + 50
With the values for x and y, you can find the coordinates for point B on the triangle, and thus be able to draw your lines.
Also, assuming you're programming in Java, the trigonometric functions in the Math class expect the angle in radians, not degrees. Lots of programming languages that provides trigonometric functions are like this.
Radians and degrees measure the same thing, but a complete rotation in degrees goes from 0 to 360° while a complete rotation in radians go from 0 to 2π.
To convert angles in degrees to radians, multiply the angle by π/180. In Java, the constant π is provided by Math.PI.
For example, an angle of 10° degrees is equivalent to 10 * π/180, or π/18 radians.
Firstly, you want cos and sin, not cos and tan.
Secondly, most maths libraries perform trigonometric functions in radians, not degrees. So 10 is a very large difference indeed! To convert from degrees to radians, multiply by (pi/180).
You shouldn't be using tan, but sin. If I remember correctly, it should be something like:
Math.cos(angle/180);
-Math.sin(angle/180);
The negative on sin is important.
The reason you are getting uneven looking angles is that every time you add 10 you're actually spinning the line around the circle 1.6 times.
The math functions expect angles to be in radians, not degrees.
360 degrees = 2*Math.PI radians.
Instead of 10, write "2*Math.PI/36.0"
Also, use sin instead of tan.