Aego Русский

aego/math

There is no overloading: Vec3 variants carry a 3 suffix (Length3, Dot3) and int variants an Int suffix (MinInt, ClampInt). Angles are in radians; Deg2Rad and Rad2Deg convert. Vector arithmetic (v + w, v * s) is part of the language and needs no import.

On this page

Example

package demo

import (
    "aego/log"
    "aego/math"
)

script Orbit {
    @export var Radius float = 3
    @export var Center Vec2 = Vec2{1, 1}

    var angle float
    var frames int

    func Ready() {
        to := Vec2{3, 4}
        log.Info("length " + string(math.Length(to)) + ", heading " + string(math.Round(math.Rad2Deg(math.Angle(to)))) + " deg")
        log.Info("unit " + string(math.Normalize(to).X) + ", " + string(math.Normalize(to).Y))
        log.Info("clamp 12 to 0..10: " + string(math.Clamp(12, 0, 10)))
        log.Info("ammo after pickup: " + string(math.MinInt(25 + 10, 30)))
    }

    func Update(dt float) {
        angle += math.Tau / 2 * dt
        frames++
        if frames % 30 == 0 {
            p := Center + Vec2{math.Cos(angle), math.Sin(angle)} * Radius
            node.SetPosition(p)
            log.Info("frame " + string(frames) + ": at " + string(math.Round(p.X * 100) / 100) + ", " + string(math.Round(p.Y * 100) / 100) + ", " + string(math.Dist(p, Center)) + " from center")
        }
    }
}

Functions

Abs(x float) floatReturns the absolute value of x.
AbsInt(x int) intReturns the absolute value of an int.
Acos(x float) floatReturns the angle in radians whose cosine is x, in [0, Pi].
Angle(v Vec2) floatReturns the direction of v in radians.
Asin(x float) floatReturns the angle in radians whose sine is x, in [-Pi/2, Pi/2].
Atan2(y float, x float) floatReturns the angle in radians of the vector (x, y), in (-Pi, Pi].
Ceil(x float) floatReturns the least whole number greater than or equal to x.
Clamp(v float, lo float, hi float) floatLimits v to the range [lo, hi].
ClampInt(v int, lo int, hi int) intLimits an int to the range [lo, hi].
Cos(x float) floatReturns the cosine of an angle in radians.
Cross(a Vec2, b Vec2) floatReturns the z component of the 3D cross product; its sign tells on which side b lies.
Cross3(a Vec3, b Vec3) Vec3Returns a vector perpendicular to a and b.
Deg2Rad(degrees float) floatConverts degrees to radians.
Dist(a Vec2, b Vec2) floatReturns the distance between two points.
Dist3(a Vec3, b Vec3) floatReturns the distance between two 3D points.
Dot(a Vec2, b Vec2) floatReturns the dot product of two Vec2.
Dot3(a Vec3, b Vec3) floatReturns the dot product of two Vec3.
Exp(x float) floatReturns e raised to the power x.
Floor(x float) floatReturns the greatest whole number less than or equal to x.
Length(v Vec2) floatReturns the length of a Vec2.
Length3(v Vec3) floatReturns the length of a Vec3.
Lerp(a float, b float, t float) floatBlends from a to b. t = 0 gives a, t = 1 gives b; t is not clamped.
LerpVec2(a Vec2, b Vec2, t float) Vec2Blends two Vec2 component-wise.
LerpVec3(a Vec3, b Vec3, t float) Vec3Blends two Vec3 component-wise.
Log(x float) floatReturns the natural logarithm of x.
Max(a float, b float) floatReturns the larger of a and b.
MaxInt(a int, b int) intReturns the larger of two ints.
Min(a float, b float) floatReturns the smaller of a and b.
MinInt(a int, b int) intReturns the smaller of two ints.
Normalize(v Vec2) Vec2Returns v scaled to length 1. A zero vector stays zero.
Normalize3(v Vec3) Vec3Returns v scaled to length 1.
Pow(x float, y float) floatReturns x raised to the power y.
QuatAxisAngle(axis Vec3, radians float) QuatBuilds a rotation of an angle in radians around an axis.
QuatEuler(x float, y float, z float) QuatBuilds a rotation from Euler angles in radians, applied as Z·Y·X.
QuatLookDir(dir Vec3) QuatReturns the rotation that points -Z along d.
QuatMul(a Quat, b Quat) QuatCombines two rotations: b is applied first, then a.
QuatRotate(q Quat, v Vec3) Vec3Rotates a vector by q.
QuatSlerp(a Quat, b Quat, t float) QuatBlends two rotations along the shortest arc.
Rad2Deg(radians float) floatConverts radians to degrees.
Rotate(v Vec2, radians float) Vec2Turns v clockwise by an angle in radians.
Round(x float) floatReturns x rounded to the nearest whole number; halves round away from zero.
Sign(x float) floatReturns -1, 0 or 1 depending on the sign of x.
Sin(x float) floatReturns the sine of an angle in radians.
Sqrt(x float) floatReturns the square root of x. Negative x gives NaN.
Tan(x float) floatReturns the tangent of an angle in radians.
Trunc(x float) floatDrops the fractional part of x.

