diff --git a/data/behavior_data.c b/data/behavior_data.c index 4048f1de..0d537900 100644 --- a/data/behavior_data.c +++ b/data/behavior_data.c @@ -2370,7 +2370,7 @@ const BehaviorScript bhvLllTiltingInvertedPyramid[] = { const BehaviorScript bhvKoopaShell[] = { BEGIN(OBJ_LIST_LEVEL), - OR_LONG(oFlags, (OBJ_FLAG_UPDATE_GFX_POS_AND_ANGLE | OBJ_FLAG_SILHOUETTE)), //! Silhouette doesn't show up in-game + OR_LONG(oFlags, (OBJ_FLAG_UPDATE_GFX_POS_AND_ANGLE | OBJ_FLAG_SILHOUETTE)), //! Silhouette doesn't show up in-game, due to the combine mode SET_OBJ_PHYSICS(/*Wall hitbox radius*/ 30, /*Gravity*/ -400, /*Bounciness*/ -50, /*Drag strength*/ 1000, /*Friction*/ 1000, /*Buoyancy*/ 200, /*Unused*/ 0, 0), BEGIN_LOOP(), CALL_NATIVE(bhv_koopa_shell_loop), diff --git a/src/engine/math_util.c b/src/engine/math_util.c index 37fb64f0..6e6f0c23 100644 --- a/src/engine/math_util.c +++ b/src/engine/math_util.c @@ -22,72 +22,47 @@ int gSplineState; /// Copy vector 'src' to 'dest' void vec3f_copy(Vec3f dest, Vec3f src) { - dest[0] = src[0]; - dest[1] = src[1]; - dest[2] = src[2]; + vec3_copy(dest, src); } /// Set vector 'dest' to (x, y, z) void vec3f_set(Vec3f dest, f32 x, f32 y, f32 z) { - dest[0] = x; - dest[1] = y; - dest[2] = z; + vec3_set(dest, x, y, z); } /// Add vector 'a' to 'dest' void vec3f_add(Vec3f dest, Vec3f a) { - dest[0] += a[0]; - dest[1] += a[1]; - dest[2] += a[2]; + vec3_add(dest, a); } /// Make 'dest' the sum of vectors a and b. void vec3f_sum(Vec3f dest, Vec3f a, Vec3f b) { - dest[0] = a[0] + b[0]; - dest[1] = a[1] + b[1]; - dest[2] = a[2] + b[2]; + vec3_sum(dest, a, b); } /// Copy vector src to dest void vec3s_copy(Vec3s dest, Vec3s src) { - dest[0] = src[0]; - dest[1] = src[1]; - dest[2] = src[2]; + vec3_copy(dest, src); } /// Set vector 'dest' to (x, y, z) void vec3s_set(Vec3s dest, s16 x, s16 y, s16 z) { - dest[0] = x; - dest[1] = y; - dest[2] = z; + vec3_set(dest, x, y, z); } /// Add vector a to 'dest' void vec3s_add(Vec3s dest, Vec3s a) { - dest[0] += a[0]; - dest[1] += a[1]; - dest[2] += a[2]; + vec3_add(dest, a); } /// Make 'dest' the sum of vectors a and b. void vec3s_sum(Vec3s dest, Vec3s a, Vec3s b) { - dest[0] = a[0] + b[0]; - dest[1] = a[1] + b[1]; - dest[2] = a[2] + b[2]; + vec3_sum(dest, a, b); } /// Subtract vector a from 'dest' void vec3s_sub(Vec3s dest, Vec3s a) { - dest[0] -= a[0]; - dest[1] -= a[1]; - dest[2] -= a[2]; -} - -/// Convert short vector a to float vector 'dest' -void vec3s_to_vec3f(Vec3f dest, Vec3s a) { - dest[0] = a[0]; - dest[1] = a[1]; - dest[2] = a[2]; + vec3_sub(dest, a); } /** @@ -114,22 +89,15 @@ void find_vector_perpendicular_to_plane(Vec3f dest, Vec3f a, Vec3f b, Vec3f c) { /// Make vector 'dest' the cross product of vectors a and b. void vec3f_cross(Vec3f dest, Vec3f a, Vec3f b) { - dest[0] = a[1] * b[2] - b[1] * a[2]; - dest[1] = a[2] * b[0] - b[2] * a[0]; - dest[2] = a[0] * b[1] - b[0] * a[1]; + vec3_cross(dest, a, b); } /// Scale vector 'dest' so it has length 1 void vec3f_normalize(Vec3f dest) { f32 invsqrt = sqrtf(sqr(dest[0]) + sqr(dest[1]) + sqr(dest[2])); - if (invsqrt < 0.00001f) return; - invsqrt = 1.0f / invsqrt; - - dest[0] *= invsqrt; - dest[1] *= invsqrt; - dest[2] *= invsqrt; + vec3_mul_val(dest, invsqrt); } /// Copy matrix 'src' to 'dest' @@ -163,9 +131,7 @@ void mtxf_identity(Mat4 mtx) { */ void mtxf_translate(Mat4 dest, Vec3f b) { mtxf_identity(dest); - dest[3][0] = b[0]; - dest[3][1] = b[1]; - dest[3][2] = b[2]; + vec3_copy(dest[3], b); } /** @@ -175,65 +141,47 @@ void mtxf_translate(Mat4 dest, Vec3f b) { * angle allows a bank rotation of the camera. */ void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s16 roll) { - register f32 invLength; - f32 dx, dz; - f32 xColX, xColY, xColZ; - f32 yColX, yColY, yColZ; - f32 zColX, zColY, zColZ; + Vec3f colX, colY, colZ; - dx = to[0] - from[0]; - dz = to[2] - from[2]; + f32 dx = to[0] - from[0]; + f32 dz = to[2] - from[2]; - invLength = -1.0f / MAX(sqrtf(sqr(dx) + sqr(dz)), 0.00001f); + register f32 invLength = -1.0f / MAX(sqrtf(sqr(dx) + sqr(dz)), 0.00001f); dx *= invLength; dz *= invLength; - yColY = coss(roll); - xColY = sins(roll) * dz; - zColY = -sins(roll) * dx; + colY[1] = coss(roll); + colY[0] = sins(roll) * dz; + colY[2] = -sins(roll) * dx; + vec3_diff(colZ, to, from); - xColZ = to[0] - from[0]; - yColZ = to[1] - from[1]; - zColZ = to[2] - from[2]; + invLength = -1.0f / MAX(sqrtf(sqr(colZ[0]) + sqr(colZ[1]) + sqr(colZ[2])), 0.00001f); + vec3_mul_val(colZ, invLength); - invLength = -1.0f / MAX(sqrtf(sqr(xColZ) + sqr(yColZ) + sqr(zColZ)), 0.00001f); - xColZ *= invLength; - yColZ *= invLength; - zColZ *= invLength; + vec3_cross(colX, colY, colZ); - xColX = yColY * zColZ - zColY * yColZ; - yColX = zColY * xColZ - xColY * zColZ; - zColX = xColY * yColZ - yColY * xColZ; + invLength = 1.0f / MAX(sqrtf(sqr(colX[0]) + sqr(colX[1]) + sqr(colX[2])), 0.00001f); + vec3_mul_val(colX, invLength); - invLength = 1.0f / MAX(sqrtf(sqr(xColX) + sqr(yColX) + sqr(zColX)), 0.00001f); + vec3_cross(colY, colZ, colX); - xColX *= invLength; - yColX *= invLength; - zColX *= invLength; + invLength = 1.0f / MAX(sqrtf(sqr(colY[0]) + sqr(colY[1]) + sqr(colY[2])), 0.00001f); + vec3_mul_val(colY, invLength); - xColY = yColZ * zColX - zColZ * yColX; - yColY = zColZ * xColX - xColZ * zColX; - zColY = xColZ * yColX - yColZ * xColX; + mtx[0][0] = colX[0]; + mtx[1][0] = colX[1]; + mtx[2][0] = colX[2]; - invLength = 1.0f / MAX(sqrtf(sqr(xColY) + sqr(yColY) + sqr(zColY)), 0.00001f); - xColY *= invLength; - yColY *= invLength; - zColY *= invLength; + mtx[0][1] = colY[0]; + mtx[1][1] = colY[1]; + mtx[2][1] = colY[2]; - mtx[0][0] = xColX; - mtx[1][0] = yColX; - mtx[2][0] = zColX; - mtx[3][0] = -(from[0] * xColX + from[1] * yColX + from[2] * zColX); - - mtx[0][1] = xColY; - mtx[1][1] = yColY; - mtx[2][1] = zColY; - mtx[3][1] = -(from[0] * xColY + from[1] * yColY + from[2] * zColY); - - mtx[0][2] = xColZ; - mtx[1][2] = yColZ; - mtx[2][2] = zColZ; - mtx[3][2] = -(from[0] * xColZ + from[1] * yColZ + from[2] * zColZ); + mtx[0][2] = colZ[0]; + mtx[1][2] = colZ[1]; + mtx[2][2] = colZ[2]; + mtx[3][0] = -vec3_dot(from, colX); + mtx[3][1] = -vec3_dot(from, colY); + mtx[3][2] = -vec3_dot(from, colZ); mtx[0][3] = 0; mtx[1][3] = 0; @@ -316,8 +264,13 @@ void mtxf_rotate_xyz_and_translate(Mat4 dest, Vec3f b, Vec3s c) { * 'angle' rotates the object while still facing the camera. */ void mtxf_billboard(Mat4 dest, Mat4 mtx, Vec3f position, s16 angle) { - dest[0][0] = coss(angle); - dest[0][1] = sins(angle); + if (angle == 0x0) { + dest[0][0] = 1; + dest[0][1] = 0; + } else { + dest[0][0] = coss(angle); + dest[0][1] = sins(angle); + } dest[0][2] = 0; dest[0][3] = 0; @@ -331,12 +284,9 @@ void mtxf_billboard(Mat4 dest, Mat4 mtx, Vec3f position, s16 angle) { dest[2][2] = 1; dest[2][3] = 0; - dest[3][0] = - mtx[0][0] * position[0] + mtx[1][0] * position[1] + mtx[2][0] * position[2] + mtx[3][0]; - dest[3][1] = - mtx[0][1] * position[0] + mtx[1][1] * position[1] + mtx[2][1] * position[2] + mtx[3][1]; - dest[3][2] = - mtx[0][2] * position[0] + mtx[1][2] * position[1] + mtx[2][2] * position[2] + mtx[3][2]; + dest[3][0] = mtx[0][0] * position[0] + mtx[1][0] * position[1] + mtx[2][0] * position[2] + mtx[3][0]; + dest[3][1] = mtx[0][1] * position[0] + mtx[1][1] * position[1] + mtx[2][1] * position[2] + mtx[3][1]; + dest[3][2] = mtx[0][2] * position[0] + mtx[1][2] * position[1] + mtx[2][2] * position[2] + mtx[3][2]; dest[3][3] = 1; } @@ -352,29 +302,19 @@ void mtxf_align_terrain_normal(Mat4 dest, Vec3f upDir, Vec3f pos, s16 yaw) { Vec3f leftDir; Vec3f forwardDir; - vec3f_set(lateralDir, sins(yaw), 0, coss(yaw)); + vec3_set(lateralDir, sins(yaw), 0, coss(yaw)); vec3f_normalize(upDir); - vec3f_cross(leftDir, upDir, lateralDir); + vec3_cross(leftDir, upDir, lateralDir); vec3f_normalize(leftDir); - vec3f_cross(forwardDir, leftDir, upDir); + vec3_cross(forwardDir, leftDir, upDir); vec3f_normalize(forwardDir); - dest[0][0] = leftDir[0]; - dest[0][1] = leftDir[1]; - dest[0][2] = leftDir[2]; - dest[3][0] = pos[0]; - - dest[1][0] = upDir[0]; - dest[1][1] = upDir[1]; - dest[1][2] = upDir[2]; - dest[3][1] = pos[1]; - - dest[2][0] = forwardDir[0]; - dest[2][1] = forwardDir[1]; - dest[2][2] = forwardDir[2]; - dest[3][2] = pos[2]; + vec3_copy(dest[0], leftDir); + vec3_copy(dest[1], upDir); + vec3_copy(dest[2], forwardDir); + vec3_copy(dest[3], pos); dest[0][3] = 0.0f; dest[1][3] = 0.0f; @@ -391,14 +331,10 @@ void mtxf_align_terrain_normal(Mat4 dest, Vec3f upDir, Vec3f pos, s16 yaw) { * 'radius' is the distance from each triangle vertex to the center */ void mtxf_align_terrain_triangle(Mat4 mtx, Vec3f pos, s16 yaw, f32 radius) { - struct Surface *sp74; - Vec3f point0; - Vec3f point1; - Vec3f point2; + struct Surface *floor; + Vec3f point0, point1, point2; Vec3f forward; - Vec3f xColumn; - Vec3f yColumn; - Vec3f zColumn; + Vec3f xColumn, yColumn, zColumn; f32 avgY; f32 minY = -radius * 3; @@ -409,9 +345,9 @@ void mtxf_align_terrain_triangle(Mat4 mtx, Vec3f pos, s16 yaw, f32 radius) { point2[0] = pos[0] + radius * sins(yaw + 0xD555); point2[2] = pos[2] + radius * coss(yaw + 0xD555); - point0[1] = find_floor(point0[0], pos[1] + 150, point0[2], &sp74); - point1[1] = find_floor(point1[0], pos[1] + 150, point1[2], &sp74); - point2[1] = find_floor(point2[0], pos[1] + 150, point2[2], &sp74); + point0[1] = find_floor(point0[0], pos[1] + 150, point0[2], &floor); + point1[1] = find_floor(point1[0], pos[1] + 150, point1[2], &floor); + point2[1] = find_floor(point2[0], pos[1] + 150, point2[2], &floor); if (point0[1] - pos[1] < minY) { point0[1] = pos[1]; @@ -427,27 +363,19 @@ void mtxf_align_terrain_triangle(Mat4 mtx, Vec3f pos, s16 yaw, f32 radius) { avgY = (point0[1] + point1[1] + point2[1]) / 3; - vec3f_set(forward, sins(yaw), 0, coss(yaw)); + vec3_set(forward, sins(yaw), 0, coss(yaw)); find_vector_perpendicular_to_plane(yColumn, point0, point1, point2); vec3f_normalize(yColumn); - vec3f_cross(xColumn, yColumn, forward); + vec3_cross(xColumn, yColumn, forward); vec3f_normalize(xColumn); - vec3f_cross(zColumn, xColumn, yColumn); + vec3_cross(zColumn, xColumn, yColumn); vec3f_normalize(zColumn); + vec3_copy(mtx[0], xColumn); + vec3_copy(mtx[1], yColumn); + vec3_copy(mtx[2], zColumn); - mtx[0][0] = xColumn[0]; - mtx[0][1] = xColumn[1]; - mtx[0][2] = xColumn[2]; mtx[3][0] = pos[0]; - - mtx[1][0] = yColumn[0]; - mtx[1][1] = yColumn[1]; - mtx[1][2] = yColumn[2]; mtx[3][1] = (avgY < pos[1]) ? pos[1] : avgY; - - mtx[2][0] = zColumn[0]; - mtx[2][1] = zColumn[1]; - mtx[2][2] = zColumn[2]; mtx[3][2] = pos[2]; mtx[0][3] = 0; @@ -466,41 +394,31 @@ void mtxf_align_terrain_triangle(Mat4 mtx, Vec3f pos, s16 yaw, f32 radius) { */ void mtxf_mul(Mat4 dest, Mat4 a, Mat4 b) { Mat4 temp; - register f32 entry0; - register f32 entry1; - register f32 entry2; + register Vec3f entry; // column 0 - entry0 = a[0][0]; - entry1 = a[0][1]; - entry2 = a[0][2]; - temp[0][0] = entry0 * b[0][0] + entry1 * b[1][0] + entry2 * b[2][0]; - temp[0][1] = entry0 * b[0][1] + entry1 * b[1][1] + entry2 * b[2][1]; - temp[0][2] = entry0 * b[0][2] + entry1 * b[1][2] + entry2 * b[2][2]; + vec3_copy(entry, a[0]); + temp[0][0] = entry[0] * b[0][0] + entry[1] * b[1][0] + entry[2] * b[2][0]; + temp[0][1] = entry[0] * b[0][1] + entry[1] * b[1][1] + entry[2] * b[2][1]; + temp[0][2] = entry[0] * b[0][2] + entry[1] * b[1][2] + entry[2] * b[2][2]; // column 1 - entry0 = a[1][0]; - entry1 = a[1][1]; - entry2 = a[1][2]; - temp[1][0] = entry0 * b[0][0] + entry1 * b[1][0] + entry2 * b[2][0]; - temp[1][1] = entry0 * b[0][1] + entry1 * b[1][1] + entry2 * b[2][1]; - temp[1][2] = entry0 * b[0][2] + entry1 * b[1][2] + entry2 * b[2][2]; + vec3_copy(entry, a[1]); + temp[1][0] = entry[0] * b[0][0] + entry[1] * b[1][0] + entry[2] * b[2][0]; + temp[1][1] = entry[0] * b[0][1] + entry[1] * b[1][1] + entry[2] * b[2][1]; + temp[1][2] = entry[0] * b[0][2] + entry[1] * b[1][2] + entry[2] * b[2][2]; // column 2 - entry0 = a[2][0]; - entry1 = a[2][1]; - entry2 = a[2][2]; - temp[2][0] = entry0 * b[0][0] + entry1 * b[1][0] + entry2 * b[2][0]; - temp[2][1] = entry0 * b[0][1] + entry1 * b[1][1] + entry2 * b[2][1]; - temp[2][2] = entry0 * b[0][2] + entry1 * b[1][2] + entry2 * b[2][2]; + vec3_copy(entry, a[2]); + temp[2][0] = entry[0] * b[0][0] + entry[1] * b[1][0] + entry[2] * b[2][0]; + temp[2][1] = entry[0] * b[0][1] + entry[1] * b[1][1] + entry[2] * b[2][1]; + temp[2][2] = entry[0] * b[0][2] + entry[1] * b[1][2] + entry[2] * b[2][2]; // column 3 - entry0 = a[3][0]; - entry1 = a[3][1]; - entry2 = a[3][2]; - temp[3][0] = entry0 * b[0][0] + entry1 * b[1][0] + entry2 * b[2][0] + b[3][0]; - temp[3][1] = entry0 * b[0][1] + entry1 * b[1][1] + entry2 * b[2][1] + b[3][1]; - temp[3][2] = entry0 * b[0][2] + entry1 * b[1][2] + entry2 * b[2][2] + b[3][2]; + vec3_copy(entry, a[3]); + temp[3][0] = entry[0] * b[0][0] + entry[1] * b[1][0] + entry[2] * b[2][0] + b[3][0]; + temp[3][1] = entry[0] * b[0][1] + entry[1] * b[1][1] + entry[2] * b[2][1] + b[3][1]; + temp[3][2] = entry[0] * b[0][2] + entry[1] * b[1][2] + entry[2] * b[2][2] + b[3][2]; temp[0][3] = temp[1][3] = temp[2][3] = 0; temp[3][3] = 1; @@ -587,12 +505,9 @@ void get_pos_from_transform_mtx(Vec3f dest, Mat4 objMtx, Mat4 camMtx) { f32 camY = camMtx[3][0] * camMtx[1][0] + camMtx[3][1] * camMtx[1][1] + camMtx[3][2] * camMtx[1][2]; f32 camZ = camMtx[3][0] * camMtx[2][0] + camMtx[3][1] * camMtx[2][1] + camMtx[3][2] * camMtx[2][2]; - dest[0] = - objMtx[3][0] * camMtx[0][0] + objMtx[3][1] * camMtx[0][1] + objMtx[3][2] * camMtx[0][2] - camX; - dest[1] = - objMtx[3][0] * camMtx[1][0] + objMtx[3][1] * camMtx[1][1] + objMtx[3][2] * camMtx[1][2] - camY; - dest[2] = - objMtx[3][0] * camMtx[2][0] + objMtx[3][1] * camMtx[2][1] + objMtx[3][2] * camMtx[2][2] - camZ; + dest[0] = objMtx[3][0] * camMtx[0][0] + objMtx[3][1] * camMtx[0][1] + objMtx[3][2] * camMtx[0][2] - camX; + dest[1] = objMtx[3][0] * camMtx[1][0] + objMtx[3][1] * camMtx[1][1] + objMtx[3][2] * camMtx[1][2] - camY; + dest[2] = objMtx[3][0] * camMtx[2][0] + objMtx[3][1] * camMtx[2][1] + objMtx[3][2] * camMtx[2][2] - camZ; } /** @@ -625,19 +540,11 @@ void vec3f_set_dist_and_angle(Vec3f from, Vec3f to, f32 dist, s16 pitch, s16 yaw * most 'inc' and going down at most 'dec'. */ s32 approach_s32(s32 current, s32 target, s32 inc, s32 dec) { - //! If target is close to the max or min s32, then it's possible to overflow - // past it without stopping. - - if (current < target) { - current += inc; - if (current > target) { - current = target; - } - } else { - current -= dec; - if (current < target) { - current = target; - } + s32 dist = (target - current); + if (dist > 0) { // current < target + current = ((dist > inc) ? (current + inc) : target); + } else if (dist < 0) { // current > target + current = ((dist < -dec) ? (current - dec) : target); } return current; } @@ -647,16 +554,11 @@ s32 approach_s32(s32 current, s32 target, s32 inc, s32 dec) { * most 'inc' and going down at most 'dec'. */ f32 approach_f32(f32 current, f32 target, f32 inc, f32 dec) { - if (current < target) { - current += inc; - if (current > target) { - current = target; - } - } else { - current -= dec; - if (current < target) { - current = target; - } + f32 dist = (target - current); + if (dist >= 0.0f) { // target >= current + current = ((dist > inc) ? (current + inc) : target); + } else { // target < current + current = ((dist < -dec) ? (current - dec) : target); } return current; } @@ -665,16 +567,7 @@ f32 approach_f32(f32 current, f32 target, f32 inc, f32 dec) { * Helper function for atan2s. Does a look up of the arctangent of y/x assuming * the resulting angle is in range [0, 0x2000] (1/8 of a circle). */ -static u16 atan2_lookup(f32 y, f32 x) { - u16 ret; - - if (x == 0) { - ret = gArctanTable[0]; - } else { - ret = gArctanTable[(s32)(y / x * 1024 + 0.5f)]; - } - return ret; -} +#define atan2_lookup(y, x) ((x == 0) ? 0x0 : atans((y) / (x))) /** * Compute the angle from (0, 0) to (x, y) as a s16. Given that terrain is in @@ -725,11 +618,11 @@ f32 atan2f(f32 y, f32 x) { return (f32) atan2s(y, x) * M_PI / 0x8000; } -#define CURVE_BEGIN_1 1 -#define CURVE_BEGIN_2 2 -#define CURVE_MIDDLE 3 -#define CURVE_END_1 4 -#define CURVE_END_2 5 +#define CURVE_BEGIN_1 0x1 +#define CURVE_BEGIN_2 0x2 +#define CURVE_MIDDLE 0x3 +#define CURVE_END_1 0x4 +#define CURVE_END_2 0x5 /** * Set 'result' to a 4-vector with weights corresponding to interpolation @@ -820,7 +713,7 @@ s32 anim_spline_poll(Vec3f result) { s32 i; s32 hasEnded = FALSE; - vec3f_copy(result, gVec3fZero); + vec3_zero(result); spline_get_weights(weights, gSplineKeyframeFraction, gSplineState); for (i = 0; i < 4; i++) { result[0] += weights[i] * gSplineKeyframe[i][1]; @@ -851,9 +744,7 @@ s32 anim_spline_poll(Vec3f result) { /// Multiply vector 'dest' by a void vec3f_mul(Vec3f dest, f32 a) { - dest[0] *= a; - dest[1] *= a; - dest[2] *= a; + vec3_mul_val(dest, a); } /// Get length of vector 'a' @@ -863,14 +754,12 @@ f32 vec3f_length(Vec3f a) { /// Get dot product of vectors 'a' and 'b' f32 vec3f_dot(Vec3f a, Vec3f b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; + return vec3_dot(a, b); } /// Make 'dest' the difference of vectors a and b. void vec3f_dif(Vec3f dest, Vec3f a, Vec3f b) { - dest[0] = a[0] - b[0]; - dest[1] = a[1] - b[1]; - dest[2] = a[2] - b[2]; + vec3_diff(dest, a, b); } // Raycasting @@ -885,51 +774,49 @@ s32 ray_surface_intersect(Vec3f orig, Vec3f dir, f32 dir_length, struct Surface return FALSE; // Get surface normal and some other stuff - norm[0] = 0; - norm[1] = surface->normal.y; - norm[2] = 0; - vec3f_mul(norm,RAY_OFFSET); + vec3_set(norm, 0, surface->normal.y, 0); + vec3_mul_val(norm, RAY_OFFSET); - vec3s_to_vec3f(v0, surface->vertex1); - vec3s_to_vec3f(v1, surface->vertex2); - vec3s_to_vec3f(v2, surface->vertex3); + vec3_copy(v0, surface->vertex1); + vec3_copy(v1, surface->vertex2); + vec3_copy(v2, surface->vertex3); - vec3f_add(v0, norm); - vec3f_add(v1, norm); - vec3f_add(v2, norm); + vec3_add(v0, norm); + vec3_add(v1, norm); + vec3_add(v2, norm); - vec3f_dif(e1, v1, v0); - vec3f_dif(e2, v2, v0); + vec3_diff(e1, v1, v0); + vec3_diff(e2, v2, v0); - vec3f_cross(h, dir, e2); + vec3_cross(h, dir, e2); // Check if we're perpendicular from the surface - a = vec3f_dot(e1, h); - if (a > -0.00001f && a < 0.00001f) + a = vec3_dot(e1, h); + if (a > -0.00001f && a < 0.00001f) { return FALSE; - + } // Check if we're making contact with the surface f = 1.0f / a; - vec3f_dif(s, orig, v0); - u = f * vec3f_dot(s, h); - if (u < 0.0f || u > 1.0f) + vec3_diff(s, orig, v0); + u = f * vec3_dot(s, h); + if (u < 0.0f || u > 1.0f) { return FALSE; - - vec3f_cross(q, s, e1); - v = f * vec3f_dot(dir, q); - if (v < 0.0f || u + v > 1.0f) + } + vec3_cross(q, s, e1); + v = f * vec3_dot(dir, q); + if (v < 0.0f || u + v > 1.0f) { return FALSE; - + } // Get the length between our origin and the surface contact point - *length = f * vec3f_dot(e2, q); - if (*length <= 0.00001 || *length > dir_length) + *length = f * vec3_dot(e2, q); + if (*length <= 0.00001 || *length > dir_length) { return FALSE; - + } // Successful contact - vec3f_copy(add_dir, dir); - vec3f_mul(add_dir, *length); - vec3f_sum(hit_pos, orig, add_dir); + vec3_copy(add_dir, dir); + vec3_mul_val(add_dir, *length); + vec3_sum(hit_pos, orig, add_dir); return TRUE; } @@ -954,14 +841,14 @@ void find_surface_on_ray_list(struct SurfaceNode *list, Vec3f orig, Vec3f dir, f // Iterate through every surface of the list for (; list != NULL; list = list->next) { // Reject surface if out of vertical bounds - if (list->surface->lowerY > top || list->surface->upperY < bottom) + if (list->surface->lowerY > top || list->surface->upperY < bottom) { continue; - + } // Check intersection between the ray and this surface if ((hit = ray_surface_intersect(orig, dir, dir_length, list->surface, chk_hit_pos, &length)) != 0) { if (length <= *max_length) { *hit_surface = list->surface; - vec3f_copy(hit_pos, chk_hit_pos); + vec3_copy(hit_pos, chk_hit_pos); *max_length = length; } } @@ -1005,12 +892,12 @@ void find_surface_on_ray(Vec3f orig, Vec3f dir, struct Surface **hit_surface, Ve // Set that no surface has been hit *hit_surface = NULL; - vec3f_sum(hit_pos, orig, dir); + vec3_sum(hit_pos, orig, dir); // Get normalized direction dir_length = vec3f_length(dir); max_length = dir_length; - vec3f_copy(normalized_dir, dir); + vec3_copy(normalized_dir, dir); vec3f_normalize(normalized_dir); // Get our cell coordinate diff --git a/src/engine/math_util.h b/src/engine/math_util.h index 60ecc52c..a17b063b 100644 --- a/src/engine/math_util.h +++ b/src/engine/math_util.h @@ -23,6 +23,7 @@ extern f32 gSineTable[]; #define sins(x) gSineTable[(u16) (x) >> 4] #define coss(x) gCosineTable[(u16) (x) >> 4] +#define atans(x) gArctanTable[(s32)((((x) * 1024) + 0.5f))] // is this correct? used for atan2_lookup #define DEG_PER_RAD 57.29577950560105 #define RAD_PER_DEG (1.0 / DEG_PER_RAD) @@ -349,7 +350,6 @@ void vec3s_set(Vec3s dest, s16 x, s16 y, s16 z); void vec3s_add(Vec3s dest, Vec3s a); void vec3s_sum(Vec3s dest, Vec3s a, Vec3s b); void vec3s_sub(Vec3s dest, Vec3s a); -void vec3s_to_vec3f(Vec3f dest, Vec3s a); void vec3f_to_vec3s(Vec3s dest, Vec3f a); void find_vector_perpendicular_to_plane(Vec3f dest, Vec3f a, Vec3f b, Vec3f c); void vec3f_cross(Vec3f dest, Vec3f a, Vec3f b); diff --git a/src/game/behaviors/donut_platform.inc.c b/src/game/behaviors/donut_platform.inc.c index f1503a0e..b3e07430 100644 --- a/src/game/behaviors/donut_platform.inc.c +++ b/src/game/behaviors/donut_platform.inc.c @@ -25,7 +25,7 @@ void bhv_donut_platform_spawner_update(void) { dx = gMarioObject->oPosX - sDonutPlatformPositions[i][0]; dy = gMarioObject->oPosY - sDonutPlatformPositions[i][1]; dz = gMarioObject->oPosZ - sDonutPlatformPositions[i][2]; - marioSqDist = dx * dx + dy * dy + dz * dz; + marioSqDist = sqr(dx) + sqr(dy) + sqr(dz); // dist > 1000 and dist < 2000 if (marioSqDist > 1000000.0f && marioSqDist < 4000000.0f) { diff --git a/src/game/behaviors/drawbridge.inc.c b/src/game/behaviors/drawbridge.inc.c index 0fd04905..88561366 100644 --- a/src/game/behaviors/drawbridge.inc.c +++ b/src/game/behaviors/drawbridge.inc.c @@ -17,8 +17,6 @@ void bhv_lll_drawbridge_spawner_loop(void) { } void bhv_lll_drawbridge_loop(void) { - s32 globalTimer = gGlobalTimer; - switch (o->oAction) { case LLL_DRAWBRIDGE_ACT_LOWER: o->oFaceAngleRoll += 0x100; @@ -32,9 +30,7 @@ void bhv_lll_drawbridge_loop(void) { if ((s16) o->oFaceAngleRoll < -0x1FFD) { o->oFaceAngleRoll = 0xDFFF; - //! Because the global timer increments when the game is paused, pausing and unpausing - // the game at regular intervals can leave the drawbridge raised indefinitely. - if (o->oTimer >= 51 && (globalTimer % 8) == 0) { + if (o->oTimer >= 51 && (o->oTimer % 8) == 0) { o->oAction = LLL_DRAWBRIDGE_ACT_LOWER; cur_obj_play_sound_2(SOUND_GENERAL_BOAT_TILT1); } @@ -43,9 +39,7 @@ void bhv_lll_drawbridge_loop(void) { if ((s16) o->oFaceAngleRoll >= 0) { o->oFaceAngleRoll = 0; - //! Because the global timer increments when the game is paused, pausing and unpausing - // the game at regular intervals can leave the drawbridge lowered indefinitely. - if (o->oTimer >= 51 && (globalTimer % 8) == 0) { + if (o->oTimer >= 51 && (o->oTimer % 8) == 0) { o->oAction = LLL_DRAWBRIDGE_ACT_RAISE; cur_obj_play_sound_2(SOUND_GENERAL_BOAT_TILT2); } diff --git a/src/game/behaviors/falling_rising_platform.inc.c b/src/game/behaviors/falling_rising_platform.inc.c index 35359690..6701027d 100644 --- a/src/game/behaviors/falling_rising_platform.inc.c +++ b/src/game/behaviors/falling_rising_platform.inc.c @@ -9,9 +9,7 @@ void bhv_squishable_platform_loop(void) { } void bhv_bitfs_sinking_platform_loop(void) { - o->oPosY -= - sins(o->oBitfsPlatformTimer) - * 0.58; //! f32 double conversion error accumulates on Wii VC causing the platform to rise up + o->oPosY -= sins(o->oBitfsPlatformTimer) * 0.58f; o->oBitfsPlatformTimer += 0x100; } diff --git a/src/game/behaviors/grand_star.inc.c b/src/game/behaviors/grand_star.inc.c index 333aea6c..4fa2ec99 100644 --- a/src/game/behaviors/grand_star.inc.c +++ b/src/game/behaviors/grand_star.inc.c @@ -3,7 +3,7 @@ s32 arc_to_goal_pos(Vec3f a0, Vec3f a1, f32 yVel, f32 gravity) { f32 dx = a0[0] - a1[0]; f32 dz = a0[2] - a1[2]; - f32 planarDist = sqrtf(dx * dx + dz * dz); + f32 planarDist = sqrtf(sqr(dx) + sqr(dz)); s32 time; o->oMoveAngleYaw = atan2s(dz, dx); o->oVelY = yVel; diff --git a/src/game/behaviors/klepto.inc.c b/src/game/behaviors/klepto.inc.c index 97e816ba..c7051475 100644 --- a/src/game/behaviors/klepto.inc.c +++ b/src/game/behaviors/klepto.inc.c @@ -104,7 +104,7 @@ static void klepto_change_target(void) { dx = gMarioObject->oPosX - sKleptoTargetPositions[i][0]; dz = gMarioObject->oPosZ - sKleptoTargetPositions[i][2]; - targetDist = sqrtf(dx * dx + dz * dz); + targetDist = sqrtf(sqr(dx) + sqr(dz)); if (targetDist < minTargetDist) { minTargetDist = targetDist; newTarget = i; diff --git a/src/game/behaviors/mad_piano.inc.c b/src/game/behaviors/mad_piano.inc.c index efefaba4..08c053a3 100644 --- a/src/game/behaviors/mad_piano.inc.c +++ b/src/game/behaviors/mad_piano.inc.c @@ -44,7 +44,7 @@ static void mad_piano_act_attack(void) { } else { f32 dx = o->oPosX - o->oHomeX; f32 dz = o->oPosZ - o->oHomeZ; - f32 distToHome = sqrtf(dx * dx + dz * dz); + f32 distToHome = sqrtf(sqr(dx) + sqr(dz)); if (distToHome > 400.0f) { distToHome = 400.0f / distToHome; diff --git a/src/game/behaviors/monty_mole.inc.c b/src/game/behaviors/monty_mole.inc.c index 685f5aae..3276cefd 100644 --- a/src/game/behaviors/monty_mole.inc.c +++ b/src/game/behaviors/monty_mole.inc.c @@ -372,12 +372,10 @@ void bhv_monty_mole_update(void) { f32 dy = o->oPosY - sMontyMoleLastKilledPosY; f32 dz = o->oPosZ - sMontyMoleLastKilledPosZ; - f32 distToLastKill = sqrtf(dx * dx + dy * dy + dz * dz); - //! The two farthest holes on the bottom level of TTM are more than // 1500 units away from each other, so the counter resets if you // attack moles in these holes consecutively. - if (distToLastKill < 1500.0f) { + if ((sqr(dx) + sqr(dy) + sqr(dz)) < sqr(1500.0f)) { if (sMontyMoleKillStreak == 7) { play_puzzle_jingle(); spawn_object(o, MODEL_1UP, bhv1upWalking); @@ -387,8 +385,9 @@ void bhv_monty_mole_update(void) { } } - //! No overflow check - sMontyMoleKillStreak += 1; + if (sMontyMoleKillStreak < (1 << 15)) { + sMontyMoleKillStreak++; + } sMontyMoleLastKilledPosX = o->oPosX; sMontyMoleLastKilledPosY = o->oPosY; diff --git a/src/game/behaviors/sparkle_spawn_star.inc.c b/src/game/behaviors/sparkle_spawn_star.inc.c index 08c27181..0121f4e8 100644 --- a/src/game/behaviors/sparkle_spawn_star.inc.c +++ b/src/game/behaviors/sparkle_spawn_star.inc.c @@ -41,7 +41,7 @@ void set_home_to_mario(void) { o->oPosY = o->oHomeY; dx = o->oHomeX - o->oPosX; dz = o->oHomeZ - o->oPosZ; - o->oForwardVel = sqrtf(dx * dx + dz * dz) / 23.0f; + o->oForwardVel = sqrtf(sqr(dx) + sqr(dz)) / 23.0f; } void set_y_home_to_pos(void) { diff --git a/src/game/behaviors/tilting_inverted_pyramid.inc.c b/src/game/behaviors/tilting_inverted_pyramid.inc.c index 1e56addc..4e07cf11 100644 --- a/src/game/behaviors/tilting_inverted_pyramid.inc.c +++ b/src/game/behaviors/tilting_inverted_pyramid.inc.c @@ -92,7 +92,7 @@ void bhv_tilting_inverted_pyramid_loop(void) { dx = gMarioObject->oPosX - o->oPosX; dy = 500.0f; dz = gMarioObject->oPosZ - o->oPosZ; - d = sqrtf(dx * dx + dy * dy + dz * dz); + d = sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); //! Always true since dy = 500, making d >= 500. if (d != 0.0f) { diff --git a/src/game/behaviors/wiggler.inc.c b/src/game/behaviors/wiggler.inc.c index 785daca4..718245af 100644 --- a/src/game/behaviors/wiggler.inc.c +++ b/src/game/behaviors/wiggler.inc.c @@ -192,7 +192,7 @@ void wiggler_init_segments(void) { bodyPart->yaw = prevBodyPart->yaw + dyaw; // As the head tilts, propagate the tilt backward - dxz = sqrtf(dx * dx + dz * dz); + dxz = sqrtf(sqr(dx) + sqr(dz)); dpitch = atan2s(dxz, dy) - prevBodyPart->pitch; clamp_s16(&dpitch, -0x2000, 0x2000); bodyPart->pitch = prevBodyPart->pitch + dpitch; diff --git a/src/game/camera.c b/src/game/camera.c index 260aea67..92dd9bc0 100644 --- a/src/game/camera.c +++ b/src/game/camera.c @@ -1287,8 +1287,8 @@ s32 update_parallel_tracking_camera(struct Camera *c, Vec3f focus, Vec3f pos) { vec3f_copy(focOffset, marioOffset); // OK - focOffset[0] = -focOffset[0] * 0.f; - focOffset[1] = focOffset[1] * 0.f; + focOffset[0] = 0.f; + focOffset[1] = 0.f; // Repeat above calcs with camOffset camOffset[0] = pos[0] - parMidPoint[0]; @@ -1319,11 +1319,6 @@ s32 update_parallel_tracking_camera(struct Camera *c, Vec3f focus, Vec3f pos) { marioOffset[1] = marioOffset[1] * zoom; marioOffset[2] = camOffset[2]; - //! Does nothing because focOffset[0] is always 0 - focOffset[0] *= 0.3f; - //! Does nothing because focOffset[1] is always 0 - focOffset[1] *= 0.3f; - pathAngle[0] = pathPitch; pathAngle[1] = pathYaw; //! No effect @@ -2128,12 +2123,8 @@ s16 update_default_camera(struct Camera *c) { unusedFreeRoamWallYaw = avoidYaw; sAvoidYawVel = yaw; sStatusFlags |= CAM_FLAG_COLLIDED_WITH_WALL; - //! Does nothing - vec3f_get_dist_and_angle(sMarioCamState->pos, cPos, &xzDist, &tempPitch, &tempYaw); // Rotate to avoid the wall approach_s16_asymptotic_bool(&yaw, avoidYaw, 10); - //! Does nothing - vec3f_set_dist_and_angle(sMarioCamState->pos, cPos, xzDist, tempPitch, tempYaw); sAvoidYawVel = (sAvoidYawVel - yaw) / 0x100; } else { if (gMarioStates[0].forwardVel == 0.f) { @@ -2386,8 +2377,6 @@ s32 update_spiral_stairs_camera(struct Camera *c, Vec3f focus, Vec3f pos) { } focYaw += sSpiralStairsYawOffset; posYaw = focYaw; - //! @bug unnecessary - camera_approach_s16_symmetric_bool(&posYaw, focYaw, 0x1000); vec3f_set_dist_and_angle(sFixedModeBasePosition, cPos, 300.f, 0, posYaw); @@ -4442,7 +4431,7 @@ s16 calculate_pitch(Vec3f from, Vec3f to) { f32 dx = to[0] - from[0]; f32 dy = to[1] - from[1]; f32 dz = to[2] - from[2]; - s16 pitch = atan2s(sqrtf(dx * dx + dz * dz), dy); + s16 pitch = atan2s(sqrtf(sqr(dx) + sqr(dz)), dy); return pitch; } @@ -4464,7 +4453,7 @@ void calculate_angles(Vec3f from, Vec3f to, s16 *pitch, s16 *yaw) { f32 dy = to[1] - from[1]; f32 dz = to[2] - from[2]; - *pitch = atan2s(sqrtf(dx * dx + dz * dz), dy); + *pitch = atan2s(sqrtf(sqr(dx) + sqr(dz)), dy); *yaw = atan2s(dz, dx); } @@ -4475,7 +4464,7 @@ f32 calc_abs_dist(Vec3f a, Vec3f b) { f32 distX = b[0] - a[0]; f32 distY = b[1] - a[1]; f32 distZ = b[2] - a[2]; - f32 distAbs = sqrtf(distX * distX + distY * distY + distZ * distZ); + f32 distAbs = sqrtf(sqr(distX) + sqr(distY) + sqr(distZ)); return distAbs; } @@ -4486,7 +4475,7 @@ f32 calc_abs_dist(Vec3f a, Vec3f b) { f32 calc_hor_dist(Vec3f a, Vec3f b) { f32 distX = b[0] - a[0]; f32 distZ = b[2] - a[2]; - f32 distHor = sqrtf(distX * distX + distZ * distZ); + f32 distHor = sqrtf(sqr(distX) + sqr(distZ)); return distHor; } @@ -7028,20 +7017,6 @@ void retrieve_info_star(struct Camera *c) { vec3f_copy(c->focus, sCameraStoreCutscene.focus); } -static UNUSED void unused_vec3s_to_vec3f(Vec3f dst, Vec3s src) { - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[2]; -} - -static UNUSED void unused_vec3f_to_vec3s(Vec3s dst, Vec3f src) { - // note: unlike vec3f_to_vec3s(), this function doesn't round the numbers and instead simply - // truncates them - dst[0] = src[0]; - dst[1] = src[1]; - dst[2] = src[2]; -} - /** * Rotate the camera's focus around the camera's position by incYaw and incPitch */ @@ -7942,11 +7917,8 @@ void cutscene_bowser_arena_set_pos(struct Camera *c) { * The y offset starts at 120, then decreases to 0 before reaching ~240 on the last frame. */ void cutscene_bowser_arena_focus_sine(UNUSED struct Camera *c) { - //! unused initialization - f32 yOff = 150.0f; - // cvar4 was zeroed when the cutscene started. - yOff = sins(sCutsceneVars[4].angle[1]) * 120.0f + 120.0f; + f32 yOff = sins(sCutsceneVars[4].angle[1]) * 120.0f + 120.0f; sCutsceneVars[4].angle[1] -= 0x200; approach_f32_asymptotic_bool(&sCutsceneVars[0].point[1], yOff, 0.5f); } diff --git a/src/game/envfx_snow.c b/src/game/envfx_snow.c index 11cc0945..a3640cdb 100644 --- a/src/game/envfx_snow.c +++ b/src/game/envfx_snow.c @@ -151,8 +151,8 @@ void orbit_from_positions(Vec3s from, Vec3s to, s16 *radius, s16 *pitch, s16 *ya f32 dy = to[1] - from[1]; f32 dz = to[2] - from[2]; - *radius = (s16) sqrtf(dx * dx + dy * dy + dz * dz); - *pitch = atan2s(sqrtf(dx * dx + dz * dz), dy); + *radius = (s16) sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); + *pitch = atan2s(sqrtf(sqr(dx) + sqr(dz)), dy); *yaw = atan2s(dz, dx); } diff --git a/src/game/mario.c b/src/game/mario.c index a7fa3526..7bdb88db 100644 --- a/src/game/mario.c +++ b/src/game/mario.c @@ -718,7 +718,7 @@ void set_steep_jump_action(struct MarioState *m) { f32 y = sins(faceAngleTemp) * m->forwardVel; f32 x = coss(faceAngleTemp) * m->forwardVel * 0.75f; - m->forwardVel = sqrtf(y * y + x * x); + m->forwardVel = sqrtf(sqr(y) + sqr(x)); m->faceAngle[1] = atan2s(x, y) + angleTemp; } @@ -1834,10 +1834,10 @@ void init_mario(void) { gMarioState->area = gCurrentArea; gMarioState->marioObj = gMarioObject; gMarioState->marioObj->header.gfx.animInfo.animID = -1; - vec3s_copy(gMarioState->faceAngle, gMarioSpawnInfo->startAngle); - vec3s_set(gMarioState->angleVel, 0, 0, 0); - vec3s_to_vec3f(gMarioState->pos, gMarioSpawnInfo->startPos); - vec3f_set(gMarioState->vel, 0, 0, 0); + vec3_copy(gMarioState->faceAngle, gMarioSpawnInfo->startAngle); + vec3_zero(gMarioState->angleVel); + vec3_copy(gMarioState->pos, gMarioSpawnInfo->startPos); + vec3_zero(gMarioState->vel); gMarioState->floorHeight = find_floor(gMarioState->pos[0], gMarioState->pos[1], gMarioState->pos[2], &gMarioState->floor); if (gMarioState->pos[1] < gMarioState->floorHeight) { diff --git a/src/game/mario_actions_cutscene.c b/src/game/mario_actions_cutscene.c index 08ea592d..45ffa280 100644 --- a/src/game/mario_actions_cutscene.c +++ b/src/game/mario_actions_cutscene.c @@ -254,7 +254,7 @@ void handle_save_menu(struct MarioState *m) { if (gSaveOptSelectIndex == MENU_OPT_SAVE_AND_QUIT) { //! crashes - fade_into_special_warp(WARP_SPECIAL_MARIO_HEAD_REGULAR, 0);// reset game + fade_into_special_warp(WARP_SPECIAL_MARIO_HEAD_REGULAR, 0); // reset game } } diff --git a/src/game/mario_actions_moving.c b/src/game/mario_actions_moving.c index 687100f9..bbb0e753 100644 --- a/src/game/mario_actions_moving.c +++ b/src/game/mario_actions_moving.c @@ -694,7 +694,7 @@ void push_or_sidle_wall(struct MarioState *m, Vec3f startPos) { s16 dWallAngle; f32 dx = m->pos[0] - startPos[0]; f32 dz = m->pos[2] - startPos[2]; - f32 movedDistance = sqrtf(dx * dx + dz * dz); + f32 movedDistance = sqrtf(sqr(dx) + sqr(dz)); //! (Speed Crash) If a wall is after moving 16384 distance, this crashes. s32 animSpeed = (s32)(movedDistance * 2.0f * 0x10000); diff --git a/src/game/mario_actions_submerged.c b/src/game/mario_actions_submerged.c index 1211de91..7d57a2a9 100644 --- a/src/game/mario_actions_submerged.c +++ b/src/game/mario_actions_submerged.c @@ -139,8 +139,8 @@ static void apply_water_current(struct MarioState *m, Vec3f step) { f32 dy = whirlpool->pos[1] - m->pos[1]; f32 dz = whirlpool->pos[2] - m->pos[2]; - f32 lateralDist = sqrtf(dx * dx + dz * dz); - f32 distance = sqrtf(lateralDist * lateralDist + dy * dy); + f32 lateralDist = sqrtf(sqr(dx) + sqr(dz)); + f32 distance = sqrtf(lateralDist * lateralDist + sqr(dy)); s16 pitchToWhirlpool = atan2s(lateralDist, dy); s16 yawToWhirlpool = atan2s(dz, dx); @@ -1049,7 +1049,7 @@ static s32 act_caught_in_whirlpool(struct MarioState *m) { f32 dx = m->pos[0] - whirlpool->oPosX; f32 dz = m->pos[2] - whirlpool->oPosZ; - f32 distance = sqrtf(dx * dx + dz * dz); + f32 distance = sqrtf(sqr(dx) + sqr(dz)); if ((marioObj->oMarioWhirlpoolPosY += m->vel[1]) < 0.0f) { marioObj->oMarioWhirlpoolPosY = 0.0f; diff --git a/src/game/obj_behaviors_2.c b/src/game/obj_behaviors_2.c index c6294082..74c3b7a2 100644 --- a/src/game/obj_behaviors_2.c +++ b/src/game/obj_behaviors_2.c @@ -191,7 +191,7 @@ static void platform_on_track_update_pos_or_spawn_ball(s32 ballIndex, f32 x, f32 dy = nextWaypoint->pos[1] - y; dz = nextWaypoint->pos[2] - z; - distToNextWaypoint = sqrtf(dx * dx + dy * dy + dz * dz); + distToNextWaypoint = sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); // Move directly to the next waypoint, even if it's farther away // than amountToMove @@ -856,7 +856,7 @@ static void treat_far_home_as_mario(f32 threshold) { f32 dx = o->oHomeX - o->oPosX; f32 dy = o->oHomeY - o->oPosY; f32 dz = o->oHomeZ - o->oPosZ; - f32 distance = sqrtf(dx * dx + dy * dy + dz * dz); + f32 distance = sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); if (distance > threshold) { o->oAngleToMario = atan2s(dz, dx); @@ -865,7 +865,7 @@ static void treat_far_home_as_mario(f32 threshold) { dx = o->oHomeX - gMarioObject->oPosX; dy = o->oHomeY - gMarioObject->oPosY; dz = o->oHomeZ - gMarioObject->oPosZ; - distance = sqrtf(dx * dx + dy * dy + dz * dz); + distance = sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); if (distance > threshold) { o->oDistanceToMario = 20000.0f; diff --git a/src/game/object_collision.c b/src/game/object_collision.c index 03be0611..3e4b0bf4 100644 --- a/src/game/object_collision.c +++ b/src/game/object_collision.c @@ -6,6 +6,7 @@ #include "mario.h" #include "object_list_processor.h" #include "spawn_object.h" +#include "engine/math_util.h" struct Object *debug_print_obj_collision(struct Object *a) { struct Object *currCollidedObj; @@ -27,7 +28,7 @@ s32 detect_object_hitbox_overlap(struct Object *a, struct Object *b) { f32 dx = a->oPosX - b->oPosX; f32 dz = a->oPosZ - b->oPosZ; f32 collisionRadius = a->hitboxRadius + b->hitboxRadius; - f32 distance = sqrtf(dx * dx + dz * dz); + f32 distance = sqrtf(sqr(dx) + sqr(dz)); if (collisionRadius > distance) { f32 dya_top = a->hitboxHeight + dya_bottom; @@ -63,7 +64,7 @@ s32 detect_object_hurtbox_overlap(struct Object *a, struct Object *b) { f32 dx = a->oPosX - b->oPosX; f32 dz = a->oPosZ - b->oPosZ; f32 collisionRadius = a->hurtboxRadius + b->hurtboxRadius; - f32 distance = sqrtf(dx * dx + dz * dz); + f32 distance = sqrtf(sqr(dx) + sqr(dz)); if (a == gMarioObject) { b->oInteractionSubtype |= INT_SUBTYPE_DELAY_INVINCIBILITY; diff --git a/src/game/object_helpers.c b/src/game/object_helpers.c index 6c4268d4..5fcd99f1 100644 --- a/src/game/object_helpers.c +++ b/src/game/object_helpers.c @@ -263,7 +263,7 @@ f32 lateral_dist_between_objects(struct Object *obj1, struct Object *obj2) { f32 dx = obj1->oPosX - obj2->oPosX; f32 dz = obj1->oPosZ - obj2->oPosZ; - return sqrtf(dx * dx + dz * dz); + return sqrtf(sqr(dx) + sqr(dz)); } f32 dist_between_objects(struct Object *obj1, struct Object *obj2) { @@ -271,7 +271,7 @@ f32 dist_between_objects(struct Object *obj1, struct Object *obj2) { f32 dy = obj1->oPosY - obj2->oPosY; f32 dz = obj1->oPosZ - obj2->oPosZ; - return sqrtf(dx * dx + dy * dy + dz * dz); + return sqrtf(sqr(dx) + sqr(dy) + sqr(dz)); } void cur_obj_forward_vel_approach_upward(f32 target, f32 increment) { @@ -1462,7 +1462,7 @@ f32 cur_obj_lateral_dist_from_mario_to_home(void) { f32 dx = o->oHomeX - gMarioObject->oPosX; f32 dz = o->oHomeZ - gMarioObject->oPosZ; - dist = sqrtf(dx * dx + dz * dz); + dist = sqrtf(sqr(dx) + sqr(dz)); return dist; } @@ -1471,7 +1471,7 @@ f32 cur_obj_lateral_dist_to_home(void) { f32 dx = o->oHomeX - o->oPosX; f32 dz = o->oHomeZ - o->oPosZ; - dist = sqrtf(dx * dx + dz * dz); + dist = sqrtf(sqr(dx) + sqr(dz)); return dist; } diff --git a/src/game/rendering_graph_node.c b/src/game/rendering_graph_node.c index d4c3b61c..22b3854f 100644 --- a/src/game/rendering_graph_node.c +++ b/src/game/rendering_graph_node.c @@ -566,7 +566,7 @@ static void geo_process_translation_rotation(struct GraphNodeTranslationRotation Vec3f translation; Mtx *mtx = alloc_display_list(sizeof(*mtx)); - vec3s_to_vec3f(translation, node->translation); + vec3_copy(translation, node->translation); mtxf_rotate_zxy_and_translate(mtxf, translation, node->rotation); mtxf_mul(gMatStack[gMatStackIndex + 1], mtxf, gMatStack[gMatStackIndex]); gMatStackIndex++; @@ -591,7 +591,7 @@ static void geo_process_translation(struct GraphNodeTranslation *node) { Vec3f translation; Mtx *mtx = alloc_display_list(sizeof(*mtx)); - vec3s_to_vec3f(translation, node->translation); + vec3_copy(translation, node->translation); mtxf_rotate_zxy_and_translate(mtxf, translation, gVec3sZero); mtxf_mul(gMatStack[gMatStackIndex + 1], mtxf, gMatStack[gMatStackIndex]); gMatStackIndex++; @@ -664,7 +664,7 @@ static void geo_process_billboard(struct GraphNodeBillboard *node) { Mtx *mtx = alloc_display_list(sizeof(*mtx)); gMatStackIndex++; - vec3s_to_vec3f(translation, node->translation); + vec3_copy(translation, node->translation); mtxf_billboard(gMatStack[gMatStackIndex], gMatStack[gMatStackIndex - 1], translation, gCurGraphNodeCamera->roll); if (gCurGraphNodeHeldObject != NULL) { @@ -1058,8 +1058,7 @@ static s32 obj_is_in_view(struct GraphNodeObject *node, Mat4 matrix) { hScreenEdge *= GFX_DIMENSIONS_ASPECT_RATIO; if (geo != NULL && geo->type == GRAPH_NODE_TYPE_CULLING_RADIUS) { - cullingRadius = - (f32)((struct GraphNodeCullingRadius *) geo)->cullingRadius; //! Why is there a f32 cast? + cullingRadius = ((struct GraphNodeCullingRadius *) geo)->cullingRadius; } else { cullingRadius = 300; }