]> git.seodisparate.com - jumpartifact.com_demo_0/commitdiff
Refactorings, cleanup
authorStephen Seo <seo.disparate@gmail.com>
Fri, 11 Aug 2023 10:30:02 +0000 (19:30 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 11 Aug 2023 10:31:03 +0000 (19:31 +0900)
src/3d_helpers.cc
src/3d_helpers.h
src/screen_trunner.cc
src/walker.h

index b0f67ec9978f4cae694fb7d5cb6d07bcf5a66854..9f413da12bafbb0d2d1e9aa80a2399b2065dfeb4 100644 (file)
@@ -164,3 +164,15 @@ std::optional<Vector3> 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);
+}
index 47695b6e5a8c52c887219560caedd30fb6889b9c..a407c01d73d6a139afc8f2db1df6792129e9d1b7 100644 (file)
@@ -32,5 +32,10 @@ extern std::optional<Vector3> 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
index 7d2d13222a4113bc5a70e950b73eeb5089762d5e..2e95e80b5c7eadd7cad24f98b293ea9212390bf2 100644 (file)
@@ -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);
   }
index 6be013c06e478694faad5818541653670f2e9dbd..d05ea70fd8c6e742afc6fe50b5902da714884080 100644 (file)
@@ -155,10 +155,8 @@ void Walker::update(float dt, const std::array<BoundingBox, BBCount> &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<BoundingBox, BBCount> &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<BoundingBox, BBCount> &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;