2 constants and enum values are listed below.

Abs function

func Abs(x float) float

Returns the absolute value of x.

Example

dist := math.Abs(target - x)

AbsInt function

func AbsInt(x int) int

Returns the absolute value of an int.

Example

steps := math.AbsInt(to - from)

Acos function

func Acos(x float) float

Returns the angle in radians whose cosine is x, in [0, Pi].

Example

a := math.Acos(math.Dot(u, v))

Angle function

func Angle(v Vec2) float

Returns the direction of v in radians.

Example

node.SetRotation(math.Angle(velocity))

Asin function

func Asin(x float) float

Returns the angle in radians whose sine is x, in [-Pi/2, Pi/2].

Example

a := math.Asin(0.5)

Atan2 function

func Atan2(y float, x float) float

Returns the angle in radians of the vector (x, y), in (-Pi, Pi].

Example

angle := math.Atan2(d.Y, d.X)

Ceil function

func Ceil(x float) float

Returns the least whole number greater than or equal to x.

Example

pages := math.Ceil(float(count) / 10)

Clamp function

func Clamp(v float, lo float, hi float) float

Limits v to the range [lo, hi].

Example

x = math.Clamp(x, -10, 10)

ClampInt function

func ClampInt(v int, lo int, hi int) int

Limits an int to the range [lo, hi].

Example

level = math.ClampInt(level, 1, 10)

Cos function

func Cos(x float) float

Returns the cosine of an angle in radians.

Example

x := math.Cos(angle) * Radius

Cross function

func Cross(a Vec2, b Vec2) float

Returns the z component of the 3D cross product; its sign tells on which side b lies.

Example

left := math.Cross(forward, toTarget) < 0

Cross3 function

func Cross3(a Vec3, b Vec3) Vec3

Returns a vector perpendicular to a and b.

Example

right := math.Cross3(forward, Vec3{0, 1, 0})

Deg2Rad function

func Deg2Rad(degrees float) float

Converts degrees to radians.

Example

node.SetRotation(math.Deg2Rad(45))

Dist function

func Dist(a Vec2, b Vec2) float

Returns the distance between two points.

Example

