Skip to content
This repository was archived by the owner on Jun 28, 2026. It is now read-only.

Angle changes#121

Open
metalgearsloth wants to merge 2 commits into
Space-Wizards-Federation:masterfrom
metalgearsloth:2026-06-20-angle-changes
Open

Angle changes#121
metalgearsloth wants to merge 2 commits into
Space-Wizards-Federation:masterfrom
metalgearsloth:2026-06-20-angle-changes

Conversation

@metalgearsloth

@metalgearsloth metalgearsloth commented Jun 20, 2026

Copy link
Copy Markdown
Contributor
  • Use double constants not floats, based on my benchmarks it might be slightly slower (depending on when I run it it varies but generally I think it's slower).
  • Use multiplication not division for constants.
  • Remove redundant readonlys (at least what rider tells me).
  • Change to some of the dir methods that actually make them faster (hence the PR).
image

Benchmark code is pretty scrunkly and verbose but it's the old methods copy-pasted into it. Can add it if we want to keep it for history.

- Use double constants not floats, based on my benchmarks it might be slightly slower (depending on when I run it it varies but generally I think it's slower).
- Use multiplication not division for constants.
- Remove redundant readonlys (at least what rider tells me).
public readonly double Theta;

private const double PiOver2 = Math.PI / 2.0;
private const double TwoPi = 2.0 * Math.PI;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use Math.Tau

/// </summary>
public readonly double Theta;

private const double PiOver2 = Math.PI / 2.0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure the compiler does not already do this for you as a very obvious optimization?

}

public readonly Vector2 ToWorldVec()
public Vector2 ToWorldVec()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add documentation while you are at it

@@ -44,7 +46,7 @@ public Angle(Vector2 dir)

public static Angle FromWorldVec(Vector2 dir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation.
Make sure to mention why this is different for world vectors

var theta = Theta - PiOver2;
var x = Math.Cos(theta);
var y = Math.Sin(theta);
return new Vector2((float) x, (float) y);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new Vector2((float) x, (float) y);
return new Vector2((float)x, (float)y);

formatting convention
isn't your IDE reading the .editorconfig correctly? This should be a warning


private const double Segment = 2 * Math.PI / 8.0; // Cut the circle into 8 pieces
private const double Offset = Segment / 2.0; // offset the pieces by 1/2 their size
private const double InvSegment = 1.0 / Segment;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure the compiler doesn't just do this for you?


[Pure]
public readonly Direction GetCardinalDir()
public Direction GetCardinalDir()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs

ang += TwoPi;

return (Direction) (Math.Floor((ang + CardinalOffset) / CardinalSegment) * 2 % 8);
return (Direction) (((int) ((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (Direction) (((int) ((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7);
return (Direction) (((int)((ang + CardinalOffset) * InvCardinalSegment) * 2) & 7);


[Pure]
public readonly Angle Opposite()
public Angle Opposite()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs, same below

if (ang < 0) // convert -PI > PI to 0 > 2PI
ang += TwoPi;

return (Direction) ((int) ((ang + Offset) * InvSegment) & 7);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (Direction) ((int) ((ang + Offset) * InvSegment) & 7);
return (Direction)((int) ((ang + Offset) * InvSegment) & 7);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants