Skip to content

Geometry

Common geometry types: 2D/3D vectors and rectangles.

Header: #include <asw/modules/geometry.h>Namespace: asw

Vec2

A templated 2D vector used for positions, directions, and sizes.

cpp
template <typename T>
class Vec2;

Constructors

cpp
Vec2();            // (0, 0)
Vec2(T x, T y);

Fields

FieldTypeDescription
xTX component
yTY component

Methods

MethodReturnDescription
angle()TAngle of the vector in radians
angle(const Vec2& other)TAngle between two vectors in radians
distance(const Vec2& other)TDistance between two vectors
dot(const Vec2& other)TDot product
cross(const Vec2& other)TCross product (scalar)
magnitude()TLength of the vector

Operators

+, -, *, /, +=, -=, *=, /=, ==, !=

Arithmetic operators work with other Vec2 instances or scalar values.

Vec3

A templated 3D vector.

cpp
template <typename T>
class Vec3;

Constructors

cpp
Vec3();                // (0, 0, 0)
Vec3(T x, T y, T z);

Fields

FieldTypeDescription
xTX component
yTY component
zTZ component

Methods

MethodReturnDescription
angle(const Vec3& other)TAngle between two vectors in radians
distance(const Vec3& other)TDistance between two vectors
dot(const Vec3& other)TDot product
cross(const Vec3& other)Vec3Cross product (vector)
magnitude()TLength of the vector

Operators

+, -, *, /, +=, -=, *=, /=, ==, !=

Quad

A templated 2D rectangle defined by position and size. Used for bounding boxes, sprite regions, and collision detection.

cpp
template <typename T>
class Quad;

Constructors

cpp
Quad();
Quad(const Vec2<T>& position, const Vec2<T>& size);
Quad(T x, T y, T width, T height);

Fields

FieldTypeDescription
positionVec2<T>Top-left position
sizeVec2<T>Width and height

Methods

MethodReturnDescription
setPosition(T x, T y)voidSet the position
setSize(T width, T height)voidSet the size
getCenter()Vec2<T>Get the center point
contains(const Vec2<T>& point)boolPoint-in-rect test
contains(T x, T y)boolPoint-in-rect test
collides(const Quad& other)boolAABB collision test
collidesTop(const Quad& other)boolTop-edge collision
collidesBottom(const Quad& other)boolBottom-edge collision
collidesLeft(const Quad& other)boolLeft-edge collision
collidesRight(const Quad& other)boolRight-edge collision

Operators

+, -, *, / (with other Quad or scalar values)

Example

cpp
asw::Vec2<float> pos(100.0f, 200.0f);
asw::Vec2<float> vel(1.0f, 0.0f);
pos += vel * deltaTime;

asw::Quad<float> player(100, 200, 32, 32);
asw::Quad<float> enemy(150, 210, 32, 32);

if (player.collides(enemy)) {
  // handle collision
}

if (player.contains(asw::input::mouse.position)) {
  // mouse is over the player
}

Released under the MIT License.