Ok, I am supposed to calculate the angle of a right triangle using Java. The measurements of the sides are a = 3 and b = 4 with the hypotenuse being c = 5. If I use Math.sin, it does not calculate the angle created by a and c. Is there another Math function I am not aware of? I have spent a long time trying to figure this out to no avail. Thanks in advance.
The probably source of the problem is that sin works in radians, rather than degrees.
To find the angle bounded by the side a and the side c, try using Math.toDegrees(Math.acos((double)a/c)). The passed value d to Math.acos(d) will have to be double.'Math.acos(d)' will give the arc cos in radian. Go through Math documentation
Related
I am currently trying to make som polygons rotate for my Asteroids game! :)
In order to do that, I'm using the AffineTransform setToRotation() method, however I am very confused about the meaning of the parameters. For setToRotation(a, b, c) I understand that b and c are the x and y coordinates of the point, the shape revolves around. a somehow makes the shape rotate, but it doesn't appear to be in degrees. So what else is it? And what to the other two setToRotation methods (setToRotation(a, b) and setToRotation(a)) do? I dont understand them AT ALL.
Thanks for every answer!
As in the documentation:
theta - the angle of rotation measured in radians
All of the trigonometric functions in java.lang.Math either accept or return radians.
You can convert from degrees to radians using Math.toRadians.
The other method overloads are also described in the documentation. Unless you can describe what about them you don't understand, there is no point in trying to explain them again, as that explanation could be in the same terms that you don't understand.
So what else is it?
It is in radians. You can see its documentation here. To convert from degrees to radians, just divide by 180 then times by π, so π radians is 180 degrees for examples. Or you can use Math.toRadians.
And what to the other two setToRotation methods (setToRotation(a, b) and setToRotation(a)) do?
Those are also very well documented. See this and this.
Basically, the one that takes one parameter is equivalent to calling setToRotation(a, b, c) but with b and c all equal to 0, and the one that takes 2 parameters is equivalent to calling setToRotation(a) with the inverse tangent of the quotient of the two parameters (setToRotation(Math.atan2(a, b))).
Ok, so on the internet, I have seen equations for solving this, but they require the normal of the plane, and are a lot higher math than I know.
Basically, if I have an x,y,z position (as well as x,y,z rotations) for my ray, and x,y,z for three points that represent my plane, How would I solve for the point of collision?
I have done 2D collisions before, but I am clueless on how this would work in 3D. Also, I work in java, though I understand C# well enough.
Thanks to the answer below, I was able to find the normal of my face. This then allowed me to, through trial and error and http://geomalgorithms.com/a05-_intersect-1.html, come up with the following code (hand made vector math excluded):
Vertice Vertice1 = faces.get(f).getV1();
Vertice Vertice2 = faces.get(f).getV2();
Vertice Vertice3 = faces.get(f).getV3();
Vector v1 = vt.subtractVertices(Vertice2, Vertice1);
Vector v2 = vt.subtractVertices(Vertice3, Vertice1);
Vector normal = vt.dotProduct(v1, v2);
//formula = -(ax + by + cz + d)/n * u where a,b,c = normal(x,y,z) and where u = the vector of the ray from camX,camY,camZ,
// with a rotation of localRotX,localRotY,localRotZ
double Collision =
-(normal.x*camX + normal.y*camY + normal.z*camZ) / vt.dotProduct(normal, vt.subtractVertices(camX,camY,camZ,
camX + Math.sin(localRotY)*Math.cos(localRotX),camY + Math.cos(localRotY)*Math.cos(localRotX),camZ + Math.sin(localRotX)));
This code, mathimatically should work, but I have yet to properly test the code. Tough I will continue working on this, I consider this topic finished. Thank you.
It would be very helpful to post one of the equations that you think would work for your situation. Without more information, I can only suggest using basic linear algebra to get the normal vector for the plane from the data you have.
In R3 (a.k.a. 3d math), the cross product of two vectors will yield a vector that is perpendicular to the two vectors. A plane normal vector is a vector that is perpendicular to the plane.
You can get two vectors that lie in your plane from the three points you mentioned. Let's call them A, B, and C.
v1 = B - A
v2 = C - A
normal = v1 x v2
Stackoverflow doesn't have Mathjax formatting so that's a little ugly, but you should get the idea: construct two vectors from your three points in the plane, take the cross product of your two vectors, and then you have a normal vector. You should then be closer to adapting the equation to your needs.
I have read how to calculate the triangle with 3 points and its angles. The problem is, I have tried different approaches and still get strange results. That can only mean I am doing something simple terribly wrong. But I am too much into my programming to see it; maybe anyone of you can help me?
I have 3 points: u, v and w
u---------------v
w is under u and v. I now want to get the angle in ° at w, which spans the triangle to u and v.
The coordinates are defined by u(ux,uy) v(vx,vy) w(wx,wy)
double dn =
Math.sqrt(Math.pow(ux-wx, 2)+Math.pow(uy-wy, 2))
* Math.sqrt(Math.pow(vx-wx, 2)+Math.pow(vy-wy, 2));
double beta = Math.acos(((ux-wx)*(vx-wx)+(uy-wy)*(vy-wy))/(dn));
beta is always less than 3.5, so I can't really figure out what I did wrong. Or did I understand the formula wrong? All variables are double, so that can be excluded I think.
I used:
(source: matheplanet.com)
.
Firstly lets say I have forgotten most of my maths long ago. I do not need to understand it in depth. The question was asked and answered (see links) but I do not want to derive my own function, is there an existing one ? The image below show a Matrix which I have rotated 45 degrees. Is there a way I could plug in the 0.707 numbers and get 45 back ? At the moment I am keeping track of the rotation on my own (simple solution), but I would prefer a function to derive back the 45 degrees
question 7291053
Matrix Rotation
The function is called "arcus sinus" or arcsin(x). For arcsin(0.707107) = 45 with a bit of rounding error.
In Java Math library you must additionally translate the result from RAD to DEG like this:
Math.asin(0.707) * 180d / Math.PI
Note that you get back something between -90° and +90° as described here.
If you want to know which axis you actually rotate and on which part (lower, upper) of the circle you are, then you must take a look at all 9 values. See here how the matrices look like for each axis.
For the angles of each axis x,y,and z
[0][1][2]
[3][4][5]
[6][7][8]
double x = Math.atan2([7], [8]);
double y = Math.atan2(-[6],Math.sqrt((Math.pow([7],2)+Math.pow([8],2)));
double z = Math.atan2([0], [3]);
then multiply the one you choose by 180/PI
I'm creating a very very simple game for fun. Realizing I needed the trajectory of an object given an angle and a velocity, it seemed logical to use this parametric equation:
x = (v*cos(ø))t and y = (v*sin(ø)t - 16t^2
I know that this equation works for a trajectory, but it isn't working with most ø values I use.
Do java angles work any differently from normal angle calculation?
My goal is for the object to start from bottom left of the window and follow an arc that is determined by the speed and angle given. However it tends to go strange directions.
The value of ø should be horizontal at 0 degrees and vertical at 90, and in the equation it refers to the angle at which the arc begins.
This is my first ever question post on this site, so if I'm missing anything in that regard please let me know.
Here is the calculating part of my code
not shown is the void time() that counts for each 5ms
also I should mention that the parX and parY are used to refer to the x and y coordinates in an unrounded form, as graphics coordinates require integer values.
Any help is much appreciated, and thank you in advance!
public void parametric()
{
parX = (float) ((speed*cos(-ø))*time);
gravity = (time*time)*(16);
parY = (float) ((float) ((speed*sin(-ø))*time)+gravity)+500;
xCoord = round(parX);
yCoord = round(parY);
}
Do java angles work any differently from normal angle calculation?
You only need to read the docs
public static double cos(double a)
Parameters:
a - an angle, in radians.
I guess you are using degrees instead of radians?