Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by ziadantar90 · Jun 24, 2014 at 11:31 AM · vector3mathanglevector2line

Angle between two lines in 2D

Hey, I've got a problem dealing with the Vector2.Angle and the Vector3.Angle The thing is during runtime I define three points A,B and C which are in 2D world.

and eventually two lines will be used , (A,B) and (B,C) I'm trying to figure out the angle between those two lines. I've tried Vector2.Angle(B-A,C-B) and bunch of others, but I think I don't understand the essential concept of the Angle() method.

The picture will illustrate my point! Click Here

how to calculate Alpha ??

Thanks for your time

angle.png (6.1 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by rutter · Jun 24, 2014 at 07:04 PM

So you're describing a shape like this?

Three points

It's important to note that vectors have both length and direction. The vector from A to B is not the same as the vector from B to A. They have equal length, but opposite direction.

So, this bit:

 Vector2.Angle(B-A,C-B)

That's not running the comparison you might expect, because one of your vectors is pointing "backwards". You end up calculating the supplementary angle:

Supplementary angle

So, what you probably want is this:

 Vector2.Angle(B-A, B-C);



three points.png (3.0 kB)
three points supp (1).png (4.3 kB)
Comment
Add comment · Show 1 · 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 ziadantar90 · Jun 25, 2014 at 08:27 AM 0
Share

Awesome man.... Thanks a lot, it did the trick

Have a good one !

avatar image
0

Answer by Zoodinger · Jun 24, 2014 at 01:24 PM

The dot product between two vectors returns the cosine of the angle.

 float dot = Vector2.Dot(A, B);
 float angleInRadians = Mathf.Acos(dot);
 float angle = Mathf.Rad2Deg(angleInRadians); //to degrees

Note: A and B need to be NORMALIZED for this to work!

Keep in mind that this will only produce angles between 0 and 180 degrees. You'll need to do some additional trickery to compensate for that. For 2d vectors this is not too hard:

 Vector3 cross = Vector3.Cross(new Vector3(A.x, A.y, 0), new Vector3(B.x, B.y, 0));

cross.z will be positive if the rotation is clockwise or negative if the rotation is counter-clockwise. Or the other way around, I'm not sure :P. Either way, you'll be able to figure out what to do in case you want to detect angles larger than 180 degrees.

Comment
Add comment · 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
0

Answer by Ankit Priyarup · Jun 24, 2014 at 03:14 PM

It's very simple with physics. Let two lines be vector a and vector b then the angle cos theta will equal to A.B(dot product)/a mode cross product to b mode.

Comment
Add comment · Show 1 · 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 ziadantar90 · Jun 24, 2014 at 06:42 PM 0
Share

The problem is I can't seem to struct the lines A line from A to B is for example : Vector2 AB = A+B; or Vector2 AB = A-B or something else ?!

I mean logically I know it and mathematically but to apply it in unity that's where's my pickle

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I find slope on a spherical terrain 2 Answers

Find Vector3 from a starting Vector3, angle, and distance. 1 Answer

Angle between 2 GameObjects and center of screen 2 Answers

Get angle relative to GameObject 1 Answer

Closest point on multiple lines? 2 Answers


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