Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Nov 09, 2019 at 02:19 PM by $$anonymous$$ for the following reason:

Other

avatar image
1
Question by $$anonymous$$ · Aug 15, 2016 at 03:44 PM · c#mathfloatvector2degree

Rotate a Vector2 around the Z axis on a mathematical level

alt text

alt text

This is what i want to achieve with a givin Vector2 and a float degree i want to get the new ("rotated") Vector2, if this makes anysense at all.

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

1 Reply

  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 15, 2016 at 04:04 PM

Well all you need is a "2x2 rotation matrix". This can be easily calculate manually like this:

 Vector2 Rotate(Vector2 aPoint, float aDegree)
 {
     float rad = aDegree * Mathf.Deg2Rad;
     float s = Mathf.Sin(a);
     float c = Mathf.Cos(a);
     return new Vector2(
         aPoint.x * c - aPoint.y * s,
         aPoint.y * c + aPoint.x * s;
     );
 }

This should rotate the point counter clockwise around the origin. If you wan to rotate clockwise you can either use a negative angle or flip the signs of the "sin" component

     return new Vector2(
         aPoint.x * c + aPoint.y * s,
         aPoint.y * c - aPoint.x * s;
     );

ps: I've written this from scratch without syntax checking. However it should work that way.

edit

If you want to rotate around a certain point you could do:

 Vector2 RotateAroundPivot(Vector2 aPoint, Vector3 aPivot, float aDegree)
 {
     aPoint -= aPivot;
     float rad = aDegree * Mathf.Deg2Rad;
     float s = Mathf.Sin(a);
     float c = Mathf.Cos(a);
     return aPivot + new Vector2(
         aPoint.x * c - aPoint.y * s,
         aPoint.y * c + aPoint.x * s;
     );
 }


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 Bunny83 · Aug 15, 2016 at 04:58 PM 2
Share

Another way would be to use a Quaternion (or the 2d equivalent: complex numbers). See this vid for an introduction into complex numbers and quaternions.

So you can simply do:

 Vector2 Rotate(Vector2 aPoint, aDegree)
 {
     return Quaternion.Euler(0,0,aDegree) * aPoint;
 }

Or with an extension class like this:

 public static class ComplexEx
 {
     public static Vector2 Complex$$anonymous$$ult(this Vector2 aVec, Vector2 aOther)
     {
         return new Vector2(aVec.x*aOther.x - aVec.y*aOther.y, aVec.x*aOther.y + aVec.y * aOther.x);
     }
     public static Vector2 Rotation(float aDegree)
     {
         float a = aDegree * $$anonymous$$athf.Deg2Rad;
         return new Vector2($$anonymous$$athf.Cos(a), $$anonymous$$athf.Sin(a));
     }
     public static Vector2 Rotate(this Vector2 aVec, float aDegree)
     {
         return Complex$$anonymous$$ult(aVec, Rotation(aDegree));
     }
 }

With that class you can rotate any Vector2 like this:

 Vector2 rotated = vec.Rotate(20f); // rotate "vec" by 20° counter clockwise.

Again, all written from scratch and not tested ^^-

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Infinity Math Result 0 Answers

How can I numerically solve an equation in Unity c#? 2 Answers

How can I use a float Variabel in an other Sript? 1 Answer

Help with Vector2.Lerp 2 Answers

Non physics determinism of floating point math across platforms 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