Skip to content

Commit

Permalink
RoundToCardinalAngle
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr committed Jan 14, 2025
1 parent 6a2b369 commit d268a1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ public bool SnapCardinals
[DataField("noRot")]
public bool NoRotation;

// TODO SPRITE
// When refactoring, make this nullable and remove EnableDirectionOverride
[DataField("overrideDir")]
public Direction DirectionOverride = Direction.East;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void RenderSprite(

// If we have a 1-directional sprite then snap it to try and always face it south if applicable.
if (sprite.Comp is {NoRotation: false, SnapCardinals: true})
cardinal = angle.GetCardinalDir().ToAngle();
cardinal = angle.RoundToCardinalAngle();

// worldRotation + eyeRotation should be the angle of the entity on-screen. If no-rot is enabled this is just set to zero.
// However, at some point later the eye-matrix is applied separately, so we subtract -eye rotation for now:
Expand All @@ -70,14 +70,12 @@ public void RenderSprite(
return;
}

// TODO sprite optimize angle.GetCardinalDir().ToAngle()

//Default rendering (NoRotation = false)
entityMatrix = Matrix3Helpers.CreateTransform(worldPosition, worldRotation);
var transformDefault = Matrix3x2.Multiply(sprite.Comp.LocalMatrix, entityMatrix);

//Snap to cardinals
entityMatrix = Matrix3Helpers.CreateTransform(worldPosition, worldRotation - angle.GetCardinalDir().ToAngle());
entityMatrix = Matrix3Helpers.CreateTransform(worldPosition, worldRotation - angle.RoundToCardinalAngle());
var transformSnap = Matrix3x2.Multiply(sprite.Comp.LocalMatrix, entityMatrix);

//No rotation
Expand Down
10 changes: 10 additions & 0 deletions Robust.Shared.Maths/Angle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public readonly Direction GetCardinalDir()
return (Direction) (Math.Floor((ang + CardinalOffset) / CardinalSegment) * 2 % 8);
}

/// <summary>
/// Rounds the angle to the nearest cardinal direction. This behaves similarly to a combination of
/// <see cref="GetCardinalDir"/> and Direction.ToAngle(), however this may return an angle outside of the range
/// returned by those methods (-pi to pi).
/// </summary>
public Angle RoundToCardinalAngle()
{
return new Angle(CardinalSegment * Math.Floor((Theta + CardinalOffset) / CardinalSegment));
}

/// <summary>
/// Rotates the vector counter-clockwise around its origin by the value of Theta.
/// </summary>
Expand Down

0 comments on commit d268a1c

Please sign in to comment.