Documentation
¶
Overview ¶
Package geom provides the small geometric vocabulary shared by the Ebitengine tool family: float vectors for simulation space, integer coordinates and rectangles for tile grids, and clamping helpers.
Vec2 is the continuous-space vector, with the usual arithmetic plus the toroidal-world helpers (Vec2.WrapTo, Vec2.ShortestDelta, Vec2.ToroidalDist) the ecosystem sims rely on. Coord addresses a tile grid cell and Rect an axis-aligned span of cells; Coord.Vec2 bridges the two spaces at cell centres. Clamp and Clamp01 are the range limiters repeated across every settings screen and camera in the family.
Example ¶
Example steers an agent one normalized step toward a target, the way the family's sims move things around.
package main
import (
"fmt"
"github.com/danielriddell21/crucible/geom"
)
func main() {
pos := geom.Vec2{X: 1, Y: 1}
target := geom.Vec2{X: 4, Y: 5}
step := target.Sub(pos).Normalize()
pos = pos.Add(step)
fmt.Printf("moved to (%.1f, %.1f), %.0f left\n", pos.X, pos.Y, pos.Dist(target))
}
Output: moved to (1.6, 1.8), 4 left
Index ¶
- func Clamp[T cmp.Ordered](v, lo, hi T) T
- func Clamp01(v float64) float64
- type Coord
- type Rect
- type Vec2
- func (v Vec2) Add(o Vec2) Vec2
- func (v Vec2) Angle() float64
- func (v Vec2) Dist(o Vec2) float64
- func (v Vec2) DistSq(o Vec2) float64
- func (v Vec2) Dot(o Vec2) float64
- func (v Vec2) Len() float64
- func (v Vec2) Normalize() Vec2
- func (v Vec2) Scale(s float64) Vec2
- func (v Vec2) ShortestDelta(o Vec2, w, h float64) Vec2
- func (v Vec2) Sub(o Vec2) Vec2
- func (v Vec2) ToroidalDist(o Vec2, w, h float64) float64
- func (v Vec2) WrapTo(w, h float64) Vec2
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Rect ¶
type Rect struct {
X, Y, W, H int
}
Rect is an axis-aligned integer rectangle on a tile grid, described by its top-left corner and size.
type Vec2 ¶
type Vec2 struct {
X, Y float64
}
Vec2 is a two-dimensional vector in continuous (simulation or screen) space.
func (Vec2) DistSq ¶
DistSq returns the squared distance between v and o. It avoids the square root, so prefer it for comparisons.
func (Vec2) Normalize ¶
Normalize returns v scaled to unit length. The zero vector is returned unchanged.
func (Vec2) ShortestDelta ¶
ShortestDelta returns the shortest vector from v to o on a torus of the given size.
func (Vec2) ToroidalDist ¶
ToroidalDist returns the shortest distance between v and o on a torus of the given size.
Example ¶
ExampleVec2_ToroidalDist shows distance on a wrapping world: crossing the seam is shorter than walking across the map.
package main
import (
"fmt"
"github.com/danielriddell21/crucible/geom"
)
func main() {
a := geom.Vec2{X: 1, Y: 5}
b := geom.Vec2{X: 9, Y: 5}
fmt.Println(a.Dist(b), a.ToroidalDist(b, 10, 10))
}
Output: 8 2