Documentation
¶
Overview ¶
Package math2d provides small, zero-allocation 2D value types shared by game packages: vectors (Vec2), axis-aligned rectangles (Rect), circles (Circle), and 3×2 affine transforms (Affine).
Coordinates use float32 to match NSGo's core geometry (core.Points, core.Point, core.Size, core.Bounds) and core.Drawing, with lossless conversions to and from core. Angles are in radians, turning from +X toward +Y (clockwise in NSGo's Y-down drawing coordinates).
Index ¶
- type Affine
- func (m Affine) ApproxEqual(o Affine, eps float32) bool
- func (m Affine) Invert() (Affine, bool)
- func (m Affine) Mul(other Affine) Affine
- func (m Affine) ToCore() core.Matrix3D
- func (m Affine) TransformPoint(p Vec2) Vec2
- func (m Affine) TransformRect(r Rect) Rect
- func (m Affine) TransformVector(v Vec2) Vec2
- type Circle
- type Rect
- func (r Rect) ApproxEqual(o Rect, eps float32) bool
- func (r Rect) Bounds() core.Bounds
- func (r Rect) Center() Vec2
- func (r Rect) ClampPoint(p Vec2) Vec2
- func (r Rect) ClosestPoint(p Vec2) Vec2
- func (r Rect) Contains(p Vec2) bool
- func (r Rect) ContainsRect(o Rect) bool
- func (r Rect) Empty() bool
- func (r Rect) Expand(margin float32) Rect
- func (r Rect) Intersection(o Rect) (Rect, bool)
- func (r Rect) Max() Vec2
- func (r Rect) Min() Vec2
- func (r Rect) Normalized() Rect
- func (r Rect) Overlaps(o Rect) bool
- func (r Rect) Size() Vec2
- func (r Rect) Translate(v Vec2) Rect
- func (r Rect) Union(o Rect) Rect
- type Vec2
- func (v Vec2) Add(o Vec2) Vec2
- func (v Vec2) Angle() float32
- func (v Vec2) ApproxEqual(o Vec2, eps float32) bool
- func (v Vec2) Clamp(lo, hi Vec2) Vec2
- func (v Vec2) ClampLen(maxLen float32) Vec2
- func (v Vec2) Cross(o Vec2) float32
- func (v Vec2) Dist(o Vec2) float32
- func (v Vec2) DistSq(o Vec2) float32
- func (v Vec2) Div(o Vec2) Vec2
- func (v Vec2) Dot(o Vec2) float32
- func (v Vec2) Len() float32
- func (v Vec2) LenSq() float32
- func (v Vec2) Lerp(o Vec2, t float32) Vec2
- func (v Vec2) Max(o Vec2) Vec2
- func (v Vec2) Min(o Vec2) Vec2
- func (v Vec2) MoveTowards(target Vec2, maxDelta float32) Vec2
- func (v Vec2) Mul(o Vec2) Vec2
- func (v Vec2) Neg() Vec2
- func (v Vec2) Normalize() Vec2
- func (v Vec2) Perp() Vec2
- func (v Vec2) Point() core.Point
- func (v Vec2) Project(onto Vec2) Vec2
- func (v Vec2) Reflect(normal Vec2) Vec2
- func (v Vec2) Rotate(rad float32) Vec2
- func (v Vec2) Scale(s float32) Vec2
- func (v Vec2) Size() core.Size
- func (v Vec2) Sub(o Vec2) Vec2
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Affine ¶
Affine is a 2D 3×2 affine transform stored in the same column order as core.Drawing.Transform and core.Matrix3D.Affine (Canvas 2D / SVG matrix(a, b, c, d, e, f)):
[ A C Tx ] [ x ] [ A*x + C*y + Tx ] [ B D Ty ] [ y ] = [ B*x + D*y + Ty ] [ 0 0 1 ] [ 1 ] [ 1 ]
The zero value maps every point to (0, 0); use Identity for the no-op transform.
Example ¶
An Affine composes translation, rotation and scale; Invert maps a world point back into the local space, as picking under a camera does.
package main
import (
"fmt"
"math"
"strconv"
"github.com/nsgo-dev/game/pkg/math2d"
)
// show prints v to two decimals, writing a rounded negative zero as 0.
func show(v math2d.Vec2) string {
num := func(f float32) string {
s := strconv.FormatFloat(float64(f), 'f', 2, 32)
if s == "-0.00" {
s = "0.00"
}
return s
}
return "(" + num(v.X) + ", " + num(v.Y) + ")"
}
func main() {
m := math2d.TRS(math2d.V2(100, 50), math.Pi/2, math2d.V2(2, 2))
world := m.TransformPoint(math2d.V2(10, 0))
fmt.Println(show(world))
inv, ok := m.Invert()
fmt.Println(show(inv.TransformPoint(world)), ok)
}
Output: (100.00, 70.00) (10.00, 0.00) true
func FromCore ¶
FromCore extracts a 2D Affine from an NSGo core.Matrix3D. It returns (Affine{}, false) if m is not a pure 2D affine transform.
func RotateAround ¶
RotateAround returns a rotation transform by rad radians about pivot (cx, cy).
func TRS ¶
TRS returns a transform that scales by scale, rotates by rot radians, and translates by pos: Translate(pos.X, pos.Y).Mul(Rotate(rot)).Mul(Scale(scale.X, scale.Y)).
func (Affine) ApproxEqual ¶
ApproxEqual reports whether each element of m and o differs by at most eps. It returns false if eps < 0 or any value is NaN.
func (Affine) Invert ¶
Invert returns the inverse of m, or (Affine{}, false) if m is singular, degenerate, or contains non-finite values.
func (Affine) TransformPoint ¶
TransformPoint applies m (including translation) to point p.
func (Affine) TransformRect ¶
TransformRect transforms the four corners of r by m and returns the tightest axis-aligned bounding Rect enclosing them. It returns the zero Rect if r is empty.
func (Affine) TransformVector ¶
TransformVector applies only the linear part of m (ignoring translation) to vector v.
type Circle ¶
Circle is a 2D circle defined by its Center and Radius.
A circle with Radius <= 0 (or NaN) is empty and contains or overlaps nothing.
Example ¶
Circle tests overlaps with other circles and with rectangles.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/math2d"
)
func main() {
bullet := math2d.Circle{Center: math2d.V2(5, 5), Radius: 1}
enemy := math2d.Circle{Center: math2d.V2(6.5, 5), Radius: 1}
wall := math2d.Rect{X: 10, Y: 0, W: 2, H: 10}
fmt.Println(bullet.Overlaps(enemy), bullet.OverlapsRect(wall), bullet.Bounds())
}
Output: true false {4 4 2 2}
func (Circle) ApproxEqual ¶
ApproxEqual reports whether c and o have centers and radii within eps. It returns false if eps < 0 or any value is NaN.
func (Circle) Bounds ¶
Bounds returns the axis-aligned bounding Rect of c, or the zero Rect if Radius < 0 or any component is NaN.
func (Circle) OverlapsRect ¶
OverlapsRect reports whether c and non-empty rectangle r intersect (including boundary contact).
type Rect ¶
type Rect struct {
X, Y, W, H float32
}
Rect is an axis-aligned 2D rectangle given by its origin (X, Y) and size (W, H).
A rectangle with W <= 0 or H <= 0 (or any NaN field) is empty. Containment uses half-open intervals [X, X+W) × [Y, Y+H), matching NSGo's core.Bounds.
Example ¶
Rect tests overlaps between axis-aligned boxes and clips one by another.
package main
import (
"fmt"
"strconv"
"github.com/nsgo-dev/game/pkg/math2d"
)
// show prints v to two decimals, writing a rounded negative zero as 0.
func show(v math2d.Vec2) string {
num := func(f float32) string {
s := strconv.FormatFloat(float64(f), 'f', 2, 32)
if s == "-0.00" {
s = "0.00"
}
return s
}
return "(" + num(v.X) + ", " + num(v.Y) + ")"
}
func main() {
player := math2d.Rect{X: 0, Y: 0, W: 10, H: 20}
spike := math2d.FromCenter(math2d.V2(12, 10), math2d.V2(8, 8))
fmt.Println(player.Overlaps(spike))
hit, ok := player.Intersection(spike)
fmt.Println(hit, ok)
fmt.Println(show(player.ClosestPoint(math2d.V2(20, 25))))
}
Output: true {8 6 2 8} true (10.00, 20.00)
func FromBounds ¶
FromBounds converts an NSGo core.Bounds to a Rect losslessly.
func FromCenter ¶
FromCenter returns a Rect centered at center with the given size.
func FromMinMax ¶
FromMinMax returns a Rect spanning from lo to hi. If lo has a component greater than hi, the resulting Rect has negative W or H; call Normalized to flip negative extents.
func (Rect) ApproxEqual ¶
ApproxEqual reports whether each field of r and o differs by at most eps. It returns false if eps < 0 or any value is NaN.
func (Rect) ClampPoint ¶
ClampPoint clamps p to the closed region spanned by r. Negative W or H is normalized before clamping.
func (Rect) ClosestPoint ¶
ClosestPoint returns the point in the closed rectangle r closest to p. For points inside r, it returns p; for points outside, it returns the nearest point on r's perimeter.
func (Rect) Contains ¶
Contains reports whether p lies within r's half-open bounds [X, X+W) × [Y, Y+H).
func (Rect) ContainsRect ¶
ContainsRect reports whether non-empty rectangle o lies completely inside non-empty r. It returns false if either rectangle is empty.
func (Rect) Empty ¶
Empty reports whether r has zero or negative area or contains any NaN component.
func (Rect) Expand ¶
Expand returns r expanded outward by margin on all four sides (negative shrinks).
func (Rect) Intersection ¶
Intersection returns the overlapping region of r and o, or (Rect{}, false) if they do not overlap or either is empty.
func (Rect) Normalized ¶
Normalized returns an equivalent Rect with non-negative W and H, shifting X or Y when W or H is negative.
type Vec2 ¶
type Vec2 struct {
X, Y float32
}
Vec2 is a 2D vector or point in float32 coordinates.
Example ¶
Vec2 is a 2D vector for positions, velocities and directions.
package main
import (
"fmt"
"math"
"strconv"
"github.com/nsgo-dev/game/pkg/math2d"
)
// show prints v to two decimals, writing a rounded negative zero as 0.
func show(v math2d.Vec2) string {
num := func(f float32) string {
s := strconv.FormatFloat(float64(f), 'f', 2, 32)
if s == "-0.00" {
s = "0.00"
}
return s
}
return "(" + num(v.X) + ", " + num(v.Y) + ")"
}
func main() {
pos := math2d.V2(0, 0)
target := math2d.V2(3, 4)
fmt.Println(pos.Dist(target), show(target.Sub(pos).Normalize()))
fmt.Println(show(pos.MoveTowards(target, 2)))
fmt.Println(show(math2d.V2(1, -1).Reflect(math2d.UnitY())))
fmt.Println(show(math2d.UnitX().Rotate(math.Pi / 2)))
}
Output: 5 (0.60, 0.80) (1.20, 1.60) (1.00, 1.00) (0.00, 1.00)
func (Vec2) ApproxEqual ¶
ApproxEqual reports whether each component of v and o differs by at most eps. It returns false if eps < 0 or any value is NaN.
func (Vec2) ClampLen ¶
ClampLen returns v scaled down so its length does not exceed maxLen. If maxLen <= 0 or NaN, or v has NaN or infinite length, it returns the zero vector.
func (Vec2) MoveTowards ¶
MoveTowards moves v toward target by at most maxDelta without overshooting. If maxDelta <= 0 or NaN, or the distance is NaN, it returns v unchanged.
func (Vec2) Normalize ¶
Normalize returns a unit vector in the direction of v. If v has zero length, NaN, or infinite length, it returns the zero vector.
func (Vec2) Project ¶
Project returns the orthogonal projection of v onto onto. If onto has zero length, NaN, or infinite length, it returns the zero vector.