Comments and answers for "What's the most efficient way to rotate a Vector2 of a certain angle around the axis orthogonal to the plane they describe?"
http://answers.unity.com/questions/661383/whats-the-most-efficient-way-to-rotate-a-vector2-o.html
The latest comments and answers for the question "What's the most efficient way to rotate a Vector2 of a certain angle around the axis orthogonal to the plane they describe?"Comment by DungDajHjep on DungDajHjep's comment
http://answers.unity.com/comments/1687987/view.html
thanks @akauperTue, 31 Dec 2019 09:58:53 GMTDungDajHjepComment by Ultroman on Ultroman's comment
http://answers.unity.com/comments/1670294/view.html
Unfortunately your code is being destroyed by not having spaces between the * and the variables, so the viewing processor transforms some of your message into italic text instead of including the *. If you care about this, I'd suggest putting the functions on Pastebin and leave a link instead, or remember to use the code tags in the message.Mon, 07 Oct 2019 11:44:42 GMTUltromanComment by XenoRo on XenoRo's answer
http://answers.unity.com/comments/1436423/view.html
For anyone interested, here is my implementation, with some extra features like:
- Supports rotating by radians or degrees (separate methods).
- Supports pivots.
- Is properly documented.
----------
/// <summary>Rotates the vector by radians.</summary>
/// <param name="vector">The vector.</param>
/// <param name="radians">The radians.</param>
/// <param name="pivot">The pivot.</param>
/// <returns>Modified vector.</returns>
public static Vector2 RotateRad(this Vector2 vector, float radians,
Vector2 pivot = default(Vector2))
{
var sin = Mathf.Sin(radians);
var cos = Mathf.Cos(radians);
vector -= pivot;
vector.Set(cos * vector.x - sin * vector.y, sin * vector.x + cos * vector.y);
vector += pivot;
return vector;
}
/// <summary>Rotates the vector by degrees.</summary>
/// <param name="vector">The vector.</param>
/// <param name="degrees">The degrees.</param>
/// <param name="pivot">The pivot.</param>
/// <returns>Modified vector.</returns>
public static Vector2 Rotate(this Vector2 vector, float degrees,
Vector2 pivot = default(Vector2))
{
return RotateRad(vector, degrees * Mathf.Deg2Rad, pivot);
}Tue, 28 Nov 2017 20:33:05 GMTXenoRoComment by XenoRo on XenoRo's answer
http://answers.unity.com/comments/1436418/view.html
@akauper Quaternion *can indeed* do the math for you, but the question is not *"how can I do this?"*, and context matters. On a low-weight code like this, overhead can make up a large portion of the method's execution time, And doing the math "manually" avoids large chunks of overhead and, at least according to my tests, can make the code be roughly 2 times faster. --- It's already super fast anyway, and optimizing it is overkill for anything but the most hardcore simulation or bullet-hell games, but it's faster none the less, and as an extension method that's probably never getting changed after you implement it once, it's certainly worth doing IMHO.Tue, 28 Nov 2017 20:15:52 GMTXenoRoComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1355342/view.html
That's irrelevant. There is no right way how something should rotate. If anything then the normal mathematical way should apply which is a counter-clockwise rotation and as far as i can tell that's exactly what's happening. If you need the rotation the other way round you have to either:
- use a negative angle
- invert the rotation axis (like you did)
But again the "normal" rotation direction is counter-clockwise. $$anonymous$$eep in mind that Unity is a left-handed system
Though if you only work with 2d vectors the solution that DDP posted is the faster one with less overhead. [Creating a quaternion with AngleAxis][1] also requires one "Sin" and one "Cos" call. Though the actual quaternion multiplication is a bit longer since it only works with 3d vectors:
// for reference this is the multiply operator of the Quaternion struct.
public static Vector3 operator *(Quaternion rotation, Vector3 point)
{
float num = rotation.x * 2f;
float num2 = rotation.y * 2f;
float num3 = rotation.z * 2f;
float num4 = rotation.x * num;
float num5 = rotation.y * num2;
float num6 = rotation.z * num3;
float num7 = rotation.x * num2;
float num8 = rotation.x * num3;
float num9 = rotation.y * num3;
float num10 = rotation.w * num;
float num11 = rotation.w * num2;
float num12 = rotation.w * num3;
Vector3 result;
result.x = (1f - (num5 + num6)) * point.x + (num7 - num12) * point.y + (num8 + num11) * point.z;
result.y = (num7 + num12) * point.x + (1f - (num4 + num6)) * point.y + (num9 - num10) * point.z;
result.z = (num8 - num11) * point.x + (num9 + num10) * point.y + (1f - (num4 + num5)) * point.z;
return result;
}
[1]: https://www.youtube.com/watch?v=3BR8t$$anonymous$$-LuB0Fri, 19 May 2017 09:06:17 GMTBunny83Comment by Truthveyor on Truthveyor's answer
http://answers.unity.com/comments/1355332/view.html
I was just looking for an answer like this. Thank you.
Except based on my test, in this example, the answer should be
<code>
return Quaternion.AngleAxis(calibrationAmount, -Vector3.forward) * normalizedDirection;
</code>
because calibrationAmount is in degrees, and using Vector3.forward will cause it to rotate in the opposite direction, so you want to use -Vector3.forward instead.Fri, 19 May 2017 08:28:32 GMTTruthveyorComment by akauper on akauper's answer
http://answers.unity.com/comments/860148/view.html
Quaternions can do all this math for you:
public static class Vector2Extension
{
public static Vector2 Rotate(this Vector2 v, float degrees)
{
return Quaternion.Euler(0, 0, degrees) * v;
}
}
This will work the same as above.Mon, 22 Dec 2014 09:15:12 GMTakauperComment by Tabemasu Games on Tabemasu Games's answer
http://answers.unity.com/comments/835688/view.html
@Trexug I didn't know it was a struct, thanks for the intel. :)Wed, 19 Nov 2014 13:02:41 GMTTabemasu GamesComment by Trexug on Trexug's answer
http://answers.unity.com/comments/835644/view.html
@Tabemasu Games
Vector2 is a struct (pass by value). Modifying the parameter is fine, as it does not affect the value provided when calling the method (since there is no ref or out keyword).Wed, 19 Nov 2014 12:03:36 GMTTrexugComment by Tabemasu Games on Tabemasu Games's answer
http://answers.unity.com/comments/834881/view.html
Your method seems good, but it does change the source vector (v) AND return it. To be consistent with other methods (like ClampMagnitude), I'd return a new Vector 2 :
public static class Vector2Extension {
public static Vector2 Rotate(this Vector2 v, float degrees) {
float radians = degrees * Mathf.Deg2Rad;
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);
float tx = v.x;
float ty = v.y;
return new Vector2(cos * tx - sin * ty, sin * tx + cos * ty);
}
}Tue, 18 Nov 2014 16:14:40 GMTTabemasu GamesAnswer by DDP
http://answers.unity.com/answers/734946/view.html
What about using an extension method?
using UnityEngine;
public static class Vector2Extension {
public static Vector2 Rotate(this Vector2 v, float degrees) {
float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
float tx = v.x;
float ty = v.y;
v.x = (cos * tx) - (sin * ty);
v.y = (sin * tx) + (cos * ty);
return v;
}
}
Can be use that way:
Vector2 direction = Vector2.right.Rotate(45f);Wed, 25 Jun 2014 16:12:57 GMTDDPComment by Jamora
http://answers.unity.com/comments/661423/view.html
How come this question is marked as wiki twice?Tue, 11 Mar 2014 18:44:37 GMTJamoraAnswer by whydoidoit
http://answers.unity.com/answers/661418/view.html
Well if it works don't knock it. You could also do this:
return Quaternion.AngleAxis(_angle, Vector3.forward) * normalizedDirection;Tue, 11 Mar 2014 18:41:30 GMTwhydoidoit