Super Mario 64 Source
A Super Mario 64 decompilation, brought to you by a bunch of clever folks.
Macros | Functions | Variables
math_util.h File Reference

Go to the source code of this file.

Macros

#define sins(x)   gSineTable[(u16) (x) >> 4]
 
#define coss(x)   gCosineTable[(u16) (x) >> 4]
 
#define min(a, b)   ((a) <= (b) ? (a) : (b))
 
#define max(a, b)   ((a) > (b) ? (a) : (b))
 
#define sqr(x)   ((x) * (x))
 

Functions

voidvec3f_copy (Vec3f dest, Vec3f src)
 Copy vector 'src' to 'dest'. More...
 
voidvec3f_set (Vec3f dest, f32 x, f32 y, f32 z)
 Set vector 'dest' to (x, y, z) More...
 
voidvec3f_add (Vec3f dest, Vec3f a)
 Add vector 'a' to 'dest'. More...
 
voidvec3f_sum (Vec3f dest, Vec3f a, Vec3f b)
 Make 'dest' the sum of vectors a and b. More...
 
voidvec3s_copy (Vec3s dest, Vec3s src)
 Copy vector src to dest. More...
 
voidvec3s_set (Vec3s dest, s16 x, s16 y, s16 z)
 Set vector 'dest' to (x, y, z) More...
 
voidvec3s_add (Vec3s dest, Vec3s a)
 Add vector a to 'dest'. More...
 
voidvec3s_sum (Vec3s dest, Vec3s a, Vec3s b)
 Make 'dest' the sum of vectors a and b. More...
 
voidvec3s_sub (Vec3s dest, Vec3s a)
 Subtract vector a from 'dest'. More...
 
voidvec3s_to_vec3f (Vec3f dest, Vec3s a)
 Convert short vector a to float vector 'dest'. More...
 
voidvec3f_to_vec3s (Vec3s dest, Vec3f a)
 Convert float vector a to a short vector 'dest' by rounding the components to the nearest integer. More...
 
voidfind_vector_perpendicular_to_plane (Vec3f dest, Vec3f a, Vec3f b, Vec3f c)
 Set 'dest' the normal vector of a triangle with vertices a, b and c. More...
 
voidvec3f_cross (Vec3f dest, Vec3f a, Vec3f b)
 Make vector 'dest' the cross product of vectors a and b. More...
 
voidvec3f_normalize (Vec3f dest)
 Scale vector 'dest' so it has length 1. More...
 
void mtxf_copy (f32 dest[4][4], f32 src[4][4])
 
void mtxf_identity (f32 mtx[4][4])
 
void mtxf_translate (f32 a[4][4], Vec3f b)
 
void mtxf_lookat (f32 mtx[4][4], Vec3f b, Vec3f c, s16 d)
 
void mtxf_rotate_zxy_and_translate (f32 mtx[4][4], Vec3f b, Vec3s c)
 
void mtxf_rotate_xyz_and_translate (f32 mtx[4][4], Vec3f b, Vec3s c)
 
void mtxf_billboard (f32 mtx1[4][4], f32 mtx2[4][4], Vec3f c, s16 d)
 
void mtxf_align_terrain_normal (f32 mtx[4][4], Vec3f b, Vec3f c, s16 d)
 
void mtxf_align_terrain_triangle (f32 mtx[4][4], Vec3f b, s16 c, f32 d)
 
void mtxf_mul (f32 dest[4][4], f32 a[4][4], f32 b[4][4])
 
void mtxf_scale_vec3f (f32 a[4][4], f32 b[4][4], Vec3f c)
 
void mtxf_mul_vec3s (f32 a[4][4], Vec3s b)
 
void mtxf_to_mtx (Mtx *a, f32 b[4][4])
 
void mtxf_rotate_xy (Mtx *a, s16 b)
 Set 'mtx' to a transformation matrix that rotates around the z axis. More...
 
void get_pos_from_transform_mtx (Vec3f a, f32 b[4][4], f32 c[4][4])
 
void vec3f_get_dist_and_angle (Vec3f a, Vec3f b, f32 *c, s16 *d, s16 *e)
 Take the vector starting at 'from' pointed at 'to' an retrieve the length of that vector, as well as the yaw and pitch angles. More...
 
void vec3f_set_dist_and_angle (Vec3f a, Vec3f b, f32 c, s16 d, s16 e)
 Construct the 'to' point which is distance 'dist' away from the 'from' position, and has the angles pitch and yaw. More...
 
s32 approach_s32 (s32 a, s32 b, s32 c, s32 d)
 Return the value 'current' after it tries to approach target, going up at most 'inc' and going down at most 'dec'. More...
 
f32 approach_f32 (f32 a, f32 b, f32 c, f32 d)
 Return the value 'current' after it tries to approach target, going up at most 'inc' and going down at most 'dec'. More...
 
s16 atan2s (f32 a, f32 b)
 Compute the angle from (0, 0) to (x, y) as a s16. More...
 
f32 atan2f (f32 a, f32 b)
 Compute the atan2 in radians by calling atan2s and converting the result. More...
 
void spline_get_weights (Vec4f a, f32 b, UNUSED s32 c)
 Set 'result' to a 4-vector with weights corresponding to interpolation value t in [0, 1] and gSplineState. More...
 
void anim_spline_init (Vec4s *a)
 Initialize a spline animation. More...
 
s32 anim_spline_poll (Vec3f a)
 Poll the next point from a spline animation. More...
 

Variables

f32 gSineTable []
 
f32 gCosineTable []
 

Macro Definition Documentation

◆ coss

#define coss (   x)    gCosineTable[(u16) (x) >> 4]

◆ max

#define max (   a,
 
)    ((a) > (b) ? (a) : (b))

◆ min

#define min (   a,
 
)    ((a) <= (b) ? (a) : (b))

◆ sins

#define sins (   x)    gSineTable[(u16) (x) >> 4]

◆ sqr

#define sqr (   x)    ((x) * (x))

Function Documentation

◆ anim_spline_init()

void anim_spline_init ( Vec4s keyFrames)

Initialize a spline animation.

'keyframes' should be an array of (s, x, y, z) vectors s: the speed of the keyframe in 1000/frames, e.g. s=100 means the keyframe lasts 10 frames (x, y, z): point in 3D space on the curve The array should end with three entries with s=0 (infinite keyframe duration). That's because the spline has a 3rd degree polynomial, so it looks 3 points ahead.

◆ anim_spline_poll()

s32 anim_spline_poll ( Vec3f  result)

Poll the next point from a spline animation.

anim_spline_init should be called before polling for vectors. Returns TRUE when the last point is reached, FALSE otherwise.

◆ approach_f32()

f32 approach_f32 ( f32  a,
f32  b,
f32  c,
f32  d 
)

Return the value 'current' after it tries to approach target, going up at most 'inc' and going down at most 'dec'.

◆ approach_s32()

s32 approach_s32 ( s32  a,
s32  b,
s32  c,
s32  d 
)

Return the value 'current' after it tries to approach target, going up at most 'inc' and going down at most 'dec'.

If target is close to the max or min s32, then it's possible to overflow

◆ atan2f()

f32 atan2f ( f32  a,
f32  b 
)

Compute the atan2 in radians by calling atan2s and converting the result.

◆ atan2s()

s16 atan2s ( f32  y,
f32  x 
)

Compute the angle from (0, 0) to (x, y) as a s16.

Given that terrain is in the xz-plane, this is commonly called with (z, x) to get a yaw angle.

◆ find_vector_perpendicular_to_plane()

void* find_vector_perpendicular_to_plane ( Vec3f  dest,
Vec3f  a,
Vec3f  b,
Vec3f  c 
)

Set 'dest' the normal vector of a triangle with vertices a, b and c.

It is similar to vec3f_cross, but it calculates the vectors (c-b) and (b-a) at the same time.

warning: function returns address of local variable

◆ get_pos_from_transform_mtx()

void get_pos_from_transform_mtx ( Vec3f  a,
f32  b[4][4],
f32  c[4][4] 
)

◆ mtxf_align_terrain_normal()

void mtxf_align_terrain_normal ( f32  mtx[4][4],
Vec3f  b,
Vec3f  c,
s16  d 
)

◆ mtxf_align_terrain_triangle()

void mtxf_align_terrain_triangle ( f32  mtx[4][4],
Vec3f  b,
s16  c,
f32  d 
)

◆ mtxf_billboard()

void mtxf_billboard ( f32  mtx1[4][4],
f32  mtx2[4][4],
Vec3f  c,
s16  d 
)

◆ mtxf_copy()

void mtxf_copy ( f32  dest[4][4],
f32  src[4][4] 
)

◆ mtxf_identity()

void mtxf_identity ( f32  mtx[4][4])

◆ mtxf_lookat()

void mtxf_lookat ( f32  mtx[4][4],
Vec3f  b,
Vec3f  c,
s16  d 
)

◆ mtxf_mul()

void mtxf_mul ( f32  dest[4][4],
f32  a[4][4],
f32  b[4][4] 
)

◆ mtxf_mul_vec3s()

void mtxf_mul_vec3s ( f32  a[4][4],
Vec3s  b 
)

◆ mtxf_rotate_xy()

void mtxf_rotate_xy ( Mtx a,
s16  b 
)

Set 'mtx' to a transformation matrix that rotates around the z axis.

◆ mtxf_rotate_xyz_and_translate()

void mtxf_rotate_xyz_and_translate ( f32  mtx[4][4],
Vec3f  b,
Vec3s  c 
)

◆ mtxf_rotate_zxy_and_translate()

void mtxf_rotate_zxy_and_translate ( f32  mtx[4][4],
Vec3f  b,
Vec3s  c 
)

