How do I break a moving objects speed up into its X and Y velocity given a fixed speed/angle? - math

Math escapes me today.
How do I find the X speed and the Y speed of an object if it is going at a defined speed (say, 5 pixels/second) at a 45 degree angle?

So always 5 pixels/sec and always 45 degrees?
The general case is
velx=cos(a)*vel;
vely=sin(a)*vel;
a is angle, usually in radians, so convert from degrees, and the signs (positive/negative) will depend on your coordinate system.
Crazy fact from the 1980s: In the old days, we used lookup tables for sin and cos!
Edited: Made my axes more conventional thanks to comment below. x is positive to your right. y is positive up. 45 degrees is to the northeast. If you have something else, let me know.

It will be
Vx=VCos#
Vy=Vsin#
So in your case it will be Vx=5*cos45 and Vy=5*sin45
At 45 angle value of Cos & Sin is same i.e 1/root 2.
Note: If you are doing any math stuff in programming then have a look at Vecmath lib.

At a 45 degree angle, an object is going sqrt(2)/2 of the speed along each axis. Generally, you can do it with sin and cosine, but for specific angles like this you can do it just by knowing pythagorean triangles.
In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. You know the hypotenuse is V. You also know that the other two sides equal each other. That means that V^2 = Vx^2 * 2. This means that Vx = sqrt(V^2/2), which equals V * sqrt(1/2).

Related

Given two coordinates, how can you find the angle from the vertical (like a clock face)

I am making a simple birds-eye 2D game where the character can face any direction, the direction the character is facing will be given in radians.
Given two sets of coordinates (point a and b) how do I find the angle between the line directly vertical from a and the line produced from a to b? This angle will be from 0 to 360 (although 360 will be treated as 0).
http://i.stack.imgur.com/J9TAU.png
In this diagram point a is the centre and point b is the one on the edge. The line extending from a is the 0 position and the line which you will work out the angle from. The point b could be anywhere on the circle and I need to find the radians of the angle to the right of the 0 line.
It's been a while since I studied it back in school, but if point B is on the unit circle, it should be a pretty trivial math problem.
A - If it's in radians, it won't be 0-360!
B - If Y >= 0, angle = arccos(x); else angle = TWOPI - arccos(x)
The usual formulation is that angle=0 is along +X, rather than +Y. You'll have to tinker a bit to get what you want. But that should be plenty to nudge you in the right direction.
You can use the atan function that many programming languages have. From vertical, the angle to point B will be:
pi/2 - atan(x, y)
Where x and y are the coordinates of point B with respect to A. Note that this might be negative, so you'll need to do some modular arithmetic to get it positive (if you care about that.)
Also, this isn't really an appropriate question for StackOverflow, since it's a math question and not a programming one.

The X angle between two 3D vectors?