if math.Dist(pos, goal) < 0.1 {

Dist3 function

func Dist3(a Vec3, b Vec3) float

Returns the distance between two 3D points.

Example

near := math.Dist3(a, b) < 2

Dot function

func Dot(a Vec2, b Vec2) float

Returns the dot product of two Vec2.

Example

facing := math.Dot(forward, toPlayer) > 0

Dot3 function

func Dot3(a Vec3, b Vec3) float

Returns the dot product of two Vec3.

Example

lit := math.Dot3(normal, light) > 0

Exp function

func Exp(x float) float

Returns e raised to the power x.

Example

decay := math.Exp(-dt * Damping)

Floor function

func Floor(x float) float

Returns the greatest whole number less than or equal to x.

Example

cell := math.Floor(pos.X / 32)

Length function

func Length(v Vec2) float

Returns the length of a Vec2.

Example

speed := math.Length(velocity)

Length3 function

func Length3(v Vec3) float

Returns the length of a Vec3.

Example

d := math.Length3(velocity)

Lerp function

func Lerp(a float, b float, t float) float

Blends from a to b. t = 0 gives a, t = 1 gives b; t is not clamped.

Example

zoom = math.Lerp(zoom, target, 5 * dt)

LerpVec2 function

func LerpVec2(a Vec2, b Vec2, t float) Vec2

Blends two Vec2 component-wise.

Example

cam = math.LerpVec2(cam, target, 4 * dt)

LerpVec3 function

func LerpVec3(a Vec3, b Vec3, t float) Vec3

Blends two Vec3 component-wise.

Example

p := math.LerpVec3(from, to, t)

Log function

func Log(x float) float

Returns the natural logarithm of x.

Example

octave := math.Log(freq) / math.Log(2)

Max function

func Max(a float, b float) float

Returns the larger of a and b.

Example

hp := math.Max(hp - damage, 0)

MaxInt function

func MaxInt(a int, b int) int

Returns the larger of two ints.

Example

lives = math.MaxInt(lives - 1, 0)

Min function

func Min(a float, b float) float

Returns the smaller of a and b.

Example

speed := math.Min(speed, MaxSpeed)

MinInt function

func MinInt(a int, b int) int

Returns the smaller of two ints.

Example

ammo = math.MinInt(ammo + 10, MaxAmmo)

Normalize function

func Normalize(v Vec2) Vec2

Returns v scaled to length 1. A zero vector stays zero.

Example

dir := math.Normalize(target - pos)

Normalize3 function

func Normalize3(v Vec3) Vec3

Returns v scaled to length 1.

Example

dir := math.Normalize3(target - pos)

Pi const

const Pi float

Pi is the ratio of a circle's circumference to its diameter, about 3.14159.

Pow function

func Pow(x float, y float) float

Returns x raised to the power y.

Example

ease := math.Pow(t, 3)

QuatAxisAngle function

func QuatAxisAngle(axis Vec3, radians float) Quat

Builds a rotation of an angle in radians around an axis.

Example

spin := math.QuatAxisAngle(Vec3{0, 1, 0}, Speed * dt)

QuatEuler function

func QuatEuler(x float, y float, z float) Quat

Builds a rotation from Euler angles in radians, applied as Z·Y·X.

Example

node.SetRotation3(math.QuatEuler(0, math.Pi, 0))

QuatLookDir function

func QuatLookDir(dir Vec3) Quat

Returns the rotation that points -Z along d.

Example

node.SetRotation3(math.QuatLookDir(target - pos))

QuatMul function

func QuatMul(a Quat, b Quat) Quat

Combines two rotations: b is applied first, then a.

Example

node.SetRotation3(math.QuatMul(spin, node.Rotation3()))

QuatRotate function

func QuatRotate(q Quat, v Vec3) Vec3

Rotates a vector by q.

Example

forward := math.QuatRotate(node.Rotation3(), Vec3{0, 0, -1})

QuatSlerp function

func QuatSlerp(a Quat, b Quat, t float) Quat

Blends two rotations along the shortest arc.

Example

r := math.QuatSlerp(from, to, 0.5)

Rad2Deg function

func Rad2Deg(radians float) float

Converts radians to degrees.

Example

log.Info(string(math.Rad2Deg(node.Rotation())))

Rotate function

func Rotate(v Vec2, radians float) Vec2

Turns v clockwise by an angle in radians.

Example

offset := math.Rotate(Vec2{1, 0}, node.Rotation())

Round function

func Round(x float) float

Returns x rounded to the nearest whole number; halves round away from zero.

Example

snapped := math.Round(x)

Sign function

func Sign(x float) float

Returns -1, 0 or 1 depending on the sign of x.

Example

dir := math.Sign(input.Axis("horizontal"))

Sin function

func Sin(x float) float

Returns the sine of an angle in radians.

Example

y := math.Sin(time.Elapsed() * 2) * Amplitude

Sqrt function

func Sqrt(x float) float

Returns the square root of x. Negative x gives NaN.

Example

d := math.Sqrt(dx*dx + dy*dy)

Tan function

func Tan(x float) float

Returns the tangent of an angle in radians.

Example

slope := math.Tan(math.Deg2Rad(30))

Tau const

const Tau float

Tau is a full turn in radians, 2·Pi.

Trunc function

func Trunc(x float) float

Drops the fractional part of x.

Example

whole := math.Trunc(-2.7)