Search Unity

Vector rotation

Discussion in 'Scripting' started by AlbertoT, Oct 30, 2009.

  1. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    Hello

    Does Unity supply a command or a combinations of commands to rotate a vector by a given angle ?
     
  2. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    A vector is a point infinite point in space. In Unity of course a vector isn't infinite, however, the same basic rules apply, so you can't rotate a vector. I think what you want to do is rotate a Transform, to do that check out transform.Rotate(rotation : Vector3);
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    why can't you rotate a vector. Take (1,1) and then
    (1,-1) is a rotated (1,1) vector 90 degrees clockwise.

    you can create a quaternion (or a matrix) which rotates x degrees around an axis and then multiply your vector by it.
    Take a look at Quaternion.AngleAxis function for example
     
  4. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    I suppose it is possible to 'rotate' a vector depending on a base vector and value of rotation, but still, it isn't rotating it, its moving it ... with style : P
     
  5. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    I was looking for a function :

    Vector3 RotateVector(Vector3, Angle);

    The returned Vector is of course the input vector rotated by the given angle
    It is not that hard to code it ,I was just wondering whether Unity supplies a "smart" commands

    How can I use the "Quaternion.AngleAxis " function for example ?
     
  6. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    for a function like that you would need to also specify which axis to rotate around.
    here's an example:
    Code (csharp):
    1. var quat : Quaternion = Quaternion.AngleAxis(90,Vector3.forward);
    2. var vect : Vector3 = Vector3(0,1,0);
    3. vect = quat * vect;
    please note though that what Lab suggested will likely be simpler. There are a lot of functions for rotation and setting angles available for transform. You can have a dummy transform to apply rotations to and then just use its resulting position. Might be a better solution depending on what you are trying to do.
     
    40detectives and leni8ec like this.
  7. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    yes, thanks
    I meant rotation about the Y axis
     
  8. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    356
    Just wanted to point out a vector doesn't always represent a point in space. It can also represent a direction - such as a surface normal, or an object's velocity. For this reason, it's entirely valid to talk about rotating a vector.

    Hope this clears up any confusion.
     
    Graph, Pulov, Zimbres and 2 others like this.
  9. sikha

    sikha

    Joined:
    Jun 28, 2011
    Posts:
    20
    I have a litttle Offtopic question
    How to check object rotation coordinates? for example I have plane in scene with coordinates in world space

    what should i rote in code for check only RATATION coordinates?


    Code (csharp):
    1.  
    2. var bl1:Transform;
    3.  
    4. function Update () {
    5.  
    6.             if(bl1.eulerAngles(Vector3(90,180,0)))
    7.             {
    8.            
    9.                 Debug.Log("is OK");
    10.            
    11.             }
    12. }
    13.  
    14.  
     
  10. sikha

    sikha

    Joined:
    Jun 28, 2011
    Posts:
    20
    I think I have foun the answer!

    Code (csharp):
    1.  
    2.             if(bl1.rotation == Quaternion.Euler(90.0, 180.0, 0))
    3.             {
    4.            
    5.                 Debug.Log("is OK");
    6.            
    7.             }
    8.  
     
  11. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
  12. Iamdain

    Iamdain

    Joined:
    Feb 3, 2010
    Posts:
    90
    You can rotate vectors using Quaternions if you assume that the origin is the origin of rotation of the vector but there aren't any math functions available to perform these rotations on vectors in Unity. Dummy objects have worked fine for me and as a bonus you have a visual representation of the transform in the editor...
     
  13. wpennypacker

    wpennypacker

    Joined:
    Feb 18, 2013
    Posts:
    2
    Not getting caught up in the debate about whether vectors can only represent points, whether you can rotate them and whether you should (they can, you can, and its often useful to), here is an easy way to add functions to do so (in C#). Toss these in a utility class-


    Code (csharp):
    1.     public static void RotateX( this Vector3 v, float angle )
    2.     {
    3.         float sin = Mathf.Sin( angle );
    4.         float cos = Mathf.Cos( angle );
    5.        
    6.         float ty = v.y;
    7.         float tz = v.z;
    8.         v.y = (cos * ty) - (sin * tz);
    9.         v.z = (cos * tz) + (sin * ty);
    10.     }
    11.    
    12.     public static void RotateY( this Vector3 v, float angle )
    13.     {
    14.         float sin = Mathf.Sin( angle );
    15.         float cos = Mathf.Cos( angle );
    16.        
    17.         float tx = v.x;
    18.         float tz = v.z;
    19.         v.x = (cos * tx) + (sin * tz);
    20.         v.z = (cos * tz) - (sin * tx);
    21.     }
    22.  
    23.     public static void RotateZ( this Vector3 v, float angle )
    24.     {
    25.         float sin = Mathf.Sin( angle );
    26.         float cos = Mathf.Cos( angle );
    27.        
    28.         float tx = v.x;
    29.         float ty = v.y;
    30.         v.x = (cos * tx) - (sin * ty);
    31.         v.y = (cos * ty) + (sin * tx);
    32.     }
    Another handy function that deals with angles in Vectors is to get the Pitch and Yaw-

    Code (csharp):
    1.     public static float GetPitch( this Vector3 v )
    2.     {
    3.         float len = Mathf.Sqrt( (v.x * v.x) + (v.z * v.z) );    // Length on xz plane.
    4.         return( -Mathf.Atan2( v.y, len ) );
    5.     }
    6.        
    7.     public static float GetYaw( this Vector3 v )
    8.     {
    9.         return( Mathf.Atan2( v.x, v.z ) );
    10.     }
     
  14. ArniBoy

    ArniBoy

    Joined:
    May 22, 2013
    Posts:
    9
    Thank you very much for supplying these. I stumbled upon this thread by googling, and you gave the perfect answer =)
     
  15. JGriffith

    JGriffith

    Joined:
    Sep 3, 2012
    Posts:
    22
    There is no debate btw. A vector is 3 numbers representing a direction and a length.

    A vector is not infinite. On a plane (1,1) and (2,2) are the same direction but not the same length.

    A vector can also represent a point in space given a specific origin. Working in world space a vector of (1,1,1) is the same as the position (1,1,1).

    A vector can be rotated, as stated, by multiplying a rotation by said vector.

    Sorry but I have seen so many damn posts with people arguing semantics over vectors, its like the guys that read somewhere that a vector and 3d pos aren't the same thing love to flame people about it but half the time don't really understand what they are saying.
     
  16. stevewetherill

    stevewetherill

    Joined:
    Mar 7, 2013
    Posts:
    1
    These are useful ... except I don't think you can extend structs in this way. Not 100% certain, but since structs are "pass by value," you end up modifying a copy of the struct (even with the the extension syntax) because the Vector3 type is a struct. The code as written does nothing, though it compiles just fine - it modifies a copy of the Vector3 v and then tosses it away. In order to make these work, I had to return the modified struct to calling code (which makes using the extension a bit moot since the value of a C# extension is at least partially in that you effectively extend an existing type). This is what I did, for example:

    Code (csharp):
    1.     public static Vector3 RotateX( this Vector3 v, float angle )
    2.     {
    3.         float sin = Mathf.Sin( angle );
    4.         float cos = Mathf.Cos( angle );
    5.        
    6.         float ty = v.y;
    7.         float tz = v.z;
    8.         v.y = (cos * ty) - (sin * tz);
    9.         v.z = (cos * tz) + (sin * ty);
    10.  
    11.         return v;
    12.     }
    13.    
    14.  
     
    Last edited: Dec 31, 2013
  17. Siccity

    Siccity

    Joined:
    Dec 7, 2013
    Posts:
    169
    No need for all that math if you're just going to rotate a 2D vector 90 degrees. Simply swap the x and y places and negate x. Here's a table for you.
    [table="width: 200"]
    [tr]
    [td](x,y)[/td]
    [td](1,2)[/td]
    [td]0 deg[/td]
    [/tr]
    [tr]
    [td](-y,x)[/td]
    [td](-2,1)[/td]
    [td]90 deg[/td]
    [/tr]
    [tr]
    [td](-x,-y)[/td]
    [td](-1,-2)[/td]
    [td]180 deg[/td]
    [/tr]
    [tr]
    [td](y,-x)[/td]
    [td](2,-1)[/td]
    [td]270 deg[/td]
    [/tr]
    [/table]
    I'm sure there's a similar way for 3D vectors but the downside is it'll only work with 90 degree rotations.
     
  18. llMarty

    llMarty

    Joined:
    Dec 14, 2014
    Posts:
    33
    Imagine I got a vector that points down like (0.0, -500.0, 0.0) and I want to rotate it by 90 degrees clock-wise, so the angle is -90 or 270. The desired new vector should point to left, so (-500.0, 0.0, 0.0), but the result by your function is (-447.0, 224.0, 0.0).

    Did I get anything wrong here?
     
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,834
    1) this thread is old

    2) just multiply by a quaternion to rotate a vector

    for example, here is a function to rotate a vector around some axis:

    Code (csharp):
    1.  
    2.         public static Vector3 RotateAroundAxis(Vector3 v,float a,Vector3 axis, bool bUseRadians = false)
    3.         {
    4.             if (bUseRadians) a *= MathUtil.RAD_TO_DEG;
    5.             var q = Quaternion.AngleAxis(a, axis);
    6.             return q * v;
    7.         }
    8.  
     
    40detectives, Gharry and hippocoder like this.
  20. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    499
    The simplest way to rotate a vector is to use the Quaternion's overloaded * operator.

    Mind that this isn't a multiplication op when it comes to Quaternions. Rather, I like to think of the * op for quaternions as the 'rotates' operation. It makes sense when you use it, like this:

    Vector3 myRotatedVector = myRotationQuat * myInitialVector;

    You can read that as myRotatedVector = myRotationQuat rotates myInitialVector.

    This operator also works with other quaternions. It can be tricky to visualize, but it can be extremely useful to combine multiple rotations this way.

    Keep in mind that rotating a vector this way assumes the vector represents an orientation (origin at 0,0,0), and not a position. If you want to rotate a point relative to something else (a pivot point), you first have to find the vector relative to the pivot, rotate that, then add the pivot's position back to the result:

    Vector3 RotatedAroundPivot = (myQuat * (myInitialPos - pivotPoint)) + pivotPoint;

    Hope this helps!

    Cheers
     
    40detectives, X05E and llMarty like this.
  21. llMarty

    llMarty

    Joined:
    Dec 14, 2014
    Posts:
    33
    Thank you so much, this is exactly what I was looking for. Finally a simple solution.
    (I really have to learn how to deal with Quaternions.)
     
  22. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,834
  23. himahuja8

    himahuja8

    Joined:
    Sep 20, 2019
    Posts:
    1
    Aren't the above specified function for the right co-ordinate system. And since the Unity environment is a left handed co-ordinate system, user might need to supply -theta instad of theta to use this.
     
unityunity