Thank you so much! That’s what I get for googling “C# angle to radians”, grabbing something off of GitHub and not really looking at it. I so miss coding with a partner and having another set of eyes. Thank you again!
Nez has some helper math to deal with this stuff… What always gets me is Y is going down. the clockwise and winding conventions to… since we learn trig with Y up + its gets confusng.
/// <summary>
/// returns the angle whose tangent is the quotient (y/x) of two specified numbers
/// </summary>
/// <param name="y">The y coordinate.</param>
/// <param name="x">The x coordinate.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Atan2(float y, float x)
{
return (float)Math.Atan2(y, x);
}
#endregion
#region Vector2
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float AngleBetweenVectors(Vector2 from, Vector2 to)
{
return Atan2(to.Y-from.Y, to.X-from.X);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 AngleToVector(float angleRadians, float length)
{
return new Vector2(Cos(angleRadians)*length, Sin(angleRadians)*length);
}
#if GRAPHICS_MG
///
/// helper for moving a value around in a circle.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 RotateAround(Vector2 position, float speed)
{
var time = Time.TotalTime * speed;
var x = Cos(time);
var y = Sin(time);
return new Vector2(position.X + x, position.Y + y);
}
#endif
/// <summary>
/// the rotation is relative to the current position not the total rotation. For example, if you are currently at 90 degrees and
/// want to rotate to 135 degrees, you would use an angle of 45, not 135.
/// </summary>
/// <returns>The around.</returns>
/// <param name="point">Point.</param>
/// <param name="center">Center.</param>
/// <param name="angleInDegrees">Angle in degrees.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 RotateAround(Vector2 point, Vector2 center, float angleInDegrees)
{
angleInDegrees=MathHelper.ToRadians(angleInDegrees);
float cos = Cos(angleInDegrees);
float sin = Sin(angleInDegrees);
var rotatedX = cos*(point.X-center.X)-sin*(point.Y-center.Y)+center.X;
var rotatedY = sin*(point.X-center.X)+cos*(point.Y-center.Y)+center.Y;
return new Vector2(rotatedX, rotatedY);
}
/// <summary>
/// the rotation is relative to the current position not the total rotation. For example, if you are currently at 1 Pi radians and
/// want to rotate to 1.5 Pi radians, you would use an angle of 0.5 Pi, not 1.5 Pi.
/// </summary>
/// <returns>The around.</returns>
/// <param name="point">Point.</param>
/// <param name="center">Center.</param>
/// <param name="angleInDegrees">Angle in radians.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 RotateAroundRadians(Vector2 point, Vector2 center, float angleInRadians)
{
float cos = Cos(angleInRadians);
float sin = Sin(angleInRadians);
var rotatedX = cos*(point.X-center.X)-sin*(point.Y-center.Y)+center.X;
var rotatedY = sin*(point.X-center.X)+cos*(point.Y-center.Y)+center.Y;
return new Vector2(rotatedX, rotatedY);
}
thinly veiled? Stack Exchange is the most gamified , egoist, living in moms basement, post grad but unemployable, anti Einstein, incel -infested rote learning site for ****…never mmind some of the very basic programming questions have half decent answers … one gjy was trying to link to a c lib to c# to normalize some vectors. i suggested porting the c lib to c# and using numerics lib and SIMD and compex numbers not using the c lib and arrays… Downvoted. -10 for not answering the dll import question exactly. on Natural philosophy and physics, downvoted for totally relevant ontological points in 4D cosmology , quoting Eisntein adn Hawking in a sort of discussion (there are no right answers or socratic dialogs unless u got tonsof points) dont ever contribute to them. they might have an answer or two but old and not open to new approches, presence of females, wild or new ideas that are relevant but the downvoters lack the imagination to make the connection. Einstein or Hawking would have like -666 score in physcs on there, if he was still “moving”. tI think we all miss coding with other some eyes, preferably in the room, covid screwed that up…sorry for the rant its summer, venting frustration at being at computer, behind schedule.