math3d

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: 0BSD Imports: 1 Imported by: 0

Documentation

Overview

Package math3d provides 3D math primitives for the Trophy engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mat4

type Mat4 [16]float64

Mat4 is a 4x4 matrix stored in column-major order. This matches OpenGL conventions for easier reasoning about transforms.

Memory layout (indices): | 0 4 8 12 | | 1 5 9 13 | | 2 6 10 14 | | 3 7 11 15 |

For a transform matrix: | Xx Yx Zx Tx | X,Y,Z = basis vectors (rotation/scale) | Xy Yy Zy Ty | T = translation | Xz Yz Zz Tz | | 0 0 0 1 |.

func Identity

func Identity() Mat4

Identity returns the identity matrix.

func LookAt

func LookAt(eye, center, up Vec3) Mat4

LookAt creates a view matrix looking from eye towards center.

func Mat4FromSlice

func Mat4FromSlice(s []float64) Mat4

Mat4FromSlice creates a Mat4 from a slice of 16 floats (column-major order).

func Orthographic

func Orthographic(left, right, bottom, top, near, far float64) Mat4

Orthographic creates an orthographic projection matrix.

func Perspective

func Perspective(fovy, aspect, near, far float64) Mat4

Perspective creates a perspective projection matrix. fovy is vertical field of view in radians. aspect is width/height. near and far are clipping planes.

func QuatToMat4

func QuatToMat4(x, y, z, w float64) Mat4

QuatToMat4 converts a quaternion (x, y, z, w) to a rotation matrix. The quaternion is expected in GLTF format: (x, y, z, w) where w is the scalar.

func Rotate

func Rotate(axis Vec3, angle float64) Mat4

Rotate creates a rotation matrix around an arbitrary axis.

func RotateX

func RotateX(angle float64) Mat4

RotateX creates a rotation matrix around the X axis.

func RotateY

func RotateY(angle float64) Mat4

RotateY creates a rotation matrix around the Y axis.

func RotateZ

func RotateZ(angle float64) Mat4

RotateZ creates a rotation matrix around the Z axis.

func Scale

func Scale(v Vec3) Mat4

Scale creates a scaling matrix.

func ScaleUniform

func ScaleUniform(s float64) Mat4

ScaleUniform creates a uniform scaling matrix.

func Translate

func Translate(v Vec3) Mat4

Translate creates a translation matrix.

func (Mat4) Determinant

func (m Mat4) Determinant() float64

Determinant returns the determinant of the matrix.

func (Mat4) Get

func (m Mat4) Get(row, col int) float64

Get returns the element at (row, col).

func (Mat4) Inverse

func (m Mat4) Inverse() Mat4

Inverse returns the inverse of the matrix. Returns identity if the matrix is singular (det=0).

func (Mat4) Mul

func (m Mat4) Mul(b Mat4) Mat4

Mul multiplies two matrices: m * b.

func (Mat4) MulVec3

func (m Mat4) MulVec3(v Vec3) Vec3

MulVec3 transforms a Vec3 as a point (w=1).

func (Mat4) MulVec3Dir

func (m Mat4) MulVec3Dir(v Vec3) Vec3

MulVec3Dir transforms a Vec3 as a direction (w=0, no translation).

func (Mat4) MulVec4

func (m Mat4) MulVec4(v Vec4) Vec4

MulVec4 transforms a Vec4.

func (*Mat4) Set

func (m *Mat4) Set(row, col int, val float64)

Set sets the element at (row, col).

func (*Mat4) SetTranslation

func (m *Mat4) SetTranslation(v Vec3)

SetTranslation sets the translation component.

func (Mat4) Translation

func (m Mat4) Translation() Vec3

Translation extracts the translation component.

func (Mat4) Transpose

func (m Mat4) Transpose() Mat4

Transpose returns the transposed matrix.

type Vec2

type Vec2 struct {
	X, Y float64
}

Vec2 represents a 2D vector.

func V2

func V2(x, y float64) Vec2

V2 creates a new Vec2.

func Zero2

func Zero2() Vec2

Zero2 returns the zero vector.

func (Vec2) Add

func (a Vec2) Add(b Vec2) Vec2

Add returns the vector sum a + b.

func (Vec2) Angle

func (a Vec2) Angle() float64

Angle returns the angle of the vector in radians.

func (Vec2) AngleTo

func (a Vec2) AngleTo(b Vec2) float64

AngleTo returns the angle between two vectors.

func (Vec2) Distance

func (a Vec2) Distance(b Vec2) float64

Distance returns the distance between two points.

func (Vec2) Dot

func (a Vec2) Dot(b Vec2) float64

Dot returns the dot product a · b.

func (Vec2) Len

func (a Vec2) Len() float64

Len returns the length of the vector.

func (Vec2) LenSq

func (a Vec2) LenSq() float64

LenSq returns the squared length (faster, no sqrt).

func (Vec2) Lerp

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

Lerp returns linear interpolation between a and b.

func (Vec2) Mul

