fixed

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package fixed provides a deterministic Q16.16 fixed-point number type and the trigonometric/root primitives the simulation needs.

The engine compiles to two targets: a native server build and a GOOS=js/GOARCH=wasm browser build. For client-side prediction to agree with the authoritative server, identical inputs must produce bit-identical outputs on both. Go's math package uses architecture-specific assembly for sin/cos/sqrt on amd64/arm64 but pure-Go fallbacks on wasm, so the two targets can disagree in the low bits. Everything here is integer-only (int64 arithmetic and shifts behave identically on every Go target), so the simulation never touches a hardware float and stays reproducible.

Index

Constants

View Source
const (
	FullCircle    int32 = 65536
	HalfCircle    int32 = 32768
	QuarterCircle int32 = 16384
)

Angles use Total Annihilation's native unit: a full turn is 65536, matching the COB integer angle scale. Storing headings as int32 TA-angles (rather than radians) keeps the whole sim in integers and reproducible.

Variables

This section is empty.

Functions

func AngleToRadians

func AngleToRadians(a int32) float64

AngleToRadians converts a TA-angle to radians for rendering/debug only.

func Atan2

func Atan2(y, x Fixed) int32

Atan2 returns the TA-angle of the vector (x, y) measured the way the engine's heading convention expects: it mirrors JS Math.atan2(y, x). Pass the "sine-side" component as y and the "cosine-side" as x. Heading toward a target at delta (dx, dz) is therefore Atan2(dx, dz). The result is in [0, 65536).

func NormalizeAngle

func NormalizeAngle(a int32) int32

NormalizeAngle folds any TA-angle into [0, 65536).

func RadiansToAngle

func RadiansToAngle(rad float64) int32

RadiansToAngle converts a radian float to a TA-angle. Boundary-only helper — never call inside the tick loop.

func ShortestArc

func ShortestArc(a int32) int32

ShortestArc maps a TA-angle delta into (-32768, 32768] — the shortest signed turn between two headings.

Types

type Fixed

type Fixed int64

Fixed is a Q16.16 signed fixed-point value stored in an int64. The wide backing integer keeps multiply intermediates from overflowing across the coordinate ranges Total Annihilation maps use (positions stay well under 2^15 world units).

const (
	// FracBits is the number of fractional bits in the Q16.16 layout.
	FracBits = 16
	// One is the fixed-point representation of 1.0.
	One Fixed = 1 << FracBits
	// Half is the fixed-point representation of 0.5.
	Half Fixed = One >> 1
	// Zero is the fixed-point representation of 0.0.
	Zero Fixed = 0
)

func Clamp

func Clamp(a, lo, hi Fixed) Fixed

Clamp constrains a to the inclusive range [lo, hi].

func Cos

func Cos(angle int32) Fixed

Cos returns the cosine of a TA-angle in Q16.16.

func FromFloat

func FromFloat(f float64) Fixed

FromFloat converts a float to fixed-point. This is the ONLY bridge from non-deterministic floats and must only be used at the system boundary (parsing FBI/asset values, decoding orders) — never inside the tick loop.

func FromInt

func FromInt(i int) Fixed

FromInt converts a whole number to fixed-point.

func Hypot

func Hypot(x, y Fixed) Fixed

Hypot returns sqrt(x*x + y*y) without the intermediate overflowing for the coordinate magnitudes the sim uses.

func Max

func Max(a, b Fixed) Fixed

Max returns the larger of a, b.

func Min

func Min(a, b Fixed) Fixed

Min returns the smaller of a, b.

func Sin

func Sin(angle int32) Fixed

Sin returns the sine of a TA-angle in Q16.16.

func SinCos

func SinCos(angle int32) (sin, cos Fixed)

SinCos returns the sine and cosine of a TA-angle as Q16.16 values in [-One, One]. It reduces to the first quadrant and applies exact symmetry, keeping CORDIC inside its convergence range.

func (Fixed) Abs

func (a Fixed) Abs() Fixed

Abs returns the absolute value.

func (Fixed) Div

func (a Fixed) Div(b Fixed) Fixed

Div divides a by b. Caller must ensure b != 0.

func (Fixed) Float

func (a Fixed) Float() float64

Float renders a fixed-point value back to float64 for rendering/debug. Never feed the result back into the simulation.

func (Fixed) Int

func (a Fixed) Int() int

Int truncates toward zero to a whole number.

func (Fixed) Mul

func (a Fixed) Mul(b Fixed) Fixed

Mul multiplies two fixed-point values.

func (Fixed) Sign

func (a Fixed) Sign() int

Sign returns -1, 0 or +1 as a plain int.

func (Fixed) Sqrt

func (a Fixed) Sqrt() Fixed

Sqrt returns the non-negative square root using integer Newton iteration. Negative inputs return 0. Fully deterministic across targets.

type Vec2

type Vec2 struct {
	X, Z Fixed
}

Vec2 is a planar (X, Z) coordinate — the ground plane the simulation moves units across. Y (height) is tracked separately on units.

func (Vec2) Add

func (a Vec2) Add(b Vec2) Vec2

Add returns a + b.

func (Vec2) DistTo

func (a Vec2) DistTo(b Vec2) Fixed

DistTo returns the distance from a to b.

func (Vec2) Len

func (a Vec2) Len() Fixed

Len returns the vector length.

func (Vec2) Sub

func (a Vec2) Sub(b Vec2) Vec2

Sub returns a - b.

type Vec3

type Vec3 struct {
	X, Y, Z Fixed
}

Vec3 is a full world-space coordinate with Y pointing up.

func (Vec3) XZ

func (a Vec3) XZ() Vec2

XZ projects a Vec3 onto the ground plane.

Jump to

Keyboard shortcuts

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