I have two 3D vectors called A and B that both only have a 3D position. I know how to find the angle along the unit circle ranging from 0-360 degrees with the atan2 function by doing:
EDIT: (my atan2 function made no sense, now it should find the "y-angle" between 2 vectors):
toDegrees(atan2(A.x-B.x,A.z-B.z))+180
But that gives me the Y angle between the 2 vectors.
I need to find the X angle between them. It has to do with using the x, y and z position values. Not the x and z only, because that gives the Y angle between the two vectors.
I need the X angle, I know it sounds vague but I don't know how to explain. Maybe for example you have a camera in 3D space, if you look up or down than you rotate the x-axis. But now I need to get the "up/down" angle between the 2 vectors. If I rotate that 3D camera along the y-axis, the x-axis doens't change. So with the 2 vectors, no matter what the "y-angle" is between them, the x-angle between the 2 vectors wil stay the same if y-angle changes because it's the "up/down" angle, like in the camara.
Please help? I just need a line of math/pseudocode, or explanation. :)
atan2(crossproduct.length,scalarproduct)
The reason for using atan2 instead of arccos or arcsin is accuracy. arccos behaves very badly close to 0 degrees. Small computation errors in argument will lead to disproportionally big errors in result. arcsin has same problem close to 90 degrees.
Computing the altitude angle
OK, it might be I finally understood your comment below about the result being independent of the y angle, and about how it relates to the two vectors. It seems you are not really interested in two vectors and the angle between these two, but instead you're interested in the difference vector and the angle that one forms against the horizontal plane. In a horizontal coordinate system (often used in astronomy), that angle would be called “altitude” or “elevation”, as opposed to the “azimuth” you compute with the formula in your (edited) question. “altitude” closely relates to the “tilt” of your camera, whereas “azimuth” relates to “panning”.
We still have a 2D problem. One coordinate of the 2D vector is the y coordinate of the difference vector. The other coordinate is the length of the vector after projecting it on the horizontal plane, i.e. sqrt(x*x + z*z). The final solution would be
x = A.x - B.x
y = A.y - B.y
z = A.z - B.z
alt = toDegrees(atan2(y, sqrt(x*x + z*z)))
az = toDegrees(atan2(-x, -z))
The order (A - B as opposed to B - A) was chosen such that “A above B” yields a positive y and therefore a positive altitude, in accordance with your comment below. The minus signs in the azimuth computation above should replace the + 180 in the code from your question, except that the range now is [-180, 180] instead of your [0, 360]. Just to give you an alternative, choose whichever you prefer. In effect you compute the azimuth of B - A either way. The fact that you use a different order for these two angles might be somewhat confusing, so think about whether this really is what you want, or whether you want to reverse the sign of the altitude or change the azimuth by 180°.
Orthogonal projection
For reference, I'll include my original answer below, for those who are actually looking for the angle of rotation around some fixed x axis, the way the original question suggested.
If this x angle you mention in your question is indeed the angle of rotation around the x axis, as the camera example suggests, then you might want to think about it this way: set the x coordinate to zero, and you will end up with 2D vectors in the y-z plane. You can think of this as an orthogonal projection onto said plain. Now you are back to a 2D problem and can tackle it there.
Personally I'd simply call atan2 twice, once for each vector, and subtract the resulting angles:
toDegrees(atan2(A.z, A.y) - atan2(B.z, B.y))
The x=0 is implicit in the above formula simply because I only operate on y and z.
I haven't fully understood the logic behind your single atan2 call yet, but the fact that I have to think about it this long indicates that I wouldn't want to maintain it, at least not without a good explanatory comment.
I hope I understood your question correctly, and this is the thing you're looking for.
Just like 2D Vectors , you calculate their angle by solving cos of their Dot Product
You don't need atan, you always go for the dot product since its a fundamental operation of vectors and then use acos to get the angle.
double angleInDegrees = acos ( cos(theta) ) * 180.0 / PI;

About Sines in Programming

I am trying to understand trigonometry and the short answer is that I do not.
I drew a little triangle to mess around with and I asked myself the question, "If I know the length of the hypotenuse and the angle, how do I find the length of the other edges?".
Then I started reading. Apparently, the sine of angle A is supposed to equal the length of the opposite side divided by the length of the hypotenuse. So I figured that, using a right triangle, multiplying the length of the hypotenuse by the sine of the angle would yield the length of the opposing side.
1.414 / 1 = .707blahblah * 1.414 = 1 on my calculator.
But in every programming language I try sin(45.0) equals .8somethingsomething. I tried c++, c#, java, php, and lua.
Is the input not being interpreted as degrees? What unit is being used and how do I convert it? I've been seeing the word Radians, it would be helpful if someone could explain what a Radian is.
Radians are units of angular measure, like degrees, except that while there are 360 degrees in a circle, there are 2*pi (about 6.28) radians in a circle. You can convert degrees to radians by multiplying by pi (3.14159) and dividing by 180.
The formula works if the triangle is a right triangle, and yes, most programming languages expect radians rather than degrees as arguments to functions like sin() and cos().
Regarding the argument in the comments below: if you fix angle <BAC, side AB, and side BC, you can see that there are two possible positions for point C which preserve the the length D2 for side BC. Therefore <BAC, D1, and D2 do not fully determine a triangle.
The input to sin functions generally is expected in radians, not degrees. For example, in the Java documentation for sin it's stated that:
Parameters:
a - an angle, in radians.
Convert the angle in degrees to radians first, by multiplying it by pi/180
A radian is the distance of the radius of a circle along its circumference. Since a circle's circumference is 2 times pi times its radius, there are 2 times pi radians in one complete circle.
Yes, you are correct. Those functions all take their input in radians, not degrees.
You can convert degrees to radians by multiplying the degrees by π/180.
Convert to radians: Radian = degree/180*Pi
In order to convert from degrees to radians, divide the number in degrees by 180 and multiply by pi.

Determining if a spherical triangle is obtuse