func (a Vec2) Mul(b Vec2) Vec2

Mul returns the component-wise product a * b.

func (Vec2) Negate

func (a Vec2) Negate() Vec2

Negate returns the negated vector.

func (Vec2) Normalize

func (a Vec2) Normalize() Vec2

Normalize returns the unit vector.

func (Vec2) Perpendicular

func (a Vec2) Perpendicular() Vec2

Perpendicular returns a perpendicular vector (90° counter-clockwise).

func (Vec2) Rotate

func (a Vec2) Rotate(angle float64) Vec2

Rotate rotates the vector by angle (radians).

func (Vec2) Scale

func (a Vec2) Scale(s float64) Vec2

Scale returns the scalar product a * s.

func (Vec2) Sub

func (a Vec2) Sub(b Vec2) Vec2

Sub returns the vector difference a - b.

type Vec3

type Vec3 struct {
	X, Y, Z float64
}

Vec3 represents a 3D vector.

func Forward

func Forward() Vec3

Forward returns the world forward vector (0, 0, -1).

func Right() Vec3

Right returns the world right vector (1, 0, 0).

func Up

func Up() Vec3

Up returns the world up vector (0, 1, 0).

func V3

func V3(x, y, z float64) Vec3

V3 creates a new Vec3.

func Zero3

func Zero3() Vec3

Zero3 returns the zero vector.

func (Vec3) Abs

func (a Vec3) Abs() Vec3

Abs returns the component-wise absolute value.

func (Vec3) Add

func (a Vec3) Add(b Vec3) Vec3

Add returns the vector sum a + b.

func (Vec3) Ceil

func (a Vec3) Ceil() Vec3

Ceil returns the component-wise ceiling.

func (Vec3) Cross

func (a Vec3) Cross(b Vec3) Vec3

Cross returns the cross product a × b.

func (Vec3) Distance

func (a Vec3) Distance(b Vec3) float64

Distance returns the distance between two points.

func (Vec3) Div

func (a Vec3) Div(s float64) Vec3

Div returns the scalar division a / s.

func (Vec3) Dot

func (a Vec3) Dot(b Vec3) float64

Dot returns the dot product a · b.

func (Vec3) Floor

func (a Vec3) Floor() Vec3

Floor returns the component-wise floor.

func (Vec3) Len

func (a Vec3) Len() float64

Len returns the length (magnitude) of the vector.

func (Vec3) LenSq

func (a Vec3) LenSq() float64

LenSq returns the squared length (faster, no sqrt).

func (Vec3) Lerp

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

Lerp returns the linear interpolation between a and b by t.

func (Vec3) Max

func (a Vec3) Max(b Vec3) Vec3

Max returns the component-wise maximum.

func (Vec3) Min

func (a Vec3) Min(b Vec3) Vec3

Min returns the component-wise minimum.

func (Vec3) Mul

func (a Vec3) Mul(b Vec3) Vec3

Mul returns the component-wise product a * b.

func (Vec3) Negate

func (a Vec3) Negate() Vec3

Negate returns the negated vector.

func (Vec3) Normalize

func (a Vec3) Normalize() Vec3

Normalize returns the unit vector in the same direction.

func (Vec3) Reflect

func (a Vec3) Reflect(n Vec3) Vec3

Reflect returns the reflection of a around normal n.

func (Vec3) Scale

func (a Vec3) Scale(s float64) Vec3

Scale returns the scalar product a * s.

func (Vec3) Sub

func (a Vec3) Sub(b Vec3) Vec3

Sub returns the vector difference a - b.

type Vec4

type Vec4 struct {
	X, Y, Z, W float64
}

Vec4 represents a 4D vector (or homogeneous 3D point).

func V4

func V4(x, y, z, w float64) Vec4

V4 creates a new Vec4.

func V4FromV3

func V4FromV3(v Vec3, w float64) Vec4

V4FromV3 creates a Vec4 from Vec3 with specified W.

func (Vec4) Add

func (v Vec4) Add(b Vec4) Vec4

Add returns the vector sum.

func (Vec4) Dot

func (v Vec4) Dot(b Vec4) float64

Dot returns the dot product.

func (Vec4) Len

func (v Vec4) Len() float64

Len returns the length.

func (Vec4) Lerp

func (v Vec4) Lerp(b Vec4, t float64) Vec4

Lerp returns linear interpolation.

func (Vec4) Normalize

func (v Vec4) Normalize() Vec4

Normalize returns the unit vector.

func (Vec4) PerspectiveDivide

func (v Vec4) PerspectiveDivide() Vec3

PerspectiveDivide returns Vec3 after dividing by W.

func (Vec4) Scale

func (v Vec4) Scale(s float64) Vec4

Scale returns the scalar product.

func (Vec4) Sub

func (v Vec4) Sub(b Vec4) Vec4

Sub returns the vector difference.

func (Vec4) Vec3

func (v Vec4) Vec3() Vec3

Vec3 returns the Vec3 portion (ignoring W).

Jump to

Keyboard shortcuts

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