From 64c5bcd1bcbcc491718764d592695ddb31315156 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 11 Aug 2023 19:30:02 +0900 Subject: [PATCH] Refactorings, cleanup --- src/3d_helpers.cc | 12 +++++++++++ src/3d_helpers.h | 5 +++++ src/screen_trunner.cc | 16 ++------------- src/walker.h | 47 ++++++++++++++++++------------------------- 4 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/3d_helpers.cc b/src/3d_helpers.cc index b0f67ec..9f413da 100644 --- a/src/3d_helpers.cc +++ b/src/3d_helpers.cc @@ -164,3 +164,15 @@ std::optional ray_to_plane(const Ray &ray, const Ray &plane) { Vector3 operator+(const Vector3 &a, const Vector3 &b) { return Vector3{a.x + b.x, a.y + b.y, a.z + b.z}; } + +Vector3 operator-(const Vector3 &a, const Vector3 &b) { + return Vector3{a.x - b.x, a.y - b.y, a.z - b.z}; +} + +Vector3 operator*(Vector3 vec3, float factor) { + return Vector3Scale(vec3, factor); +} + +Vector3 operator*(Matrix mat, Vector3 vec3) { + return Vector3Transform(vec3, mat); +} diff --git a/src/3d_helpers.h b/src/3d_helpers.h index 47695b6..a407c01 100644 --- a/src/3d_helpers.h +++ b/src/3d_helpers.h @@ -32,5 +32,10 @@ extern std::optional ray_to_plane(const Ray &ray, const Ray &plane); // extern Vector4 operator*(const Matrix &m, const Vector4 &v); extern Vector3 operator+(const Vector3 &a, const Vector3 &b); +extern Vector3 operator-(const Vector3 &a, const Vector3 &b); + +extern Vector3 operator*(Vector3 vec3, float factor); + +extern Vector3 operator*(Matrix mat, Vector3 vec3); #endif diff --git a/src/screen_trunner.cc b/src/screen_trunner.cc index 7d2d132..2e95e80 100644 --- a/src/screen_trunner.cc +++ b/src/screen_trunner.cc @@ -257,9 +257,8 @@ bool TRunnerScreen::update(float dt) { this->camera_target.z = zf; if (idx != SURFACE_UNIT_WIDTH / 2 + (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { - this->camera_pos = Vector3Add( - Vector3Scale(Vector3Normalize(this->camera_target), 4.0F), - this->camera_target); + this->camera_pos = (Vector3Normalize(this->camera_target) * 4.0F) + + this->camera_target; this->camera_pos.y += 4.0F; } else { this->camera_pos.x = 0.0F; @@ -319,17 +318,6 @@ bool TRunnerScreen::draw() { Vector3{xf - 0.5F, current.sw, zf + 0.5F}, color); } - // DrawModel(Model{.transform = TEMP_cube_model.transform * TEMP_matrix, - // .meshCount = TEMP_cube_model.meshCount, - // .materialCount = TEMP_cube_model.materialCount, - // .meshes = TEMP_cube_model.meshes, - // .materials = TEMP_cube_model.materials, - // .meshMaterial = TEMP_cube_model.meshMaterial, - // .boneCount = TEMP_cube_model.boneCount, - // .bones = TEMP_cube_model.bones, - // .bindPose = TEMP_cube_model.bindPose}, - // Vector3{0.0F, 0.0F, -4.0F}, 1.0F, WHITE); - for (auto &walker : walkers) { walker.draw(TEMP_cube_model); } diff --git a/src/walker.h b/src/walker.h index 6be013c..d05ea70 100644 --- a/src/walker.h +++ b/src/walker.h @@ -155,10 +155,8 @@ void Walker::update(float dt, const std::array &bbs, // body to target pos if ((flags & 3) == 2) { float diff = Vector3Distance(target_body_pos, body_pos); - body_pos = - Vector3Add(body_pos, Vector3Scale(Vector3Normalize(Vector3Subtract( - target_body_pos, body_pos)), - dt * BODY_TARGET_SPEED)); + body_pos = body_pos + Vector3Normalize(target_body_pos - body_pos) * + (dt * BODY_TARGET_SPEED); if (Vector3Distance(target_body_pos, body_pos) > diff) { flags &= ~3; body_pos = target_body_pos; @@ -179,42 +177,38 @@ void Walker::update(float dt, const std::array &bbs, Vector3 ideal_foot_pos; if ((flags & 0x18) == 0) { // Is nw. - ideal_foot_pos = Vector3Transform( - Vector3Normalize(Vector3{-1.0F, 0.0F, -1.0F}), rotationMatrix); + ideal_foot_pos = + rotationMatrix * Vector3Normalize(Vector3{-1.0F, 0.0F, -1.0F}); } else if ((flags & 0x18) == 0x8) { // Is ne. - ideal_foot_pos = Vector3Transform( - Vector3Normalize(Vector3{1.0F, 0.0F, -1.0F}), rotationMatrix); + ideal_foot_pos = + rotationMatrix * Vector3Normalize(Vector3{1.0F, 0.0F, -1.0F}); } else if ((flags & 0x18) == 0x10) { // Is sw. - ideal_foot_pos = Vector3Transform( - Vector3Normalize(Vector3{-1.0F, 0.0F, 1.0F}), rotationMatrix); + ideal_foot_pos = + rotationMatrix * Vector3Normalize(Vector3{-1.0F, 0.0F, 1.0F}); } else if ((flags & 0x18) == 0x18) { // Is se. - ideal_foot_pos = Vector3Transform( - Vector3Normalize(Vector3{1.0F, 0.0F, 1.0F}), rotationMatrix); + ideal_foot_pos = + rotationMatrix * Vector3Normalize(Vector3{1.0F, 0.0F, 1.0F}); } ideal_foot_pos = - Vector3Add(body_pos_same_y, - Vector3Scale(ideal_foot_pos, this->body_feet_radius)); + body_pos_same_y + (ideal_foot_pos * this->body_feet_radius); // Check if body is past threshold. if (Vector3Distance(ideal_foot_pos, leg_target) > FEET_RADIUS_PLACEMENT_CHECK_SCALE * this->feet_radius) { should_lift = true; - Vector3 diff = Vector3Subtract(this->target_body_pos, this->body_pos); + Vector3 diff = this->target_body_pos - this->body_pos; if (Vector3Length(diff) > 0.1F) { Vector3 dir = Vector3Normalize(diff); leg_target = - Vector3Add(ideal_foot_pos, - Vector3Scale(dir, this->feet_radius * - FEET_RADIUS_PLACEMENT_SCALE)); + ideal_foot_pos + + (dir * (this->feet_radius * FEET_RADIUS_PLACEMENT_SCALE)); } else { - Vector3 dir = - Vector3Normalize(Vector3Subtract(ideal_foot_pos, leg_target)); + Vector3 dir = Vector3Normalize(ideal_foot_pos - leg_target); leg_target = - Vector3Add(ideal_foot_pos, - Vector3Scale(dir, this->feet_radius * - FEET_RADIUS_PLACEMENT_SCALE)); + ideal_foot_pos + + (dir * (this->feet_radius * FEET_RADIUS_PLACEMENT_SCALE)); } // Get average .y of ground at target position. Ray downwards{.position = Vector3{leg_target.x, leg_target.y + 5.0F, @@ -244,10 +238,9 @@ void Walker::update(float dt, const std::array &bbs, // Moving horizontally. float prev_dist = Vector3Distance( leg_pos, Vector3{leg_target.x, leg_pos.y, leg_target.z}); - Vector3 dir = Vector3Normalize(Vector3Subtract( - Vector3{leg_target.x, leg_pos.y, leg_target.z}, leg_pos)); - leg_pos = - Vector3Add(leg_pos, Vector3Scale(dir, dt * FEET_HORIZ_MOVE_SPEED)); + Vector3 dir = Vector3Normalize( + Vector3{leg_target.x, leg_pos.y, leg_target.z} - leg_pos); + leg_pos = leg_pos + (dir * (dt * FEET_HORIZ_MOVE_SPEED)); if (Vector3Distance(leg_pos, Vector3{leg_target.x, leg_pos.y, leg_target.z}) >= prev_dist) { leg_pos.x = leg_target.x;