Documentation
¶
Index ¶
- func Abs[T ~float64 | ~int](v T) T
- func CircleIntersect(c1, c2 Circle) bool
- func CircularMovement(cx, cy, radius, angleRad float64) (float64, float64)
- func Clamp[T ~float64 | ~int](value, lower, upper T) T
- func DegToRad(deg float64) float64
- func EuclideanDistance(x1, y1, x2, y2 float64) float64
- func IsInFront(entityPos, targetPos, facingDir Vector2) bool
- func IsTargetInVisionCone(enemyPos, facingDir, targetPos Vector2, coneAngleDegrees float64, ...) bool
- func OBBvsOBB(p1, p2 Polygon, pos1, pos2 Vector2, rot1, rot2 float64) bool
- func RadToDeg(rad float64) float64
- func RectIntersect(r1, r2 Rect) bool
- func Sign[T ~float64 | ~int](x T) T
- func SinCos(rad float64) (float64, float64)
- func SquaredDistance(x1, y1, x2, y2 float64) float64
- type Circle
- type Mat3
- type Polygon
- type Rect
- type Vector2
- func FromAngle(angle float64) Vector2
- func Lerp(a, b Vector2, t float64) Vector2
- func Rotate(point Vector2, radians float64) Vector2
- func RotateAround(point, pivot Vector2, radians float64) Vector2
- func Scale(point Vector2, scaleX, scaleY float64) Vector2
- func ScaleAround(point, pivot Vector2, scaleX, scaleY float64) Vector2
- func TransformPoint(m Mat3, v Vector2) Vector2
- func Translate(point, offset Vector2) Vector2
- func (v Vector2) Add(o Vector2) Vector2
- func (v Vector2) Angle() float64
- func (v Vector2) AngleBetween(o Vector2) float64
- func (v Vector2) Distance(o Vector2) float64
- func (v Vector2) DistanceSquared(o Vector2) float64
- func (v Vector2) Div(scalar float64) Vector2
- func (v Vector2) Dot(o Vector2) float64
- func (v Vector2) IsFacingSameDirection(o Vector2, toleranceDegrees float64) bool
- func (v Vector2) Length() float64
- func (v Vector2) LengthSquared() float64
- func (v Vector2) Mul(scalar float64) Vector2
- func (v Vector2) Normalize() Vector2
- func (v Vector2) Reflect(normal Vector2) Vector2
- func (v Vector2) Rotate(angle float64) Vector2
- func (v Vector2) Sub(o Vector2) Vector2
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CircleIntersect ¶
CircleIntersect checks if two circles intersect.
func CircularMovement ¶
CircularMovement returns a point on a circle given center, radius, and angle in radians. Example: enemyPos := CircularMovement(centerX, centerY, 5, timeElapsed)
func EuclideanDistance ¶
EuclideanDistance returns the distance between two points. Euclidean Distance: - Use when actual distances matter (e.g., clustering, nearest-neighbor search, measuring geometric distance). - Needed when comparisons must be interpretable in the original units.
Example: dist := EuclideanDistance(p1x, p1y, p2x, p2y)
func IsInFront ¶
IsInFront checks if the target position is in front of the entity's facing direction. facingDir should be normalized.
func IsTargetInVisionCone ¶
func IsTargetInVisionCone(enemyPos, facingDir, targetPos Vector2, coneAngleDegrees float64, maxDistance float64) bool
IsTargetInVisionCone checks if target is inside enemy's vision cone. facingDir should be normalized. coneAngleDegrees is the full width of the vision cone (e.g., 90 means 45° to each side).
func OBBvsOBB ¶
OBBvsOBB checks if two oriented bounding boxes intersect. This uses the Separating Axis Theorem (SAT) for convex polygons.
func RectIntersect ¶
RectIntersect checks if two axis-aligned rectangles intersect.
func Sign ¶
Sign returns -1 for negative numbers, 1 for positive numbers, and 0 for zero. Example: direction := Sign(velocity.X)
func SinCos ¶
SinCos returns sin(angle), cos(angle) for given radians. Useful for circular motion, aiming, oscillations.
func SquaredDistance ¶
SquaredDistance returns the squared distance between two points (faster). Squared Distance: - Often used in optimization because it is easier to compute and differentiate (no square root). - Common in k-means clustering (objective function minimizes squared distances, though nearest-centroid assignment still uses Euclidean distance). - Used in many machine learning loss functions (like Mean Squared Error).
Example: if SquaredDistance(x1, y1, x2, y2) < 25 { /* close enough */ }
Types ¶
type Mat3 ¶
type Mat3 [3][3]float64
Mat3 represents a 3x3 transformation matrix.
func RotationMatrix ¶
RotationMatrix creates a rotation matrix (radians).
func ScalingMatrix ¶
ScalingMatrix creates a scaling matrix.
func TranslationMatrix ¶
TranslationMatrix creates a translation matrix.
type Polygon ¶
type Polygon struct {
Points []Vector2 // Using Vector2 from other file
}
Polygon represents a convex polygon with points in local space.
type Vector2 ¶
type Vector2 struct {
X, Y float64
}
Vector2 represents a 2D vector with X and Y components.
func FromAngle ¶
FromAngle creates a unit vector from an angle in radians. Example: dir := FromAngle(playerAimAngle).Mul(speed)
func Lerp ¶
Lerp linearly interpolates between two vectors by t (0 to 1). Example: smoothPos := Lerp(playerPos, targetPos, 0.1)
func Rotate ¶
Rotate rotates a point around the origin by radians. Example: rotated := Rotate(point, math.Pi/4)
func RotateAround ¶
RotateAround rotates a point around a pivot by radians. Example: rotated := RotateAround(point, pivot, math.Pi/2)
func Scale ¶
Scale scales a point relative to the origin (0,0). Example: bigger := Scale(point, 2, 2)
func ScaleAround ¶
ScaleAround scales a point around a specific pivot. Example: bigger := ScaleAround(point, pivot, 2, 2)
func TransformPoint ¶
TransformPoint applies a 3x3 transformation matrix to a Vector2.
func Translate ¶
Translate moves a point by an offset. Example: newPos := Translate(oldPos, Vector2{1, 0})
func (Vector2) Add ¶
Add returns the sum of two vectors. Example: playerPos = playerPos.Add(playerVelocity)
func (Vector2) Angle ¶
Angle returns the angle (in radians) of the vector from the X-axis. Example: bulletAngle := bulletDir.Angle()
func (Vector2) AngleBetween ¶
AngleBetween returns the angle (in radians) between two vectors. Example: angle := v1.AngleBetween(v2)
func (Vector2) Distance ¶
Distance returns the distance between two points. Example: dist := playerPos.Distance(enemyPos)
func (Vector2) DistanceSquared ¶
DistanceSquared returns squared distance between two points. Example: if playerPos.DistanceSquared(enemyPos) < 10000 { /* in range */ }
func (Vector2) Div ¶
Div returns the vector divided by a scalar. Example: slowVelocity := velocity.Div(2)
func (Vector2) Dot ¶
Dot returns the dot product of two vectors. Example: if velocity.Dot(wallNormal) < 0 { /* moving into wall */ }
func (Vector2) IsFacingSameDirection ¶
IsFacingSameDirection returns true if the angle between the vectors is small. Useful for checking if two entities are aligned in direction.
func (Vector2) Length ¶
Length returns the magnitude (length) of the vector. Example: speed := velocity.Length()
func (Vector2) LengthSquared ¶
LengthSquared returns the squared magnitude (faster, no sqrt). Example: if pos.Sub(target).LengthSquared() < 25 { /* close enough */ }
func (Vector2) Mul ¶
Mul returns the vector scaled by a scalar. Example: fastBulletVelocity := bulletDirection.Mul(500)
func (Vector2) Normalize ¶
Normalize returns the unit vector in the same direction. Example: moveDir := inputVector.Normalize()
func (Vector2) Reflect ¶
Reflect returns the vector reflected across a normal. Example: bounce := velocity.Reflect(surfaceNormal)