Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Scharkee · Mar 31, 2011 at 06:05 AM · mathtriangle

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tom 17 · Mar 31, 2011 at 06:37 AM 0
Share

$$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?

avatar image Scharkee · Apr 01, 2011 at 05:56 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scharkee · Apr 02, 2011 at 04:05 AM 0
Share

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.

avatar image Owen-Reynolds · Apr 02, 2011 at 07:16 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scharkee · Mar 31, 2011 at 06:19 AM 0
Share

I'm trying to get measurements and angles on a 2d plane though.

avatar image Tom 17 · Mar 31, 2011 at 06:29 AM 0
Share

SirGive: Oh please, a normal is not a position. If this dude is a little confused allready please use exact wording yourself.

avatar image SirGive · Mar 31, 2011 at 02:38 PM 0
Share

yeah, i meant direction. my bad. i posted that half asleep. >_<

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Finding a specific point on the edge of a triangle 1 Answer

Calculating triangle side length based on hypotenuse? 1 Answer

calculus help angels of a triangle 1 Answer

Math question, center of triangle that is part of a square 1 Answer

Why does this simple calculation not show decimals? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges