Angle Helpers

Angle and orientation math — sector mapping, yaw extraction, quaternion conversion, and hysteresis for directional classification.

The Orientation namespace (GS_Core::Orientation) provides angle-to-sector mapping for directional gameplay — animation direction selection, facing classification, and compass-style sector queries. It splits a full circle into N equal angular sectors and determines which sector a given angle falls into, with hysteresis to prevent rapid sector-switching at boundaries.

Used by: Paper-facing systems (gs_performer), directional input reactors (gs_unit), targeting systems (gs_interaction)

For usage guides and setup examples, see The Basics: GS_Core.

 

Contents


SectionConfig Enum

A reflected, editor-friendly enum selecting common sector counts and alignment modes. Two alignment modes exist per count:

  • cardinal / sideAligned — sector boundaries fall on the cardinal directions; sectors straddle diagonals
  • quarters / forwardAligned — sector boundaries fall on diagonals; sectors straddle cardinals
FamilyValues
x4x4_cardinal, x4_quarters
x6x6_sideAligned, x6_forwardAligned
x8x8_cardinal, x8_quarters
x10x10_sideAligned, x10_forwardAligned
x12x12_sideAligned, x12_forwardAligned
x14x14_sideAligned, x14_forwardAligned
x16x16_sideAligned, x16_forwardAligned
x18x18_sideAligned, x18_forwardAligned
x20x20_sideAligned, x20_forwardAligned
x22x22_sideAligned, x22_forwardAligned
x24x24_sideAligned, x24_forwardAligned

Register with GS_Core::Orientation::ReflectOrientationEnums(context) to expose these in the Inspector.


RotationDirection Enum

Controls the winding convention used when computing sector angles.

ValueNumericDescription
CCW1Counter-clockwise winding
CW-1Clockwise winding

Pick Struct

Returned by all PickByAngle overloads. Contains full sector geometry for the selected sector.

FieldTypeDescription
indexintWhich sector was selected [0, N)
countintTotal number of sectors
anglefloatThe input angle as provided
widthfloatAngular width of each sector (2π / N)
centerfloatCenter angle of the selected sector
startfloatStart angle of the selected sector
endfloatEnd angle of the selected sector

Use GS_Core::Orientation::Changed(pick, prevIndex) to detect when the sector index changes between frames.


API Reference

Sector Mapping

FunctionDescription
PickByAngle(angle, count, halfAligned, prevIndex, hysteresisDeg, startAngle, dir)Primary overload — full parameter control
PickByAngle(angle, SectionConfig, prevIndex, hysteresisDeg, startAngle, dir)Convenience overload using SectionConfig enum
PickByAngle(angle, count, offsetRad, prevIndex, hysteresisDeg, startAngle, dir)Low-level overload with explicit alignment offset
ConfigToParams(cfg)Maps a SectionConfig value to its (count, halfAligned) pair
Changed(pick, prevIndex)Returns true if the sector index changed from prevIndex

Angle Math

FunctionDescription
WrapToTwoPi(x)Wraps any angle to [0, 2π)
WrapToPi(x)Wraps any angle to (-π, π]
AlignmentOffsetRad(N, halfAligned)Returns the alignment shift in radians for a given count and mode

Yaw and Quaternion

FunctionDescription
YawFromDir(dir, rotDir)Flat yaw from a direction vector (Z-up world)
YawFromDir(dir, upAxis, forwardHint, rotDir)General yaw with custom up and forward axes
FlatSignedYaw_ToCam(camFwd, rootFwd, up, dir)Signed yaw from camera-forward to entity-forward, projected flat
QuatFromYaw(yawRad, upAxis)Builds a rotation quaternion from a yaw angle and up axis

Reflection

FunctionDescription
ReflectOrientationEnums(context)Registers SectionConfig and RotationDirection with SerializeContext

Usage Example

#include <GS_Core/Utility/Math/SectionByAngle.h>

// Member field — track previous sector to enable hysteresis
int m_prevSectorIndex = -1;

// In your tick / update function:
AZ::Vector3 moveDir = GetMovementDirection();

// Get the yaw from the movement direction (Z-up world)
float yaw = GS_Core::Orientation::YawFromDir(moveDir, GS_Core::Orientation::RotationDirection::CCW);

// Map to an 8-way sector with 5-degree hysteresis
GS_Core::Orientation::Pick pick = GS_Core::Orientation::PickByAngle(
    yaw,
    GS_Core::Orientation::SectionConfig::x8_cardinal,
    m_prevSectorIndex,  // previous index for hysteresis
    5.0f                // hysteresis in degrees
);

if (GS_Core::Orientation::Changed(pick, m_prevSectorIndex))
{
    m_prevSectorIndex = pick.index;
    // React to direction change: play animation, update facing, etc.
}

See Also

For related resources:


Get GS_Core

GS_Core — Explore this gem on the product page and add it to your project.