Skip to main content

Units

The units.h header provides fundamental spatial and mathematical types used throughout Atlas Engine. These types offer clean, type-safe wrappers around GLM types with convenient APIs for game development.

Position3d

Represents a 3D position with single-precision floating point coordinates.
struct Position3d {
    float x;
    float y;
    float z;
};

Static Factory Methods

Position3d::zero()      // (0, 0, 0)
Position3d::up()        // (0, 1, 0)
Position3d::down()      // (0, -1, 0)
Position3d::forward()   // (0, 0, 1)
Position3d::back()      // (0, 0, -1)
Position3d::right()     // (1, 0, 0)
Position3d::left()      // (-1, 0, 0)
Position3d::invalid()   // (NaN, NaN, NaN)

Operations

Position3d pos(10.0f, 5.0f, -3.0f);
pos += Position3d(1.0f, 0.0f, 0.0f);  // Addition
Position3d diff = pos - Position3d::up();  // Subtraction
Position3d scaled = pos * 2.0f;  // Scalar multiplication
Position3d normalized = pos.normalized();  // Normalize to unit vector

// Convert to/from GLM
glm::vec3 glmPos = pos.toGlm();
Position3d fromGlm = Position3d::fromGlm(glmPos);

Usage Example

// Set object position
Camera camera;
camera.setPosition({-5.0f, 1.0f, 0.0f});
camera.lookAt({0.0f, 1.0f, 0.0f});

// Create objects with positions
CoreObject ground = createBox({2.0f, 0.1f, 2.0f});
ground.setPosition({0.0f, -0.1f, 0.0f});

Type Aliases

The following aliases all use Position3d internally but provide semantic clarity:
TypePurposeExample
Scale3dScaling factors{2.0f, 1.0f, 1.0f}
Size3dPhysical dimensions{0.5f, 0.5f, 0.5f}
Point3dPoint in space{10.0f, 5.0f, 3.0f}
Normal3dSurface normals{0.0f, 1.0f, 0.0f}
Magnitude3dVector magnitudes{1.0f, 1.0f, 1.0f}
Velocity3dMovement velocity{5.0f, 0.0f, 2.0f}
Impulse3dPhysics impulse{0.0f, 0.0f, 20.0f}
Force3dForce vectors{0.0f, -9.8f, 0.0f}
Vector3Generic 3D vector{1.0f, 2.0f, 3.0f}

Rotation3d

Represents rotation using Euler angles (pitch, yaw, roll) in degrees.
struct Rotation3d {
    float pitch;  // Rotation around X-axis
    float yaw;    // Rotation around Y-axis
    float roll;   // Rotation around Z-axis
};

Operations

Rotation3d rot(0.0f, 45.0f, 0.0f);  // 45 degrees yaw
rot.yaw += deltaTime * 90.0f;  // Rotate 90 deg/sec

// Convert to quaternion for rendering
glm::quat quatRot = rot.toGlmQuat();

// Convert from quaternion
Rotation3d fromQuat = Rotation3d::fromGlmQuat(someQuat);

Usage Example

// Rotate an instance
Instance &potUp = pot.createInstance();
potUp.rotate({0.0f, -90.0f, 0.0f});
potUp.move({0.5f, 0.0f, 0.5f});

Quaternion

Represents rotation using a quaternion with conversion to/from Euler angles.
struct Quaternion {
    float x, y, z, w;
    
    glm::quat toGlm() const;
    static Quaternion fromGlm(const glm::quat &quat);
    static Rotation3d toEuler(const Quaternion &quat);
    static Quaternion fromEuler(const Rotation3d &euler);
};

Color

Represents RGBA colors with single-precision components (0.0 to 1.0).
struct Color {
    float r = 1.0;
    float g = 1.0;
    float b = 1.0;
    float a = 1.0;
};

Static Color Constants

