- Home /
Calculating a right triangle trouble...
Update--4/1/11
Just running Mathf.Cos(45) returns 0.525322, when WebMath and my desktop calculator say its suppose to be 0.70710.
Debug.Log(Mathf.Cos(45));
Simplified code
var Point_A:Vector2 = Vector2(0,0);
var Point_B:Vector2 = Vector2(5,5);
var dir_a_to_b:Vector2 = (Point_B - Point_A);
var Angle_A:float = Vector2.Angle(dir_a_to_b, Vector2(1,0));
var Angle_C:float = 90.0;
var hypo:float = Vector2.Distance(Point_A, Point_B);
var adj:float = hypo * Mathf.Cos(Angle_A);
Debug.Log(Mathf.Cos(Angle_A));
I'm trying to calculate a right angle triangle but my math keeps coming back wrong.
//this cube sits at (0,5,0) but everything should be calculated on the x and z axis...so (0,0)
Cube_From = GameObject.Find("Cube_From");
Cube_From.renderer.material.color = Color.white;
//this cube sits at (0,5,5) or (0,5). Its 5 units down Cube_From's axis and is blue...so (0,5)
Cube_Z = GameObject.Find("Cube_Z");
Cube_Z.renderer.material.color = Color.blue;
//the angle between Cube_From to Cube_Z which returns 0.
CF_CZ = Vector2.Angle((Vector2(Cube_Z.transform.position.x,Cube_Z.transform.position.z) - Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z)), Cube_From.transform.up);
//this cube sits at (5,5,5) or (5x,5z)
Cube_XYZ = GameObject.Find("Cube_XYZ");
Cube_XYZ.renderer.material.color = Color.yellow;
//get the angle of the opposite...should be 45
CF_CXYZ = Vector2.Angle((Vector2(Cube_XYZ.transform.position.x,Cube_XYZ.transform.position.z) - Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z)), Cube_From.transform.up);
//get the lenght of the hypotenuse
hypo = Vector2.Distance(Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z), (Vector2(Cube_XYZ.transform.position.x,Cube_XYZ.transform.position.z)));
//get the length of side_a
Side_A = Vector2.Distance(Vector2(Cube_From.transform.position.x,Cube_From.transform.position.z), (Vector2(Cube_Z.transform.position.x,Cube_Z.transform.position.z)));
//Calculate the adjacent/side a
adj = hypo * Mathf.Cos(CF_CXYZ);
My math is really rusty so I'm not sure if the calculation is simplified correctly.
Wikipedia says Cos A = adj/hypo and WebMath says its simplified to adj = hypo*Cos(A) but i don't get 5 when unity calculates it. I get 3.7 ish in length.
$$anonymous$$aybe you should name your cube a square if you do mean 2D math. Anyhow from what I can decipher from you code you might want to use kind of a unitSquare and just scale and rotate the vectors. Back to school math: What do you take as given and what do you want to compute?
I guess the only thing known is point A, point B, and point C's angle which will be 90 degrees. You should be able to figure out the rest of the triangle but the math come back wrong.
Answer by Owen-Reynolds · Apr 02, 2011 at 01:56 AM
cos(45 degrees) is 0.707. cos(45 radians) is 0.52.
Unity thinks that 0 degrees is North (or forward) and goes clock-wise, so 90 degrees is right/east and 180 is south. Most game engines do the same.
Real math (Mathf.Sin and Cos) thinks that 0 is right and goes counter clockwise using radians, so 1.57 is North and 3.14 is left/west.
If you have an angle, A, you got from Unity (say rotation.euler.y is 10 degrees, meaning ahead and a little right,) you can convert to the "real" system using, ummm, A2 = A*(2*Mathf.PI/360)*-1+Mathf.PI/2
You can then use A2 (which is radians, CCW, 0=right) for trig functions. The results will be in distance, so don't need to be converted. Contrary-wise, if you use arcTangent for anything, it gives you the angle in "real" math radians, so needs to be converted back to gameEngine units with the opposite equation.
Awesome it works, thank you so much! $$anonymous$$y triangle is Point_A(0,0), Point_B(5,5), and Point_C(5,0), but when i change Point_B to (5,6) my adjacent changes to 6, which means my adjacent is on the y-axis and Point_C is actually (0,5)...right? But when i change $$anonymous$$athf.Cos(Angle_A); to $$anonymous$$athf.Sin(Angle_A); it seems to make my adjacent along the x-axis now...yeah? Oh math, i love you as much as I'm confused by you. Thanks for your time Reynolds.
Yes -- flipping sin/cos turns it by 90 degrees. If you think of the angle as a clock hand, cos is side-to-side distance and sin is up/down. Turn the clock on it's side and the hand spins exactly the same, but cos is now up/dpwn.
Answer by SirGive · Mar 31, 2011 at 06:09 AM
Trig functions are for 2D. What you want to play with are vectors. You'll need to know things like the cross product to get the adjacent side. Also, reflection angle and all to get the correct normal.
You could always use raycasting. It returns the normal, which would be the direction where you would want the cube to go.
I'm trying to get measurements and angles on a 2d plane though.
SirGive: Oh please, a normal is not a position. If this dude is a little confused allready please use exact wording yourself.
yeah, i meant direction. my bad. i posted that half asleep. >_<