◆ mtxf_scale_vec3f()

void mtxf_scale_vec3f ( f32  a[4][4],
f32  b[4][4],
Vec3f  c 
)

◆ mtxf_to_mtx()

void mtxf_to_mtx ( Mtx a,
f32  b[4][4] 
)

◆ mtxf_translate()

void mtxf_translate ( f32  a[4][4],
Vec3f  b 
)

◆ spline_get_weights()

void spline_get_weights ( Vec4f  result,
f32  t,
UNUSED s32  c 
)

Set 'result' to a 4-vector with weights corresponding to interpolation value t in [0, 1] and gSplineState.

Given the current control point P, these weights are for P[0], P[1], P[2] and P[3] to obtain an interpolated point. The weights naturally sum to 1, and they are also always in range [0, 1] so the inteprolated point will never overshoot. The curve is guaranteed to go through the first and last point, but not through intermediate points.

gSplineState ensures that the curve is clamped: the first two points and last two points have different weight formulas. These are the weights just before gSplineState transitions: 1: [1, 0, 0, 0] 1->2: [0, 3/12, 7/12, 2/12] 2->3: [0, 1/6, 4/6, 1/6] 3->3: [0, 1/6, 4/6, 1/6] (repeats) 3->4: [0, 1/6, 4/6, 1/6] 4->5: [0, 2/12, 7/12, 3/12] 5: [0, 0, 0, 1]

I suspect that the weight formulas will give a 3rd degree B-spline with the common uniform clamped knot vector, e.g. for n points: [0, 0, 0, 0, 1, 2, ... n-1, n, n, n, n] TODO: verify the classification of the spline / figure out how polynomials were computed

◆ vec3f_add()

void* vec3f_add ( Vec3f  dest,
Vec3f  a 
)

Add vector 'a' to 'dest'.

warning: function returns address of local variable

◆ vec3f_copy()

void* vec3f_copy ( Vec3f  dest,
Vec3f  src 
)

Copy vector 'src' to 'dest'.

warning: function returns address of local variable

◆ vec3f_cross()

void* vec3f_cross ( Vec3f  dest,
Vec3f  a,
Vec3f  b 
)

Make vector 'dest' the cross product of vectors a and b.

warning: function returns address of local variable

◆ vec3f_get_dist_and_angle()

void vec3f_get_dist_and_angle ( Vec3f  a,
Vec3f  b,
f32 c,
s16 d,
s16 e 
)

Take the vector starting at 'from' pointed at 'to' an retrieve the length of that vector, as well as the yaw and pitch angles.

◆ vec3f_normalize()

void* vec3f_normalize ( Vec3f  dest)

Scale vector 'dest' so it has length 1.

Possible division by zero

warning: function returns address of local variable

◆ vec3f_set()

void* vec3f_set ( Vec3f  dest,
f32  x,
f32  y,
f32  z 
)

Set vector 'dest' to (x, y, z)

warning: function returns address of local variable

◆ vec3f_set_dist_and_angle()

void vec3f_set_dist_and_angle ( Vec3f  a,
Vec3f  b,
f32  c,
s16  d,
s16  e 
)

Construct the 'to' point which is distance 'dist' away from the 'from' position, and has the angles pitch and yaw.

◆ vec3f_sum()

void* vec3f_sum ( Vec3f  dest,
Vec3f  a,
Vec3f  b 
)

Make 'dest' the sum of vectors a and b.

warning: function returns address of local variable

◆ vec3f_to_vec3s()

void* vec3f_to_vec3s ( Vec3s  dest,
Vec3f  a 
)

Convert float vector a to a short vector 'dest' by rounding the components to the nearest integer.

warning: function returns address of local variable

◆ vec3s_add()

void* vec3s_add ( Vec3s  dest,
Vec3s  a 
)

Add vector a to 'dest'.

warning: function returns address of local variable

◆ vec3s_copy()

void* vec3s_copy ( Vec3s  dest,
Vec3s  src 
)

Copy vector src to dest.

warning: function returns address of local variable

◆ vec3s_set()

void* vec3s_set ( Vec3s  dest,
s16  x,
s16  y,
s16  z 
)

Set vector 'dest' to (x, y, z)

warning: function returns address of local variable

◆ vec3s_sub()

void* vec3s_sub ( Vec3s  dest,
Vec3s  a 
)

Subtract vector a from 'dest'.

warning: function returns address of local variable

◆ vec3s_sum()

void* vec3s_sum ( Vec3s  dest,
Vec3s  a,
Vec3s  b 
)

Make 'dest' the sum of vectors a and b.

warning: function returns address of local variable

◆ vec3s_to_vec3f()

void* vec3s_to_vec3f ( Vec3f  dest,
Vec3s  a 
)

Convert short vector a to float vector 'dest'.

warning: function returns address of local variable

Variable Documentation

◆ gCosineTable

f32 gCosineTable[]

◆ gSineTable

f32 gSineTable[]