Given two points, A and B, defined by longitude and latitude I want to determine if another point C is ~between~ A and B. ~between~ is hard for me to define. I don't mean on the line - it almost certainly won't be.
Geometric diagram http://www.freeimagehosting.net/uploads/b5c5ebf480.jpg
In this diagram, point C is ~between~ A and B because it is between the normals of points A and B and the line between them (normals denoted by thin line). Point D is not ~between~ A and B but it is ~between~ B and F.
Another way of saying this is that I want to determine if the triangles ABC and ABD are obtuse or not.
Note that the points will be very close together - within 10s of metres normally.
I'm thinking that the law of haversines may help but I don't know what the inverse of haversine is.
Many thanks for all help.
First, start with translating your points to local tangent plane. We will use the fact that your triangles are much smaller than the earth's radius. (Tangent space is such that equal deltas in each of the two coordinates correspond to equal distances)
This is done by dividing longtitudes by sin(lat):
A_local_x = A_lat_rads;
A_local_y = A_lon_rads/sin(A_lat_rads);
Then,
Compute lengths:
double ABsquared = (A_local_x - B_local_x)*(A_local_x - B_local_x) + (A_local_y - B_local_y)*(A_local_y - B_local_y);
double BCsquared = ..., ACsquared.
Finally:
bool obtuse = (ABsquared+BCsquared < ACsquared) || (ABsquared+ACsquared < BCsquared);
Obtuse means "it is not within the line", as you say. I am not checking whether triangle ABC is obtuse, but whether the angles at B and at A are obtuse. That's it.
note: I haven't tested this code. Please tell me how it works by plugging different points, if there's a bug I will fix it.
If your points are very close—10s of meters could easily qualify—you may be able to approximate it as a 2-d problem, and just calculate the angles CAB, θ and CBA, φ (using dot product). If both θ and φ are less than π/2, you C is "between".
cos(θ) = (AC · AB) / (|AC| |AB|)
If that approximation isn't good enough for you, you will need spherical trigonometry, which is also not too hard.
Note that if I understood your problem correctly, you need to check if the angles CAB and CBA are acute, not that the angle ACB is obtuse or acute.

How do you calculate the reflex angle given two vectors in 3D space?

I want to calculate the angle between two vectors a and b. Lets assume these are at the origin. This can be done with
theta = arccos(a . b / |a| * |b|)
However arccos gives you the angle in [0, pi], i.e. it will never give you an angle greater than 180 degrees, which is what I want. So how do you find out when the vectors have gone past the 180 degree mark? In 2D I would simply let the sign of the y-component on one of the vectors determine what quadrant the vector is in. But what is the easiest way to do it in 3D?
EDIT: I wanted to keep the question general but here we go. I'm programming this in c and the code I use to get the angle is theta = acos(dot(a, b)/mag(a)*mag(b)) so how would you programmatically determine the orientation?
This works in 2D because you have a plane defined in which you define the rotation.
If you want to do this in 3D, there is no such implicit 2D plane. You could transform your 3D coordinates to a 2D plane going through all three points, and do your calculation inside this plane.
But, there are of course two possible orientations for the plane, and that will affect which angles will be > 180 or smaller.
I came up with the following solution that takes advantage of the direction change of the cross product of the two vectors:
Make a vector n = a X b and normalize it. This vector is normal to the plane spanned by a and b.
Whenever a new angle is calculated compare it with the old normal. In the comparison, treat the old and the current normals as points and compute the distance between them. If this distance is 2 the normal (i.e. the cross product a X b has flipped).
You might want to have a threshold for the distance as the distance after a flip might be shorter than 2, depending on how the vectors a and b are oriented and how often you update the angle.
One solution that you could use:
What you effectively need to do is create a plane that one of the vectors is coplanar to.
Getting the cross product of both vectors will create a plane, then is you get the normal of this plane, you can get the angle between this and the vector you need to get the signed angle for, and you can use the angle to determine the sign.
If the angle is greater than 90 degrees, then it is below the created plane; less than 90 degrees, and it is above.
Depending on cost of calculations, the dot product can be used at this stage instead of the angle.
Just make sure that you always calculate the normals by the same order of vectors.
This is useable more easily if you're using the XYZ axes, and that's what you're comparing against, since you already have the vectors needed for the plane.
There are possbly more efficient solutions, but this is one I came up with.
Edit: clarification of created vectors
a X b = p. This is perpendicular to both a and b.
Then, do either:
a X p or b X p to create another vector that is the normal to the plane created by the 2 vectors. Choice of vector depends on which you're trying to find the angle for.
Strictly speaking, two 3D vectors always have two angles between them - one below or equal to 180, the other over or equal to 180. Arccos gives you one of them, you can get the other by subtracting from 360. Think of it that way: imagine two lines intersect. You have 4 angles there - 2 of one value, 2 of another. What's the angle between the lines? No single answer. Same here. Without some kind of extra criteria, you can not, in theory, tell which of the two angle values should be taken into account.
EDIT: So what you really need is an arbitrary example of fixing an orientation. Here's one: we look from the positive Z direction. If the plane between the two vectors contains the Z axis, we look from the positive Y direction. If the plane is YZ, we look from the positive X direction. I'll think how to express this in coordinate form, then edit again.

Resources