Documentation ¶
Overview ¶
Package lin provides a linear math library that includes vectors, matrices, quaternions, transforms and some utility functions. Linear math operations are useful in 3D applications for describing and transforming virtual objects as well as simulating physics.
Package lin is provided as part of the vu (virtual universe) 3D engine.
Index ¶
- Constants
- Variables
- func AbsMax(a0, a1, a2, a3 float64) int
- func Aeq(a, b float64) bool
- func AeqZ(x float64) bool
- func Atan2F(y, x float64) float64
- func Clamp(s, lb, ub float64) float64
- func Deg(rad float64) float64
- func IsNeg(x float64) bool
- func Lerp(a, b, ratio float64) float64
- func Max3(a, b, c float64) float64
- func Min3(a, b, c float64) float64
- func MultSQ(x, y, z float64, q *Q) (vx, vy, vz float64)
- func Nang(radians float64) float64
- func Rad(deg float64) float64
- func Round(val float64, prec int) float64
- type M3
- func (m *M3) Abs(a *M3) *M3
- func (m *M3) Add(a, b *M3) *M3
- func (m *M3) Adj(a *M3) *M3
- func (m *M3) Aeq(a *M3) bool
- func (m *M3) Cof(row, col int) float64
- func (m *M3) Det() float64
- func (m *M3) Eq(a *M3) bool
- func (m *M3) Inv(a *M3) *M3
- func (m *M3) Mult(l, r *M3) *M3
- func (m *M3) MultLtR(lt, r *M3) *M3
- func (m *M3) Scale(s float64) *M3
- func (m *M3) ScaleS(x, y, z float64) *M3
- func (m *M3) ScaleSM(x, y, z float64) *M3
- func (m *M3) ScaleV(v *V3) *M3
- func (m *M3) Set(a *M3) *M3
- func (m *M3) SetAa(ax, ay, az, ang float64) *M3
- func (m *M3) SetM4(a *M4) *M3
- func (m *M3) SetQ(q *Q) *M3
- func (m *M3) SetS(Xx, Xy, Xz, Yx, Yy, Yz, Zx, Zy, Zz float64) *M3
- func (m *M3) SetSkewSym(v *V3) *M3
- func (m *M3) Sub(a, b *M3) *M3
- func (m *M3) Transpose(a *M3) *M3
- type M4
- func (m *M4) Add(a, b *M4) *M4
- func (m *M4) Aeq(a *M4) bool
- func (m *M4) Eq(a *M4) bool
- func (m *M4) Mult(l, r *M4) *M4
- func (m *M4) Ortho(left, right, bottom, top, near, far float64) *M4
- func (m *M4) Persp(fov, aspect, near, far float64) *M4
- func (m *M4) PerspInv(fov, aspect, near, far float64) *M4
- func (m *M4) Scale(s float64) *M4
- func (m *M4) ScaleMS(x, y, z float64) *M4
- func (m *M4) ScaleSM(x, y, z float64) *M4
- func (m *M4) Set(a *M4) *M4
- func (m *M4) SetQ(q *Q) *M4
- func (m *M4) TranslateMT(x, y, z float64) *M4
- func (m *M4) TranslateTM(x, y, z float64) *M4
- func (m *M4) Transpose(a *M4) *M4
- type Q
- func (q *Q) Aa() (ax, ay, az, angle float64)
- func (q *Q) Add(r, s *Q) *Q
- func (q *Q) Aeq(r *Q) bool
- func (q *Q) Ang(r *Q) float64
- func (q *Q) Div(s float64) *Q
- func (q *Q) Dot(r *Q) float64
- func (q *Q) Eq(r *Q) bool
- func (q *Q) GetS() (x, y, z, w float64)
- func (q *Q) Inv(r *Q) *Q
- func (q *Q) Len() float64
- func (q *Q) Mult(r, s *Q) *Q
- func (q *Q) MultQV(r *Q, v *V3) *Q
- func (q *Q) MultT(t *T) *Q
- func (q *Q) Neg() *Q
- func (q *Q) Nlerp(r, s *Q, ratio float64) *Q
- func (q *Q) Scale(s float64) *Q
- func (q *Q) Set(r *Q) *Q
- func (q *Q) SetAa(ax, ay, az, angle float64) *Q
- func (q *Q) SetM(m *M3) *Q
- func (q *Q) SetS(x, y, z, w float64) *Q
- func (q *Q) SetT(t *T) *Q
- func (q *Q) Sub(r, s *Q) *Q
- func (q *Q) Unit() *Q
- type T
- func (t *T) Aeq(a *T) bool
- func (t *T) App(v *V3) *V3
- func (t *T) AppR(x, y, z float64) (vx, vy, vz float64)
- func (t *T) AppS(x, y, z float64) (vx, vy, vz float64)
- func (t *T) Eq(a *T) bool
- func (t *T) Integrate(a *T, linv, angv *V3, dt float64) *T
- func (t *T) Inv(v *V3) *V3
- func (t *T) InvS(x, y, z float64) (vx, vy, vz float64)
- func (t *T) Mult(a, b *T) *T
- func (t *T) Set(a *T) *T
- func (t *T) SetAa(ax, ay, az, ang float64) *T
- func (t *T) SetI() *T
- func (t *T) SetLoc(lx, ly, lz float64) *T
- func (t *T) SetRot(x, y, z, w float64) *T
- func (t *T) SetVQ(loc *V3, rot *Q) *T
- type V3
- func (v *V3) Abs() *V3
- func (v *V3) Add(a, b *V3) *V3
- func (v *V3) Aeq(a *V3) bool
- func (v *V3) AeqZ() bool
- func (v *V3) Ang(a *V3) float64
- func (v *V3) AppT(t *T, a *V3) *V3
- func (v *V3) Cross(a, b *V3) *V3
- func (v *V3) Dist(a *V3) float64
- func (v *V3) DistSqr(a *V3) float64
- func (v *V3) Div(s float64) *V3
- func (v *V3) Dot(a *V3) float64
- func (v *V3) Eq(a *V3) bool
- func (v *V3) GetS() (x, y, z float64)
- func (v *V3) Len() float64
- func (v *V3) LenSqr() float64
- func (v *V3) Lerp(a, b *V3, fraction float64) *V3
- func (v *V3) Max(a, b *V3) *V3
- func (v *V3) Min(a, b *V3) *V3
- func (v *V3) Mult(a, b *V3) *V3
- func (v *V3) MultMv(m *M3, cv *V3) *V3
- func (v *V3) MultQ(a *V3, q *Q) *V3
- func (v *V3) MultvM(rv *V3, m *M3) *V3
- func (v *V3) MultvQ(a *V3, q *Q) *V3
- func (v *V3) Neg(a *V3) *V3
- func (v *V3) Nlerp(a, b *V3, ratio float64) *V3
- func (v *V3) Plane(p, q *V3)
- func (v *V3) Scale(a *V3, s float64) *V3
- func (v *V3) Set(a *V3) *V3
- func (v *V3) SetS(x, y, z float64) *V3
- func (v *V3) Sub(a, b *V3) *V3
- func (v *V3) Swap(a *V3) *V3
- func (v *V3) Unit() *V3
- type V4
- func (v *V4) Abs() *V4
- func (v *V4) Add(a, b *V4) *V4
- func (v *V4) AeqZ() bool
- func (v *V4) Div(s float64) *V4
- func (v *V4) Dot(a *V4) float64
- func (v *V4) Eq(a *V4) bool
- func (v *V4) GetS() (x, y, z, w float64)
- func (v *V4) Len() float64
- func (v *V4) LenSqr() float64
- func (v *V4) Lerp(a, b *V4, ratio float64) *V4
- func (v *V4) Max(a, b *V4) *V4
- func (v *V4) Min(a, b *V4) *V4
- func (v *V4) Mult(a, b *V4) *V4
- func (v *V4) MultMv(m *M4, cv *V4) *V4
- func (v *V4) MultvM(rv *V4, m *M4) *V4
- func (v *V4) Neg(a *V4) *V4
- func (v *V4) Nlerp(a, b *V4, ratio float64) *V4
- func (v *V4) Scale(a *V4, s float64) *V4
- func (v *V4) Set(a *V4) *V4
- func (v *V4) SetS(x, y, z, w float64) *V4
- func (v *V4) Sub(a, b *V4) *V4
- func (v *V4) Swap(a *V4) *V4
- func (v *V4) Unit() *V4
Constants ¶
const ( // PI and its commonly needed varients. PI float64 = math.Pi PIx2 float64 = PI * 2 HALF_PI float64 = PIx2 * 0.25 DEG_RAD float64 = PIx2 / 360.0 // X degrees * DEG_RAD = Y radians RAD_DEG float64 = 360.0 / PIx2 // Y radians * RAD_DEG = X degrees // Convenience numbers. LARGE float64 = math.MaxFloat32 SQRT2 float64 = math.Sqrt2 SQRT3 float64 = 1.73205 // EPSILON is used to distinguish when a float is close enough to a number. // Wikipedia: "In set theory epsilon is the limit ordinal of the sequence..." EPSILON float64 = 0.000001 )
Various linear math constants.
Variables ¶
var M3I = &M3{
1, 0, 0,
0, 1, 0,
0, 0, 1}
M3I provides a reference identity matrix that can be used in calculations. It should never be changed.
var M3Z = &M3{
0, 0, 0,
0, 0, 0,
0, 0, 0}
M3Z provides a reference zero matrix that can be used in calculations. It should never be changed.
var M4I = &M4{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1}
M4I provides a reference identity matrix that can be used in calculations. It should never be changed.
var M4Z = &M4{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}
M4Z provides a reference zero matrix that can be used in calculations. It should never be changed.
var QI = &Q{0, 0, 0, 1}
QI provides a reference identity matrix that can be used in calculations. It should never be changed.
Functions ¶
func AbsMax ¶
AbsMax returns the index of the largest absolute value of the 4 given values. The returned index is always from 0-3.
func Aeq ¶
Aeq (~=) almost-equals returns true if the difference between a and b is so small that it doesn't matter.
func AeqZ ¶
AeqZ (~=) almost-equals returns true if the difference between x and zero is so small that it doesn't matter.
func Atan2F ¶
Atan2F is a fast approximation of Atan2. Source was posted at:
http://www.gamedev.net/topic/441464-manually-implementing-atan2-or-atan/
func Clamp ¶
Clamp returns a scalar value (one of: s, lb, ub) guaranteed to be within the range given by lower bound lb and upper bound ub.
func MultSQ ¶
MultSQ applies rotation q to scalar vector (x,y,z) The updated scalar vector (vx,vy,vz) is returned.
Types ¶
type M3 ¶
type M3 struct {
Xx, Xy, Xz float64 // indices 0, 1, 2 [00, 01, 02] X-Axis
Yx, Yy, Yz float64 // indices 3, 4, 5 [10, 11, 12] Y-Axis
Zx, Zy, Zz float64 // indices 6, 7, 8 [20, 21, 22] Z-Axis
}
M3 is a 3x3 matrix where the matrix elements are individually addressable.
func NewM3I ¶
func NewM3I() *M3
NewM3I creates a new 3x3 identity matrix.
[ 1 0 0 ] [ Xx Xy Xz ] [ 0 1 0 ] => [ Yx Yy Yz ] [ 0 0 1 ] [ Zx Zy Zz ]
func (*M3) Abs ¶
Abs updates m to be the the absolute (non-negative) element values of the corresponding element values in matrix a. The source matrix a is unchanged. The updated matrix m is returned.
func (*M3) Add ¶
Add (+) adds matrices a and b storing the results in m. Each element of matrix b is added to the corresponding matrix a element. It is safe to use the calling matrix m as one or both of the parameters. For example the plus.equals operation (+=) is
m.Add(m, b)
The updated matrix m is returned.
func (*M3) Adj ¶
Adj updates m to be the adjoint matrix of matrix a. The adjoint matrix is created by the transpose of the cofactor matrix of the original matrix.
[ a.cof(0,0) a.cof(1,0) a.cof(2,0) ] [ mXx mXy mXz ] [ a.cof(0,1) a.cof(1,1) a.cof(2,1) ] => [ mYx mYy mYz ] [ a.cof(0,2) a.cof(1,2) a.cof(2,2) ] [ mZx mZy mZz ]
The updated matrix m is returned.
func (*M3) Aeq ¶
Aeq (~=) almost equals returns true if all the elements in matrix m have essentially the same value as the corresponding elements in matrix a. Used where equals is unlikely to return true due to float precision.
func (*M3) Cof ¶
Cof returns one of the possible cofactors of a 3x3 matrix given the input minor (the row and column removed from the calculation). Wikipedia states:
"cofactors [...] are useful for computing both the determinant and inverse of square matrices".
func (*M3) Det ¶
Det returns the determinant of matrix m. Determinants are helpful when calculating the inverse of transform matrices. Wikipedia states:
"The determinant provides important information about [..] a matrix that corresponds to a linear transformation of a vector space [..] the transformation has an inverse operation exactly when the determinant is nonzero."
func (*M3) Eq ¶
Eq (==) returns true if all the elements in matrix m have the same value as the corresponding elements in matrix a.
func (*M3) Inv ¶
Inv updates m to be the inverse of matrix a. The updated matrix m is returned. Matrix m is not updated if the matrix has no inverse.
func (*M3) Mult ¶
Mult (*) multiplies matrices l and r storing the results in m.
[ lXx lXy lXz ] [ rXx rXy rXz ] [ mXx mXy mXz ] [ lYx lYy lYz ]x[ rYx rYy rYz ] => [ mYx mYy mYz ] [ lZx lZy lZz ] [ rZx rZy rZz ] [ mZx mZy mZz ]
It is safe to use the calling matrix m as one or both of the parameters. For example (*=) is
m.Mult(m, r)
The updated matrix m is returned.
func (*M3) MultLtR ¶
MultLtR multiplies the transpose of matrix l on left of matrix r and stores the result in m. This can be used for saving a method call when calculating inverse transforms.
[ lXx lYx lZx ] [ rXx rXy rXz ] [ mXx mXy mXz ] [ lXy lYy lZy ]x[ rYx rYy rYz ] => [ mYx mYy mYz ] [ lXz lYz lZz ] [ rZx rZy rZz ] [ mZx mZy mZz ]
It is safe to use the calling matrix m as one or both of the parameters. The updated matrix m is returned.
func (*M3) Scale ¶
Scale (*) each element of matrix m by the given scalar. The updated matrix m is returned.
func (*M3) ScaleS ¶
ScaleS (*) scales each column of matrix m using the corresponding scaler elements x, y, z. The updated matrix m is returned.
func (*M3) ScaleSM ¶
ScaleSM updates m to be the multiplication of a scale matrix created from x, y, z and itself. The updated matrix m is returned so that it may be immediately used in another operation.
[ x 0 0 ] [ mXx mXy mXz ] [ mXx' mXy' mXz' ] [ 0 y 0 ] x [ mYx mYy mYz ] => [ mYx' mYy' mYz' ] [ 0 0 z ] [ mZx mZy mZz ] [ mZx' mZy' mZz' ]
Be sure to pick the correct scale (SM or MS) when doing transforms.
func (*M3) ScaleV ¶
ScaleV (*) scales each column of matrix m using the given vector v for elements for x, y, z. The updated matrix m is returned.
func (*M3) Set ¶
Set (=) assigns all the scaler values from matrix a to the corresponding scaler values in matrix m. The source matrix a is unchanged. The updated matrix m is returned.
func (*M3) SetAa ¶
SetAa, set axis-angle, updates m to be a rotation matrix from the given axis (ax, ay, az) and angle (in radians). See:
http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle http://web.archive.org/web/20041029003853/... ...http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q38 (*note column order)
The updated matrix m is returned.
func (*M3) SetM4 ¶
SetM4 (=) updates calling matrix m to be the 3x3 matrix from the top left corner of the given 4x4 matrix m4. The source matrix a is unchanged. The updated matrix m is returned.
[ Xx Xy Xz Xw ] [ Xx Xy Xz ] [ Yx Yy Yz Yw ] => [ Yx Yy Yz ] [ Zx Zy Zz Zw ] [ Zx Zy Zz ] [ Wx Wy Wz Ww ]
func (*M3) SetQ ¶
SetQ converts a quaternion rotation representation to a matrix rotation representation. SetQ updates matrix m to be the rotation matrix representing the rotation described by unit-quaternion q.
[ mXx mXy mXz ] [ qx qy qz qw ] => [ mYx mYy mYz ] [ mZx mZy mZz ]
The parameter q is unchanged. The updated matrix m is returned.
func (*M3) SetS ¶
SetS (=) explicitly sets the matrix scaler values using the given scalers. The source matrix a is unchanged. The updated matrix m is returned.
Xx, Xy, Xz is the X Axis. Yx, Yy, Yz is the Y Axis. Zx, Zy, Zz is the Z Axis.
func (*M3) SetSkewSym ¶
SetSkewSym sets the matrix m to be a skew-symetric matrix based on the elements of vector v. Wikipedia states:
"A skew-symmetric matrix is a square matrix whose transpose is also its negative."
func (*M3) Sub ¶
Sub (-) subtracts matrices b from a storing the results in m. Each element of matrix b is subtracted from the corresponding matrix a element. It is safe to use the calling matrix m as one or both of the parameters. For example the minus.equals operation (-=) is
m.Sub(m, b)
The updated matrix m is returned.
func (*M3) Transpose ¶
Transpose updates m to be the reflection of matrix a over its diagonal. This essentially changes row-major order to column-major order or vice-versa.
[ Xx Xy Xz ] [ Xx Yx Zx ] [ Yx Yy Yz ] => [ Xy Yy Zy ] [ Zx Zy Zz ] [ Xz Yz Zz ]
The input matrix a is not changed. Matrix m may be used as the input parameter. The updated matrix m is returned.
type M4 ¶
type M4 struct {
Xx, Xy, Xz, Xw float64 // indices 0, 1, 2, 3 [00, 01, 02, 03] X-Axis
Yx, Yy, Yz, Yw float64 // indices 4, 5, 6, 7 [10, 11, 12, 13] Y-Axis
Zx, Zy, Zz, Zw float64 // indices 8, 9, a, b [20, 21, 22, 23] Z-Axis
Wx, Wy, Wz, Ww float64 // indices c, d, e, f [30, 31, 32, 33]
}
M4 is a 4x4 matrix where the matrix elements are individually addressable.
func NewM4I ¶
func NewM4I() *M4
NewM4I creates a new 4x4 identity matrix.
[ 1 0 0 0 ] [ Xx Xy Xz Xw ] [ 0 1 0 0 ] => [ Yx Yy Yz Yw ] [ 0 0 1 0 ] [ Zx Zy Zz Zw ] [ 0 0 0 1 ] [ Wx Wy Wz Ww ]
func (*M4) Aeq ¶
Aeq (~=) almost equals returns true if all the elements in matrix m have essentially the same value as the corresponding elements in matrix a. Same as M3.Aeq().
func (*M4) Eq ¶
Eq (==) returns true if all the elements in matrix m have the same value as the corresponding elements in matrix a.
func (*M4) Mult ¶
Mult updates matrix m to be the multiplication of input matrices l, r.
[ lXx lXy lXz lXw ] [ rXx rXy rXz rXw ] [ mXx mXy mXz mXw ] [ lYx lYy lYz lYw ]x[ rYx rYy rYz rYw ] => [ mYx mYy mYz mYw ] [ lZx lZy lZz lZw ] [ rZx rZy rZz rZw ] [ mZx mZy mZz mZw ] [ lWx lWy lWz lWw ] [ rWx rWy rWz rWw ] [ mWx mWy mWz mWw ]
Same behaviour as M3.Mult()
func (*M4) Ortho ¶
Ortho sets matrix m with projection values needed to transform a 3 dimensional model to a 2 dimensional plane. Orthographic projection ignores depth. The input arguments are:
left, right: Vertical clipping planes. bottom, top: Horizontal clipping planes. near, far : Depth clipping planes. The depth values are negative if the plane is to be behind the viewer
An orthographic matrix fills the following matrix locations:
[ a 0 0 0 ] [ Xx Xy Xz Xw ] [ 0 b 0 0 ] => [ Yx Yy Yz Yw ] [ 0 0 c 0 ] [ Zx Zy Zz Zw ] [ d e f 1 ] [ Wx Wy Wz Ww ]
func (*M4) Persp ¶
Persp sets matrix m with projection values needed to transform a 3 dimensional model to a 2 dimensional plane. Objects that are further away from the viewer will appear smaller. The input arguments are:
fov An amount in degrees indicating how much of the scene is visible. aspect The ratio of height to width of the model. near, far The depth clipping planes. The depth values are negative if the plane is to be behind the viewer
A perspective projection matrix fills the following matrix locations:
[ a 0 0 0 ] [ Xx Xy Xz Xw ] [ 0 b 0 0 ] => [ Yx Yy Yz Yw ] [ 0 0 c d ] [ Zx Zy Zz Zw ] [ 0 0 e 0 ] [ Wx Wy Wz Ww ]
func (*M4) PerspInv ¶
PerspInv sets matrix m to be a new inverse matrix of the given perspective matrix values (see NewPersp()).
[ a' 0 0 0 ] where a' = 1/a d' = 1/e [ Xx Xy Xz Xw ] [ 0 b' 0 0 ] b' = 1/b e' = 1/d => [ Yx Yy Yz Yw ] [ 0 0 0 d'] c' = -(c/de) [ Zx Zy Zz Zw ] [ 0 0 e' c'] [ Wx Wy Wz Ww ]
This is used when going from screen x,y coordinates to 3D coordinates. as in the case when creating a picking ray from a mouse location.
func (*M4) Scale ¶
Scale (*) each element of matrix m by the given scalar. The updated matrix m is returned.
func (*M4) ScaleMS ¶
ScaleMS updates m to be the multiplication of m and a scale matrix created from x, y, z. The updated matrix m is returned so that it may be immediately used in another operation.
[ mXx mXy mXz mXw ] [ x 0 0 0 ] [ mXx' mXy' mXz' mXw ] [ mYx mYy mYz mYw ] x [ 0 y 0 0 ] => [ mYx' mYy' mYz' mYw ] [ mZx mZy mZz mZw ] [ 0 0 z 0 ] [ mZx' mZy' mZz' mZw ] [ mWx mWy mWz mWw ] [ 0 0 0 1 ] [ mWx' mWy' mWz' mWw ]
Be sure to pick the correct scale (SM or MS) when doing transforms.
func (*M4) ScaleSM ¶
ScaleSM updates m to be the multiplication of a scale matrix created from x, y, z and itself. Same behaviours as M3.ScaleSM.
[ x 0 0 0 ] [ mXx mXy mXz mXw ] [ mXx' mXy' mXz' mXw' ] [ 0 y 0 0 ] x [ mYx mYy mYz mYw ] => [ mYx' mYy' mYz' mYw' ] [ 0 0 z 0 ] [ mZx mZy mZz mZw ] [ mZx' mZy' mZz' mZw' ] [ 0 0 0 1 ] [ mWx mWy mWz mWw ] [ mWx mWy mWz mWw ]
func (*M4) Set ¶
Set (=) assigns all the elements values from matrix a to the corresponding element values in matrix m. The source matrix a is unchanged. The updated matrix m is returned.
func (*M4) SetQ ¶
SetQ converts a quaternion rotation representation to a matrix rotation representation. SetQ updates matrix m to be the rotation matrix representing the rotation described by unit-quaternion q.
[ mXx mXy mXz 0 ] [ qx qy qz qw ] => [ mYx mYy mYz 0 ] [ mZx mZy mZz 0 ] [ 0 0 0 1 ]
The parameter q is unchanged. The updated matrix m is returned.
func (*M4) TranslateMT ¶
TranslateMT updates m to be the multiplication of itself and a translation matrix created from x, y, z. The updated matrix m is returned.
[ mXx mXy mXz mXw ] [ 1 0 0 0 ] [ mXx' mXy' mXz' mXw ] [ mYx mYy mYz mYw ] x [ 0 1 0 0 ] => [ mYx' mYy' mYz' mYw ] [ mZx mZy mZz mZw ] [ 0 0 1 0 ] [ mZx' mZy' mZz' mZw ] [ mWx mWy mWz mWw ] [ x y z 1 ] [ mWx' mWy' mWz' mWw ]
Be sure to pick the correct translate (TM or MT) when doing transforms.
func (*M4) TranslateTM ¶
TranslateTM updates m to be the multiplication of a translation matrix T created from x, y, z, and itself. The updated matrix m is returned.
[ 1 0 0 0 ] [ mXx mXy mXz mXw ] [ mXx mXy mXz mXw ] [ 0 1 0 0 ] x [ mYx mYy mYz mYw ] => [ mYx mYy mYz mYw ] [ 0 0 1 0 ] [ mZx mZy mZz mZw ] [ mZx mZy mZz mZw ] [ x y z 1 ] [ mWx mWy mWz mWw ] [ mWx' mWy' mWz' mWw' ]
Be sure to pick the correct translate (TM or MT) when doing transforms.
type Q ¶
type Q struct { X float64 // X component of direction vector. Y float64 // Y component of direction vector. Z float64 // Z component of direction vector. W float64 // Angle of rotation. }
Unit length quaternions represent an angle of rotation and an direction/orientation and are used to track/manipulate 3D object rotations. Quaternions behave nicely for mathematical operations other than they are not commutative.
func (*Q) Aa ¶
Aa gets the rotation of quaternion q as an axis and angle. The axis (x, y, z) and the angle in radians is returned. The return elements will be zero if the length of the quaternion is 0. See:
http://web.archive.org/web/20041029003853/... ...http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q57
func (*Q) Aeq ¶
Aeq (~=) almost-equals returns true if all the elements in quaternion q have essentially the same value as the corresponding elements in quaternion r. Used where a direct comparison is unlikely to return true due to floats.
func (*Q) Ang ¶
Ang returns the angle in radians between quaternions q and r. See
http://math.stackexchange.com/questions/90081/quaternion-distance
for the formula to calculate angles between quaternions, i.e.:
angle = Acos(2⟨q dot r⟩(q dot r)−1)
func (*Q) Div ¶
Div (/= inverse-scale) divides each element in q by the given scalar value The updated q is returned. Scale values of zero are logged as an error and q is not scaled.
func (*Q) Dot ¶
Dot returns the dot product of the quaternions q and r. Quaternion q may be used as the input parameter. For example (Dot=), the length squared, is
q.Dot(q)
The updated calling quaternion q is returned.
func (*Q) Eq ¶
Eq (==) returns true if each element in the quaternion q has the same value as the corresponding element in quaterion r.
func (*Q) Inv ¶
Inv updates q to be inverse of quaternion r. The updated q is returned. The inverse of a quaternion is the same as the conjugate, as long as the quaternion is unit-length.
func (*Q) Mult ¶
Mult (*) multiplies quaternions r and s returning the result in q. This applies the rotation of s to r giving q, leaving r and s unchanged. It is safe to use the calling quaternion q as one or both of the parameters. For example (*=) is
q.Mult(q, s)
The updated calling quaternion q is returned.
func (*Q) MultQV ¶
MultQV multiplies quaternion r and vector v and returns the result in quaternion q. The upated quaternion q is returned.
func (*Q) MultT ¶
MultT applies the rotation in transform t to the quaternion q. The updated quaternion q is returned.
func (*Q) Neg ¶
Neg (-) returns the negative of quaternion q where each element is negated. The updated q is returned.
func (*Q) Nlerp ¶
Nlerp updates q to be the normalized linear interpolation between quaternions r and s where ratio is expected to be between 0 and 1. The input quaternions r and s are not changed. See:
http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/ http://number-none.com/product/Understanding Slerp, Then Not Using It/
The updated calling quaternion q is returned.
func (*Q) Set ¶
Set (=) assigns all the elements values from quaternion r to the corresponding element values in quaternion q. The updated quaternion q is returned.
func (*Q) SetAa ¶
SetAa, set axis-angle, updates q to have the rotation of the given axis (ax, ay, az) and angle (in radians). See:
http://web.archive.org/web/20041029003853/... ...http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
The updated quaternion q is returned. The quaternion q is not updated if the axis length is 0.
func (*Q) SetM ¶
SetM updates quaternion q to be the rotation of matrix m. See
http://www.flipcode.com/documents/matrfaq.html#Q55 http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
The updated q is returned.
func (*Q) SetS ¶
SetS (=) explicitly sets each of the quaternion values to the given values. The updated quaternion q is returned.
func (*Q) SetT ¶
SetT updates quaternion q to have the rotation in transform t. The updated quaternion q is returned.
type T ¶
type T struct { Loc *V3 // Location (translation, origin). Rot *Q // Rotation (direction, orientation). }
T is a 3D transform for rotation and translation. It excludes scaling and shear information. T is used as a simplification and optimization instead of keeping all transform information in a 4x4 matrix.
T supports linear algebra operations that are similar to those supported by V3, V4, M3, M4, and Q. The main ones are:
Multiply two transforms together to produce a composite transform. Apply a transform or inverse transform to a vector.
func (*T) Aeq ¶
Aeq (~=) almost-equals returns true if all the elements in transform t have essentially the same value as the corresponding elements in transform a. Used where a direct comparison is unlikely to return true due to floats.
func (*T) AppR ¶
AppR applies just the transform rotation to input vector (x,y,z) returning the rotated vector (vx,vy,vz)
func (*T) AppS ¶
AppS applies transform t, rotation then translation, to input scalar vector (x,y,z) returning the transformed scalar vector (vx,vy,vz).
func (*T) Eq ¶
Equals (==) returns true of all elements of transform t have the same value as the corresponding element of transform a.
func (*T) Integrate ¶
Integrate updates transform t to be the linear integration of transform a with the given linear velocity linv, and angular velocity angv over the given amount of time dt. Transforms t and a must be distinct. The input vectors linv, angv are not changed. The updated transform t is returned.
Based on bullet physics: btTransformUtil::integrateTransform.
func (*T) Inv ¶
Inv updates vector v to be the inverse transform t applied to vector a. The updated vector v is returned.
func (*T) InvS ¶
InvS applies the inverse transform t, inverse translation, then inverse rotation, to input vector (x,y,z) returning the transformed vector (vx,vy,vz).
func (*T) Mult ¶
Mult (*) updates the transform t to be the product of the transforms a and b. Transform t may be used as one or both of the input transforms. The updated transform t is returned.
func (*T) Set ¶
Set (=, copy, clone) assigns all the elements values from transform a to the corresponding element values in transform t. The updated transform t is returned.
func (*T) SetAa ¶
SetAa updates transform t to have the rotation specified by the given axis and angle in radians. The updated transform t is returned.
func (*T) SetI ¶
SetI updates transform t to be the identity transform. The updated transform t is returned.
func (*T) SetLoc ¶
SetLoc updates transform t to have the location speccified by lx, ly, lz. The updated transform t is returned.
type V3 ¶
type V3 struct { X float64 // increments as X moves to the right. Y float64 // increments as Y moves up from bottom left. Z float64 // increments as Z moves out of the screen (right handed view space). }
V3 is a 3 element vector. This can also be used as a point.
func (*V3) Abs ¶
Abs updates vector v to have the absolute value of the elements of vector a. The updated vector v is returned.
func (*V3) Add ¶
Add (+) adds vectors a and b storing the results of the addition in v. Vector v may be used as one or both of the parameters. For example (+=) is
v.Add(v, b)
The updated vector v is returned.
func (*V3) Aeq ¶
Aeq (~=) almost-equals returns true if all the elements in vector v have essentially the same value as the corresponding elements in vector a. Used where a direct comparison is unlikely to return true due to floats.
func (*V3) AeqZ ¶
AeqZ (~=) almost equals zero returns true if the square length of the vector is close enough to zero that it makes no difference.
func (*V3) Ang ¶
Ang returns the angle in radians between vector v and input vector a. Ang returns 0 if the magnitude of the two vectors is 0.
func (*V3) AppT ¶
AppT updates vector v to be the transform t applied to vector a. Vector a is unchanged. The updated vector v is returned.
func (*V3) Cross ¶
Cross updates v to be the cross product of vectors a and b. A cross product vector is a vector that is perpendicular to both input vectors. This is only meaningful in 3 (or 7) dimensions. Input vectors a and b are unchanged. Vector v may be used as either input parameter.The updated vector v is returned.
func (*V3) Dist ¶
Dist returns the distance between vector end-points v and a Both vectors (points) v and a are unchanged.
func (*V3) DistSqr ¶
DistSqr returns the distance squared between vector end-points v and a. Both vectors (points) v and a are unchanged.
func (*V3) Div ¶
Div (/= inverse-scale) divides each element in v by the given scalar value. The updated vector v is returned. Vector v is not changed if scalar s is zero.
func (*V3) Dot ¶
Dot vector v with input vector a. Both vectors v and a are unchanged. Wikipedia states:
"This operation can be defined either algebraically or geometrically. Algebraically, it is the sum of the products of the corresponding entries of the two sequences of numbers. Geometrically, it is the product of the magnitudes of the two vectors and the cosine of the angle between them."
func (*V3) Eq ¶
Eq (==) returns true if each element in the vector v has the same value as the corresponding element in vector a.
func (*V3) Len ¶
Len returns the length of vector v. Vector length is the square root of the dot product. The calling vector v is unchanged.
func (*V3) LenSqr ¶
LenSqr returns the length of vector v squared. The calling vector v is unchanged.
func (*V3) Lerp ¶
Lerp updates vector v to be a fraction of the distance (linear interpolation) between the input vectors a and b. The input ratio is not verified, but is expected to be between 0 and 1. Vector v may be used as one of the parameters.
func (*V3) Max ¶
Max updates the vector v elements to be the maxiumum of the corresponding elements from either vectors a or b. The updated vector v is returned.
func (*V3) Min ¶
Min updates the vector v elements to be the minimum of the corresponding elements from either vectors a or b. The updated vector v is returned.
func (*V3) Mult ¶
Mult (*) multiplies the elements of vectors a and b storing the result in v. Vector v may be used as one or both of the parameters. For example (*=) is For example (*=) is
v.Mult(v, b)
The updated vector v is returned.
func (*V3) MultMv ¶
MultMv updates vector v to be the multiplication of matrix m and column vector cv. Vector v may be used as the input vector cv. The udpated vector v is returned.
[ Xx Xy Xz ] [ vx ] [ vx' ] [ Yx Yy Yz ] x [ vy ] = [ vx' ] [ Zx Zy Zz ] [ vz ] [ vz' ]
func (*V3) MultQ ¶
MultQ (*) multiplies a vector by quaternion, effectively applying the rotation of quaternion q to vector a and storing the result in v. The input vector a, and quaternion q are unchanged.
func (*V3) MultvM ¶
MultvM updates vector v to be the multiplication of row vector rv and matrix m. Vector v may be used as the input vector rv. The udpated vector v is returned.
[ Xx Xy Xz ] [ vx vy vz ] x [ Yx Yy Yz ] = [ vx' vy' vz' ] [ Zx Zy Zz ]
func (*V3) Neg ¶
Neg (-) sets vector v to be the negative values of vector a. Vector v may be used as the input parameter. The updated vector v is returned.
func (*V3) Nlerp ¶
Nlerp updates vector v to be a normalized vector that is the linerar interpolation between a and b. See:
http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/ http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
The calling vector v may be used as either or both of the input parameters.
func (*V3) Plane ¶
Plane generates 2 vectors perpendicular to normal vector v. The perpendicular vectors and are returned as values of vectors p and q.
Based on bullet physics: btVector3::btPlaneSpace1
func (*V3) Scale ¶
Scale (*=) updates the elements in vector v by multiplying the corresponding elements in vector a by the given scalar value. Vector v may be used as one or both of the vector parameters. The updated vector v is returned.
func (*V3) Set ¶
Set (=, copy, clone) sets the elements of vector v to have the same values as the elements of vector a. The updated vector v is returned.
func (*V3) SetS ¶
SetS (=) sets the vector elements to the given values. The updated vector v is returned.
func (*V3) Sub ¶
Sub (-) subtracts vectors b from a storing the results of the subtraction in v. Vector v may be used as one or both of the parameters. For example (-=) is
v.Sub(v, b)
The updated vector v is returned.
type V4 ¶
type V4 struct { X float64 // increments as X moves to the right. Y float64 // increments as Y moves up from bottom left. Z float64 // increments as Z moves out of the screen (right handed view space). W float64 // fourth dimension makes for nice 3D matrix math. }
V4 is a 4 element vector. It can be used for points and directions where, as a point it would have W:1, and as a direction it would have W:0.
func (*V4) Abs ¶
Abs updates vector v to have the absolute value of the elements of vector a. Same behaviour as V3.Abs().
func (*V4) Add ¶
Add (+) adds vectors a and b storing the results of the addition in v. Same behaviour as V3.Add().
func (*V4) AeqZ ¶
AeqZ (~=) almost equals zero returns true if the square length of the vector is close enough to zero that it makes no difference.
func (*V4) Div ¶
Div (/= inverse-scale) divides each element in v by the given scalar value. Same behaviour as V3.Div().
func (*V4) Eq ¶
Eq (==) returns true if each element in the vector v has the same value as the corresponding element in vector a.
func (*V4) Lerp ¶
Lerp updates vector v to be a fraction of the distance (linear interpolation) between the input vectors a and b. Same behaviour as V3.Lerp()
func (*V4) Max ¶
Max updates the vector v elements to be the maximum of the corresponding elements from either vectors a or b. Same behaviour as V3.Max().
func (*V4) Min ¶
Min updates the vector v elements to be the minimum of the corresponding elements from either vectors a or b. Same behaviour as V3.Min().
func (*V4) Mult ¶
Mult (*) multiplies the elements of vectors a and b storing the result in v. Same behaviour as V3.Mult().
func (*V4) MultMv ¶
MultMv updates vector v to be the multiplication of matrix m and column vector cv. Same behaviour as V3.MultMv().
[ Xx Xy Xz Xw ] [ vx ] [ vx' ] [ Yx Yy Yz Yw ] x [ vy ] = [ vy' ] [ Zx Zy Zz Zw ] [ vz ] [ vz' ] [ Wx Wy Wz Ww ] [ vw ] [ vw' ]
func (*V4) MultvM ¶
MultvM updates vector v to be the multiplication of row vector rv and matrix m. Same behaviour as V4.MultvM().
[ Xx Xy Xz Xw ] [ vx vy vz vw ] x [ Yx Yy Yz Yw ] = [ vx' vy' vz' vw'] [ Zx Zy Zz Zw ] [ Wx Wy Wz Ww ]
func (*V4) Neg ¶
Neg (-) sets vector v to be the negative values of vector a. Same behaviour as V3.Neg().
func (*V4) Nlerp ¶
Nlerp updates vector v to be a normalized vector that is the linerar interpolation between v1 and v2. Same behaviour as V3.Lerp()
func (*V4) Scale ¶
Scale (*=) updates the elements in vector v by multiplying the corresponding elements in vector a by the given scalar value. Same behaviour as V3.Scale().
func (*V4) Set ¶
Set (=, copy, clone) sets the elements of vector v to have the same values as the elements of vector a. The updated vector v is returned.
func (*V4) SetS ¶
SetS (=) sets the vector elements to the given values. The updated vector v is returned.
func (*V4) Sub ¶
Sub (-) subtracts vectors b from a storing the results of the subtraction in v. Same behaviour as V3.Sub().