Construct a two-dimensional vector.
Math
Small 2D vector and rectangle helpers.
Coordinates use the same screen-space convention as Draw: x grows to the
right, y grows downward, and a rectangle is a top-left corner plus a width
and a height. These are the types every drawing call takes, so most apps
need nothing more than this module for their geometry.
next = model.position.add(model.velocity.scale(input.time.elapsed_seconds))
Physics is the other one: it is 3D projective geometric algebra, for
rotations and intersections that 2D vectors cannot express.
zero : Vec2
The zero vector.
Construct an axis-aligned rectangle.
Construct a circle.
Clamp a value to inclusive lower and upper bounds.
Clamp a value to the inclusive 0-to-1 range.
Linearly interpolate between two scalars; amounts outside 0 to 1 extrapolate.
Square root for game-scale F32 values. Roc's pinned builtins do not expose sqrt yet, so keep this pure Roc.
add : Vec2, Vec2 -> Vec2
Add two vectors component-wise.
sub : Vec2, Vec2 -> Vec2
Subtract two vectors component-wise.
Multiply both vector components by a scalar.
Compute the vector dot product.
length_squared : Vec2 -> F32
Squared vector length, avoiding a square root.
Euclidean vector length.
distance_squared : Vec2, Vec2 -> F32
Squared distance between two points.
Euclidean distance between two points.
normalize : Vec2 -> Vec2
Return a unit vector, or zero when the input has zero length.
Linearly interpolate between two vectors.
Left edge of a rectangle.
Right edge of a rectangle.
Top edge of a rectangle.
Bottom edge of a rectangle.
center : Rect -> Vec2
Center point of a rectangle.
closest_point : Rect, Vec2 -> Vec2
Closest point in or on a rectangle.
Whether a rectangle contains a point, including its edges.
circle_contains : Circle, Vec2 -> Bool
Whether a circle contains a point, including its edge.
Whether two rectangles overlap or touch.
circle_overlaps : Circle, Circle -> Bool
Whether two circles overlap or touch.
circle_rect : Circle, Rect -> Bool
Whether a circle and rectangle overlap or touch.
contains_point : Circle, Vec2 -> Bool
Receiver-friendly aliases for circle queries. The longer names avoid ambiguity with the corresponding rectangle methods.
overlaps_circle : Circle, Circle -> Bool
overlaps_rect : Circle, Rect -> Bool
Vec2
:= {
x : F32,
y : F32,
}
Two-dimensional floating-point vector. Math.Vec2 and Draw.Vector2 on
the platform are this same type, re-exported.
is_eq : _
Compare two of these values.
Rect
:= {
x : F32,
y : F32,
width : F32,
height : F32,
}
Axis-aligned rectangle represented by top-left position and size.
is_eq : _
Compare two of these values.
Circle
:= {
center : Vec2,
radius : F32,
}
Circle represented by center and radius.
is_eq : _
Compare two of these values.