Hello Does Unity supply a command or a combinations of commands to rotate a vector by a given angle ?
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);
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
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
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 ?
for a function like that you would need to also specify which axis to rotate around. here's an example: Code (csharp): var quat : Quaternion = Quaternion.AngleAxis(90,Vector3.forward); var vect : Vector3 = Vector3(0,1,0); 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.
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.
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): var bl1:Transform; function Update () { if(bl1.eulerAngles(Vector3(90,180,0))) { Debug.Log("is OK"); } }
I think I have foun the answer! Code (csharp): if(bl1.rotation == Quaternion.Euler(90.0, 180.0, 0)) { Debug.Log("is OK"); }
I seem to remember vectors being rotated when I studied physics at university, I am sure it was part of 'maths for physics': http://en.wikipedia.org/wiki/Rotation_(mathematics)
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...
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): public static void RotateX( this Vector3 v, float angle ) { float sin = Mathf.Sin( angle ); float cos = Mathf.Cos( angle ); float ty = v.y; float tz = v.z; v.y = (cos * ty) - (sin * tz); v.z = (cos * tz) + (sin * ty); } public static void RotateY( this Vector3 v, float angle ) { float sin = Mathf.Sin( angle ); float cos = Mathf.Cos( angle ); float tx = v.x; float tz = v.z; v.x = (cos * tx) + (sin * tz); v.z = (cos * tz) - (sin * tx); } public static void RotateZ( this Vector3 v, float angle ) { float sin = Mathf.Sin( angle ); float cos = Mathf.Cos( angle ); float tx = v.x; float ty = v.y; v.x = (cos * tx) - (sin * ty); v.y = (cos * ty) + (sin * tx); } Another handy function that deals with angles in Vectors is to get the Pitch and Yaw- Code (csharp): public static float GetPitch( this Vector3 v ) { float len = Mathf.Sqrt( (v.x * v.x) + (v.z * v.z) ); // Length on xz plane. return( -Mathf.Atan2( v.y, len ) ); } public static float GetYaw( this Vector3 v ) { return( Mathf.Atan2( v.x, v.z ) ); }
Thank you very much for supplying these. I stumbled upon this thread by googling, and you gave the perfect answer =)
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.
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): public static Vector3 RotateX( this Vector3 v, float angle ) { float sin = Mathf.Sin( angle ); float cos = Mathf.Cos( angle ); float ty = v.y; float tz = v.z; v.y = (cos * ty) - (sin * tz); v.z = (cos * tz) + (sin * ty); return v; }
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.
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?
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): public static Vector3 RotateAroundAxis(Vector3 v,float a,Vector3 axis, bool bUseRadians = false) { if (bUseRadians) a *= MathUtil.RAD_TO_DEG; var q = Quaternion.AngleAxis(a, axis); return q * v; }
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
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.)
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.