xmath

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T ~float64 | ~int](v T) T

func CircleIntersect

func CircleIntersect(c1, c2 Circle) bool

CircleIntersect checks if two circles intersect.

func CircularMovement

func CircularMovement(cx, cy, radius, angleRad float64) (float64, float64)

CircularMovement returns a point on a circle given center, radius, and angle in radians. Example: enemyPos := CircularMovement(centerX, centerY, 5, timeElapsed)

func Clamp

func Clamp[T ~float64 | ~int](value, lower, upper T) T

func DegToRad

func DegToRad(deg float64) float64

DegToRad converts degrees to radians. Example: angleRad := DegToRad(90)

func EuclideanDistance

func EuclideanDistance(x1, y1, x2, y2 float64) float64

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

func IsInFront(entityPos, targetPos, facingDir Vector2) bool

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

func OBBvsOBB(p1, p2 Polygon, pos1, pos2 Vector2, rot1, rot2 float64) bool

OBBvsOBB checks if two oriented bounding boxes intersect. This uses the Separating Axis Theorem (SAT) for convex polygons.

func RadToDeg

func RadToDeg(rad float64) float64

RadToDeg converts radians to degrees. Example: angleDeg := RadToDeg(math.Pi)

func RectIntersect

func RectIntersect(r1, r2 Rect) bool

RectIntersect checks if two axis-aligned rectangles intersect.

func Sign

func Sign[T ~float64 | ~int](x T) T

Sign returns -1 for negative numbers, 1 for positive numbers, and 0 for zero. Example: direction := Sign(velocity.X)

func SinCos

func SinCos(rad float64) (float64, float64)

SinCos returns sin(angle), cos(angle) for given radians. Useful for circular motion, aiming, oscillations.

func SquaredDistance

func SquaredDistance(x1, y1, x2, y2 float64) float64

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 Circle

type Circle struct {
	X, Y   float64
	Radius float64
}

Circle represents a circle shape.

type Mat3

type Mat3 [3][3]float64

Mat3 represents a 3x3 transformation matrix.

func Identity3

func Identity3() Mat3

Identity3 returns the identity matrix.

func MulMat3

func MulMat3(a, b Mat3) Mat3

MulMat3 multiplies two 3x3 matrices.

func RotationMatrix

func RotationMatrix(radians float64) Mat3

RotationMatrix creates a rotation matrix (radians).

func ScalingMatrix

func ScalingMatrix(sx, sy float64) Mat3

ScalingMatrix creates a scaling matrix.

func TranslationMatrix

func TranslationMatrix(tx, ty float64) Mat3

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 Rect

type Rect struct {
	X, Y          float64 // Top-left corner
	Width, Height float64
}

Rect represents an axis-aligned rectangle.

type Vector2

type Vector2 struct {
	X, Y float64
}

Vector2 represents a 2D vector with X and Y components.

func FromAngle

func FromAngle(angle float64) Vector2

FromAngle creates a unit vector from an angle in radians. Example: dir := FromAngle(playerAimAngle).Mul(speed)

func Lerp

func Lerp(a, b Vector2, t float64) Vector2

Lerp linearly interpolates between two vectors by t (0 to 1). Example: smoothPos := Lerp(playerPos, targetPos, 0.1)

func Rotate

func Rotate(point Vector2, radians float64) Vector2

Rotate rotates a point around the origin by radians. Example: rotated := Rotate(point, math.Pi/4)

func RotateAround

func RotateAround(point, pivot Vector2, radians float64) Vector2

RotateAround rotates a point around a pivot by radians. Example: rotated := RotateAround(point, pivot, math.Pi/2)

func Scale

func Scale(point Vector2, scaleX, scaleY float64) Vector2

Scale scales a point relative to the origin (0,0). Example: bigger := Scale(point, 2, 2)

func ScaleAround

func ScaleAround(point, pivot Vector2, scaleX, scaleY float64) Vector2

ScaleAround scales a point around a specific pivot. Example: bigger := ScaleAround(point, pivot, 2, 2)

func TransformPoint

func TransformPoint(m Mat3, v Vector2) Vector2

TransformPoint applies a 3x3 transformation matrix to a Vector2.

func Translate

func Translate(point, offset Vector2) Vector2

Translate moves a point by an offset. Example: newPos := Translate(oldPos, Vector2{1, 0})

func (Vector2) Add

func (v Vector2) Add(o Vector2) Vector2

Add returns the sum of two vectors. Example: playerPos = playerPos.Add(playerVelocity)

func (Vector2) Angle

func (v Vector2) Angle() float64

Angle returns the angle (in radians) of the vector from the X-axis. Example: bulletAngle := bulletDir.Angle()

func (Vector2) AngleBetween

func (v Vector2) AngleBetween(o Vector2) float64

AngleBetween returns the angle (in radians) between two vectors. Example: angle := v1.AngleBetween(v2)

func (Vector2) Distance

func (v Vector2) Distance(o Vector2) float64

Distance returns the distance between two points. Example: dist := playerPos.Distance(enemyPos)

func (Vector2) DistanceSquared

func (v Vector2) DistanceSquared(o Vector2) float64

DistanceSquared returns squared distance between two points. Example: if playerPos.DistanceSquared(enemyPos) < 10000 { /* in range */ }

func (Vector2) Div

func (v Vector2) Div(scalar float64) Vector2

Div returns the vector divided by a scalar. Example: slowVelocity := velocity.Div(2)

func (Vector2) Dot

func (v Vector2) Dot(o Vector2) float64

Dot returns the dot product of two vectors. Example: if velocity.Dot(wallNormal) < 0 { /* moving into wall */ }

func (Vector2) IsFacingSameDirection

func (v Vector2) IsFacingSameDirection(o Vector2, toleranceDegrees float64) bool

IsFacingSameDirection returns true if the angle between the vectors is small. Useful for checking if two entities are aligned in direction.

func (Vector2) Length

func (v Vector2) Length() float64

Length returns the magnitude (length) of the vector. Example: speed := velocity.Length()

func (Vector2) LengthSquared

func (v Vector2) LengthSquared() float64

LengthSquared returns the squared magnitude (faster, no sqrt). Example: if pos.Sub(target).LengthSquared() < 25 { /* close enough */ }

func (Vector2) Mul

func (v Vector2) Mul(scalar float64) Vector2

Mul returns the vector scaled by a scalar. Example: fastBulletVelocity := bulletDirection.Mul(500)

func (Vector2) Normalize

func (v Vector2) Normalize() Vector2

Normalize returns the unit vector in the same direction. Example: moveDir := inputVector.Normalize()

func (Vector2) Reflect

func (v Vector2) Reflect(normal Vector2) Vector2

Reflect returns the vector reflected across a normal. Example: bounce := velocity.Reflect(surfaceNormal)

func (Vector2) Rotate

func (v Vector2) Rotate(angle float64) Vector2

Rotate rotates the vector by an angle in radians. Example: rotatedDir := direction.Rotate(math.Pi / 4) // 45° turn

func (Vector2) Sub

func (v Vector2) Sub(o Vector2) Vector2

Sub returns the difference of two vectors. Example: toTarget := enemyPos.Sub(playerPos)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL