Skip to content

Commit b730bb9

Browse files
authored
Vector rotation helper functions in Vector2 (MonoGame#8117)
* Added vector rotation helper functions * Moved Vector rotation function to Vector2 & added non static versions
1 parent eaaaa58 commit b730bb9

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

MonoGame.Framework/MathHelper.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static float Barycentric(float value1, float value2, float value3, float
7878
public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
7979
{
8080
// Using formula from http://www.mvps.org/directx/articles/catmull/
81-
// Internally using doubles not to lose precission
81+
// Internally using doubles not to lose precision
8282
double amountSquared = amount * amount;
8383
double amountCubed = amountSquared * amount;
8484
return (float)(0.5 * (2.0 * value2 +
@@ -142,7 +142,7 @@ public static float Distance(float value1, float value2)
142142
/// <returns>The result of the Hermite spline interpolation.</returns>
143143
public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
144144
{
145-
// All transformed to double not to lose precission
145+
// All transformed to double not to lose precision
146146
// Otherwise, for high numbers of param:amount the result is NaN instead of Infinity
147147
double v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result;
148148
double sCubed = s * s * s;
@@ -265,29 +265,29 @@ public static float SmoothStep(float value1, float value2, float amount)
265265

266266
return result;
267267
}
268-
268+
269269
/// <summary>
270270
/// Converts radians to degrees.
271271
/// </summary>
272272
/// <param name="radians">The angle in radians.</param>
273273
/// <returns>The angle in degrees.</returns>
274274
/// <remarks>
275-
/// This method uses double precission internally,
275+
/// This method uses double precision internally,
276276
/// though it returns single float
277277
/// Factor = 180 / pi
278278
/// </remarks>
279279
public static float ToDegrees(float radians)
280280
{
281281
return (float)(radians * 57.295779513082320876798154814105);
282282
}
283-
283+
284284
/// <summary>
285285
/// Converts degrees to radians.
286286
/// </summary>
287287
/// <param name="degrees">The angle in degrees.</param>
288288
/// <returns>The angle in radians.</returns>
289289
/// <remarks>
290-
/// This method uses double precission internally,
290+
/// This method uses double precision internally,
291291
/// though it returns single float
292292
/// Factor = pi / 180
293293
/// </remarks>
@@ -313,14 +313,14 @@ public static float WrapAngle(float angle)
313313
return angle;
314314
}
315315

316-
/// <summary>
316+
/// <summary>
317317
/// Determines if value is powered by two.
318318
/// </summary>
319319
/// <param name="value">A value.</param>
320320
/// <returns><c>true</c> if <c>value</c> is powered by two; otherwise <c>false</c>.</returns>
321-
public static bool IsPowerOfTwo(int value)
322-
{
323-
return (value > 0) && ((value & (value - 1)) == 0);
324-
}
321+
public static bool IsPowerOfTwo(int value)
322+
{
323+
return (value > 0) && ((value & (value - 1)) == 0);
324+
}
325325
}
326326
}

MonoGame.Framework/Vector2.cs

+74-1
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ public static void TransformNormal(ref Vector2 normal, ref Matrix matrix, out Ve
11871187
result.X = x;
11881188
result.Y = y;
11891189
}
1190-
1190+
11911191
/// <summary>
11921192
/// Apply transformation on normals within array of <see cref="Vector2"/> by the specified <see cref="Matrix"/> and places the results in an another array.
11931193
/// </summary>
@@ -1254,6 +1254,79 @@ Vector2[] destinationArray
12541254
}
12551255
}
12561256

1257+
/// <summary>
1258+
/// Rotates a vector by the specified number of radians
1259+
/// </summary>
1260+
/// <param name="value">The vector to be rotated.</param>
1261+
/// <param name="radians">The amount to rotate the vector.</param>
1262+
/// <returns>A rotated copy of value.</returns>
1263+
/// <remarks>
1264+
/// A positive angle and negative angle
1265+
/// would rotate counterclockwise and clockwise,
1266+
/// respectively
1267+
/// </remarks>
1268+
public static Vector2 Rotate(Vector2 value, float radians)
1269+
{
1270+
float cos = MathF.Cos(radians);
1271+
float sin = MathF.Sin(radians);
1272+
1273+
return new Vector2(value.X * cos - value.Y * sin, value.X * sin + value.Y * cos);
1274+
}
1275+
1276+
/// <summary>
1277+
/// Rotates a <see cref="Vector2"/> by the specified number of radians
1278+
/// </summary>
1279+
/// <param name="radians">The amount to rotate this <see cref="Vector2"/>.</param>
1280+
/// <remarks>
1281+
/// A positive angle and negative angle
1282+
/// would rotate counterclockwise and clockwise,
1283+
/// respectively
1284+
/// </remarks>
1285+
public void Rotate(float radians)
1286+
{
1287+
float cos = MathF.Cos(radians);
1288+
float sin = MathF.Sin(radians);
1289+
1290+
float oldx = X;
1291+
1292+
X = X * cos - Y * sin;
1293+
Y = oldx * sin + Y * cos;
1294+
}
1295+
1296+
/// <summary>
1297+
/// Rotates a <see cref="Vector2"/> around another <see cref="Vector2"/> representing a location
1298+
/// </summary>
1299+
/// <param name="value">The <see cref="Vector2"/> to be rotated</param>
1300+
/// <param name="origin">The origin location to be rotated around</param>
1301+
/// <param name="radians">The amount to rotate by in radians</param>
1302+
/// <returns>The rotated <see cref="Vector2"/></returns>
1303+
/// <remarks>
1304+
/// A positive angle and negative angle
1305+
/// would rotate counterclockwise and clockwise,
1306+
/// respectively
1307+
/// </remarks>
1308+
public static Vector2 RotateAround(Vector2 value, Vector2 origin, float radians)
1309+
{
1310+
return Rotate(value - origin, radians) + origin;
1311+
}
1312+
1313+
/// <summary>
1314+
/// Rotates a <see cref="Vector2"/> around another <see cref="Vector2"/> representing a location
1315+
/// </summary>
1316+
/// <param name="origin">The origin location to be rotated around</param>
1317+
/// <param name="radians">The amount to rotate by in radians</param>
1318+
/// <remarks>
1319+
/// A positive angle and negative angle
1320+
/// would rotate counterclockwise and clockwise,
1321+
/// respectively
1322+
/// </remarks>
1323+
public void RotateAround(Vector2 origin, float radians)
1324+
{
1325+
this -= origin;
1326+
Rotate(radians);
1327+
this += origin;
1328+
}
1329+
12571330
/// <summary>
12581331
/// Deconstruction method for <see cref="Vector2"/>.
12591332
/// </summary>

0 commit comments

Comments
 (0)