]> git.seodisparate.com - jumpartifact.com_demo_0/commitdiff
Impl. roaming walkers
authorStephen Seo <seo.disparate@gmail.com>
Fri, 11 Aug 2023 03:38:55 +0000 (12:38 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 11 Aug 2023 03:38:55 +0000 (12:38 +0900)
src/3d_helpers.cc
src/3d_helpers.h
src/common_constants.h [new file with mode: 0644]
src/screen_trunner.cc
src/screen_trunner.h
src/walker.cc
src/walker.h

index ec4f3ea98467774e2d8f64bf4dd196dcc0007e5c..b0f67ec9978f4cae694fb7d5cb6d07bcf5a66854 100644 (file)
@@ -160,3 +160,7 @@ std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane) {
                  ray.position.y + ray.direction.y * amount,
                  ray.position.z + ray.direction.z * amount};
 }
+
+Vector3 operator+(const Vector3 &a, const Vector3 &b) {
+  return Vector3{a.x + b.x, a.y + b.y, a.z + b.z};
+}
index 88b0021f45c35ff06ceba4a3f88ce88879fc9c1c..47695b6e5a8c52c887219560caedd30fb6889b9c 100644 (file)
@@ -31,4 +31,6 @@ extern std::optional<Vector3> ray_to_plane(const Ray &ray, const Ray &plane);
 // weirdness regarding column-major matrices.
 // extern Vector4 operator*(const Matrix &m, const Vector4 &v);
 
+extern Vector3 operator+(const Vector3 &a, const Vector3 &b);
+
 #endif
diff --git a/src/common_constants.h b/src/common_constants.h
new file mode 100644 (file)
index 0000000..47e0c0f
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef JUMPARTIFACT_DOT_COM_DEMO_0_COMMON_CONSTANTS_H_
+#define JUMPARTIFACT_DOT_COM_DEMO_0_COMMON_CONSTANTS_H_
+
+constexpr unsigned int SURFACE_UNIT_WIDTH = 51;
+constexpr unsigned int SURFACE_UNIT_HEIGHT = 51;
+
+constexpr float SURFACE_X_OFFSET = (float)SURFACE_UNIT_WIDTH / 2.0F - 0.5F;
+constexpr float SURFACE_Y_OFFSET = (float)SURFACE_UNIT_HEIGHT / 2.0F - 0.5F;
+
+#endif
index c652728004166b7b82900cc781e9a0321385cf8f..7d2d13222a4113bc5a70e950b73eeb5089762d5e 100644 (file)
@@ -20,7 +20,21 @@ TRunnerScreen::TRunnerScreen(std::weak_ptr<ScreenStack> stack)
     : Screen(stack),
       surface(),
       surface_bbs(),
-      walker(),
+      walkers{Walker{(float)(SURFACE_UNIT_WIDTH / 4) - SURFACE_X_OFFSET,
+                     (float)(SURFACE_UNIT_HEIGHT / 4) - SURFACE_Y_OFFSET, true},
+
+              Walker{(float)((SURFACE_UNIT_WIDTH / 4) * 3) - SURFACE_X_OFFSET,
+                     (float)(SURFACE_UNIT_HEIGHT / 4) - SURFACE_Y_OFFSET, true},
+
+              Walker{(float)(SURFACE_UNIT_WIDTH / 4) - SURFACE_X_OFFSET,
+                     (float)((SURFACE_UNIT_HEIGHT / 4) * 3) - SURFACE_Y_OFFSET,
+                     true},
+
+              Walker{(float)((SURFACE_UNIT_WIDTH / 4) * 3) - SURFACE_X_OFFSET,
+                     (float)((SURFACE_UNIT_HEIGHT / 4) * 3) - SURFACE_Y_OFFSET,
+                     true}
+
+      },
       camera{Vector3{0.0F, 1.0F, 0.5F}, Vector3{0.0F, 0.0F, 0.0F},
              Vector3{0.0F, 1.0F, 0.0F}, 80.0F, CAMERA_PERSPECTIVE},
       flags(),