Color::white()       // (1, 1, 1, 1)
Color::black()       // (0, 0, 0, 1)
Color::red()         // (1, 0, 0, 1)
Color::green()       // (0, 1, 0, 1)
Color::blue()        // (0, 0, 1, 1)
Color::transparent() // (0, 0, 0, 0)
Color::yellow()      // (1, 1, 0, 1)
Color::cyan()        // (0, 1, 1, 1)
Color::magenta()     // (1, 0, 1, 1)
Color::gray()        // (0.5, 0.5, 0.5, 1)
Color::orange()      // (1, 0.65, 0, 1)
Color::purple()      // (0.5, 0, 0.5, 1)
Color::brown()       // (0.6, 0.4, 0.2, 1)
Color::pink()        // (1, 0.75, 0.8, 1)
Color::lime()        // (0, 1, 0, 1)
Color::navy()        // (0, 0, 0.5, 1)
Color::teal()        // (0, 0.5, 0.5, 1)
Color::olive()       // (0.5, 0.5, 0, 1)
Color::maroon()      // (0.5, 0, 0, 1)

Operations

// Create custom colors
Color customColor(0.2f, 0.8f, 0.5f, 1.0f);

// Parse from hex (0xRRGGBB)
Color fromHex = Color::fromHex(0xFF5733);

// Mix colors
Color blended = Color::mix(Color::red(), Color::blue(), 0.5f);

// Arithmetic
Color brightRed = Color::red() * 1.5f;
Color combined = Color::red() + Color::green();

Usage Example

// Set material colors
CoreObject pot = createBox({1.0f, 0.25f, 0.25f}, Color(0.6f, 0.4f, 0.2f));

// Use color constants
ground.material.albedo = Color::white();

// Create colored fluids
Fluid water;
water.create({0.9, 0.9}, Color::blue());

Position2d

Represents a 2D position with single-precision floating point coordinates.
struct Position2d {
    float x;
    float y;
};

Type Aliases

  • Scale2d - 2D scaling factors
  • Point2d - 2D point in space
  • Movement2d - 2D movement vector
  • Magnitude2d - 2D magnitude vector

Usage Example

// UI positioning
Text fpsText("FPS: 0", font, {25.0f, 25.0f}, Color::white());

// Mouse movement
void onMouseMove(Window &window, Movement2d movement) override {
    camera.updateLook(window, movement);
}

Size2d

Represents 2D dimensions with width and height.
struct Size2d {
    float width;
    float height;
};

Operations

Size2d windowSize{1920.0f, 1080.0f};
Size2d halfSize = windowSize / 2.0f;
float area = windowSize.width * windowSize.height;
glm::vec2 glmSize = windowSize.toGlm();

Radians

Represents angular measurements in radians with conversion from degrees.
struct Radians {
    float value;
    
    static Radians fromDegrees(float degrees);
    float toFloat() const;
};

Usage Example

// Convert from degrees to radians
Radians angle = Radians::fromDegrees(45.0f);

// Perform angle arithmetic
Radians doubled = angle * 2.0f;

// Convert back to float for trigonometry
float sinValue = std::sin(angle.toFloat());

Direction3d

Enumeration of cardinal 3D directions.
enum class Direction3d {
    Up,        // Positive Y axis
    Down,      // Negative Y axis
    Left,      // Negative X axis
    Right,     // Positive X axis
    Forward,   // Positive Z axis
    Backward   // Negative Z axis
};

Id

Type alias for OpenGL object identifiers.
using Id = unsigned int;

Best Practices

  1. Use type aliases for semantic clarity: Use Velocity3d instead of Position3d when representing velocity
  2. Prefer static factory methods: Use Position3d::zero() instead of Position3d(0, 0, 0)
  3. Use Color constants: Prefer Color::red() over Color(1.0f, 0.0f, 0.0f, 1.0f)
  4. Convert to GLM when needed: Use .toGlm() when interfacing with rendering code
  5. Use hex colors for web colors: Color::fromHex(0xFF5733) is cleaner than manual RGB values

Build docs developers (and LLMs) love