@@ -241,7 +255,6 @@ bool TRunnerScreen::update(float dt) {
         this->camera_target.y =
             (current.nw + current.ne + current.sw + current.se) / 4.0F;
         this->camera_target.z = zf;
-        this->walker.set_body_pos(this->camera_target);
         if (idx != SURFACE_UNIT_WIDTH / 2 +
                        (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) {
           this->camera_pos = Vector3Add(
@@ -273,7 +286,9 @@ bool TRunnerScreen::update(float dt) {
 
   camera_to_targets(dt);
 
-  walker.update(dt, surface_bbs, SURFACE_UNIT_WIDTH, SURFACE_UNIT_HEIGHT);
+  for (auto &walker : walkers) {
+    walker.update(dt, surface_bbs, SURFACE_UNIT_WIDTH, SURFACE_UNIT_HEIGHT);
+  }
 
   return false;
 }
@@ -315,7 +330,9 @@ bool TRunnerScreen::draw() {
   //                 .bindPose = TEMP_cube_model.bindPose},
   //           Vector3{0.0F, 0.0F, -4.0F}, 1.0F, WHITE);
 
-  walker.draw(TEMP_cube_model);
+  for (auto &walker : walkers) {
+    walker.draw(TEMP_cube_model);
+  }
 
   // TODO DEBUG
   DrawLine3D(Vector3{0.0F, 3.0F, 0.0F}, mouse_hit, BLACK);
index 24e6c234b0c0cf814827a39741734fe8a4e4476a..d375309372de2071545698b10661df046115db46 100644 (file)
 #include <raylib.h>
 
 // local includes
+#include "common_constants.h"
 #include "walker.h"
 
 constexpr float POS_VALUE_INC_RATE = 0.2F;
 constexpr float CAMERA_UPDATE_RATE = 1.0F;
 
-constexpr unsigned int SURFACE_UNIT_WIDTH = 51;
-constexpr unsigned int SURFACE_UNIT_HEIGHT = 51;
-
-constexpr float SURFACE_X_OFFSET = (float)SURFACE_UNIT_WIDTH / 2.0F - 0.5F;
-constexpr float SURFACE_Y_OFFSET = (float)SURFACE_UNIT_HEIGHT / 2.0F - 0.5F;
-
 constexpr float SURFACE_HEIGHT_INTERVAL = 0.7F;
 
 class TRunnerScreen : public Screen {
@@ -61,7 +56,7 @@ class TRunnerScreen : public Screen {
       surface;
   std::array<BoundingBox, SURFACE_UNIT_WIDTH * SURFACE_UNIT_HEIGHT> surface_bbs;
 
-  Walker walker;
+  std::array<Walker, 4> walkers;
 
   Camera3D camera;
   std::bitset<64> flags;
index 57bba25f9bb3823b2b8d583201d93ed248ec192e..357670d9e0e6386f74f3fb709b7ef15d94eacb88 100644 (file)
 #include "3d_helpers.h"
 #include "ems.h"
 
-Walker::Walker(float body_height, float body_feet_radius, float feet_radius)
-    : body_pos{0.0F, body_height, 0.0F},
-      target_body_pos{0.0F, body_height, 0.0F},
+Walker::Walker(float x, float z, bool auto_roaming, float body_height,
+               float body_feet_radius, float feet_radius)
+    : body_pos{x, body_height, z},
+      target_body_pos{x, body_height, z},
       leg_nw(),
       leg_ne(),
       leg_sw(),
@@ -32,31 +33,43 @@ Walker::Walker(float body_height, float body_feet_radius, float feet_radius)
       lift_start_y(0.0F),
       rotation(0.0F),
       target_rotation(0.0F),
-      body_idle_move_timer(0.0F) {
+      body_idle_move_timer(0.0F),
+      roaming_time(5.0F),
+      roaming_timer(0.0F) {
+  flags |= auto_roaming ? 4 : 0;
+  roaming_time =
+      call_js_get_random() * ROAMING_WAIT_VARIANCE + ROAMING_WAIT_AMOUNT;
+
   const Vector3 nw = Vector3Normalize(Vector3{-1.0F, 0.0F, -1.0F});
   const Vector3 ne = Vector3Normalize(Vector3{1.0F, 0.0F, -1.0F});
   const Vector3 sw = Vector3Normalize(Vector3{-1.0F, 0.0F, 1.0F});
   const Vector3 se = Vector3Normalize(Vector3{1.0F, 0.0F, 1.0F});
 
-  leg_nw = Vector3Add(
+  const Vector3 offset{x, 0.0F, z};
+
+  leg_nw =
+      offset +
       Vector3{(call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV, 0.0F,
-              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV},
-      Vector3Scale(nw, body_feet_radius));
+              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV} +
+      Vector3Scale(nw, body_feet_radius);
   target_leg_nw = leg_nw;
-  leg_ne = Vector3Add(
+  leg_ne =
+      offset +
       Vector3{(call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV, 0.0F,
-              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV},
-      Vector3Scale(ne, body_feet_radius));
+              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV} +
+      Vector3Scale(ne, body_feet_radius);
   target_leg_ne = leg_ne;
-  leg_sw = Vector3Add(
+  leg_sw =
+      offset +
       Vector3{(call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV, 0.0F,
-              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV},
-      Vector3Scale(sw, body_feet_radius));
+              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV} +
+      Vector3Scale(sw, body_feet_radius);
   target_leg_sw = leg_sw;
-  leg_se = Vector3Add(
+  leg_se =
+      offset +
       Vector3{(call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV, 0.0F,
-              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV},
-      Vector3Scale(se, body_feet_radius));
+              (call_js_get_random() - 0.5F) / FEET_INIT_POS_VARIANCE_DIV} +
+      Vector3Scale(se, body_feet_radius);
   target_leg_se = leg_se;
 }
 
index a0016c86cb96c2247ae573f9f15a8f827d7b59cf..850cdd1486380c2b3f5383298799db7befc4b082 100644 (file)
@@ -13,6 +13,8 @@
 
 // local includes
 #include "3d_helpers.h"
+#include "common_constants.h"
+#include "ems.h"
 
 constexpr float FEET_RADIUS_PLACEMENT_CHECK_SCALE = 1.0F;
 constexpr float FEET_RADIUS_PLACEMENT_SCALE = 0.9F;
@@ -25,11 +27,13 @@ constexpr float FEET_INIT_POS_VARIANCE_DIV = 3.0F;
 constexpr float BODY_ROTATION_SPEED = 1.0F;
 constexpr float BODY_IDLE_TIMER_RATE = 1.0F;
 constexpr float BODY_IDLE_MOVE_AMOUNT = 0.2F;
+constexpr float ROAMING_WAIT_AMOUNT = 2.0F;
+constexpr float ROAMING_WAIT_VARIANCE = 7.0F;
 
 class Walker {
  public:
-  Walker(float body_height = 2.0F, float body_feet_radius = 1.7F,
-         float feet_radius = 1.5F);
+  Walker(float x, float z, bool auto_roaming, float body_height = 2.0F,
+         float body_feet_radius = 1.7F, float feet_radius = 1.5F);
 
   template <typename TBBS>
   void update(float dt, const TBBS &bbs, unsigned int width,
@@ -58,6 +62,7 @@ class Walker {
   // ???? ??00 - body stopped
   // ???? ??01 - rotating to move
   // ???? ??10 - moving
+  // ???? ?1?? - auto roaming
   unsigned int flags;
 
   const float body_height;
@@ -67,11 +72,36 @@ class Walker {
   float rotation;
   float target_rotation;
   float body_idle_move_timer;
+  float roaming_time;
+  float roaming_timer;
 };
 
 template <typename TBBS>
 void Walker::update(float dt, const TBBS &bbs, unsigned int width,
                     unsigned int height) {
+  if ((flags & 4) != 0 && (flags & 3) == 0) {
+    roaming_timer += dt;
+    if (roaming_timer > roaming_time) {
+      roaming_timer = 0.0F;
+      roaming_time =
+          call_js_get_random() * ROAMING_WAIT_VARIANCE + ROAMING_WAIT_AMOUNT;
+      unsigned int idx = call_js_get_random() * (float)bbs.size();
+      float x = (float)(idx % width) - SURFACE_X_OFFSET;
+      float y = 0.0F;
+      float z = (float)(idx / width) - SURFACE_Y_OFFSET;
+
+      Ray downwards{.position = Vector3{x, 20.0F, z},
+                    .direction = Vector3{0.0F, -1.0F, 0.0F}};
+      for (const auto &bb : bbs) {
+        if (GetRayCollisionBox(downwards, bb).hit) {
+          y = (bb.min.y + bb.max.y) / 2.0F;
+        }
+      }
+
+      set_body_pos(Vector3{x, y, z});
+    }
+  }
+
   const auto initialized_setup_fn = [&bbs](Vector3 &leg, Vector3 &leg_target) {
     Ray downwards{.position = leg, .direction = Vector3{0.0F, -1.0F, 0.0F}};
     for (const auto &bb : bbs) {