]> git.localhorst.tv Git - blank.git/commitdiff
lerp random walk and make it change ang vel
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 28 Aug 2015 13:43:51 +0000 (15:43 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 28 Aug 2015 13:44:48 +0000 (15:44 +0200)
src/ai/RandomWalk.hpp
src/ai/ai.cpp

index 4d00f0bbdc53276b06df664fb87d23d6c1856ac0..8a0a19da51aaa898499d5cdba4202075bed5797f 100644 (file)
@@ -5,6 +5,8 @@
 
 #include "../rand/GaloisLFSR.hpp"
 
+#include <glm/glm.hpp>
+
 
 namespace blank {
 
@@ -18,9 +20,21 @@ public:
 
        void Update(int dt) override;
 
+private:
+       void Change() noexcept;
+
 private:
        GaloisLFSR random;
-       int time_left;
+
+       glm::vec3 start_vel;
+       glm::vec3 target_vel;
+
+       glm::vec3 start_rot;
+       glm::vec3 target_rot;
+
+       int switch_time;
+       float lerp_max;
+       float lerp_time;
 
 };
 
index 8cd4f1411abb1b9b21be99edb5e679a947c1c4e9..a1abfdc3cf73444102ed63e6210a00b7e2566f1e 100644 (file)
@@ -68,7 +68,13 @@ Controller::~Controller() {
 RandomWalk::RandomWalk(Entity &e, std::uint64_t seed) noexcept
 : Controller(e)
 , random(seed)
-, time_left(0) {
+, start_vel(e.Velocity())
+, target_vel(start_vel)
+, start_rot(e.AngularVelocity())
+, target_rot(start_rot)
+, switch_time(0)
+, lerp_max(1.0f)
+, lerp_time(0.0f) {
 
 }
 
@@ -77,45 +83,36 @@ RandomWalk::~RandomWalk() {
 }
 
 void RandomWalk::Update(int dt) {
-       time_left -= dt;
-       if (time_left > 0) return;
-       time_left += 2500 + (random.Next<unsigned short>() % 5000);
-
-       constexpr float move_vel = 0.0005f;
-
-       glm::vec3 new_vel = Controlled().Velocity();
-
-       switch (random.Next<unsigned char>() % 9) {
-               case 0:
-                       new_vel.x = -move_vel;
-                       break;
-               case 1:
-                       new_vel.x = 0.0f;
-                       break;
-               case 2:
-                       new_vel.x = move_vel;
-                       break;
-               case 3:
-                       new_vel.y = -move_vel;
-                       break;
-               case 4:
-                       new_vel.y = 0.0f;
-                       break;
-               case 5:
-                       new_vel.y = move_vel;
-                       break;
-               case 6:
-                       new_vel.z = -move_vel;
-                       break;
-               case 7:
-                       new_vel.z = 0.0f;
-                       break;
-               case 8:
-                       new_vel.z = move_vel;
-                       break;
+       switch_time -= dt;
+       lerp_time -= dt;
+       if (switch_time < 0) {
+               switch_time += 2500 + (random.Next<unsigned short>() % 5000);
+               lerp_max = 1500 + (random.Next<unsigned short>() % 1000);
+               lerp_time = lerp_max;
+               Change();
+       } else if (lerp_time > 0) {
+               float a = std::min(lerp_time / lerp_max, 1.0f);
+               Controlled().Velocity(mix(target_vel, start_vel, a));
+               Controlled().AngularVelocity(mix(target_rot, start_rot, a));
+       } else {
+               Controlled().Velocity(target_vel);
+               Controlled().AngularVelocity(target_rot);
        }
+}
+
+void RandomWalk::Change() noexcept {
+       start_vel = target_vel;
+       start_rot = target_rot;
+
+       constexpr float base = 0.000001f;
+
+       target_vel.x = base * (random.Next<short>() % 1024);
+       target_vel.y = base * (random.Next<short>() % 1024);
+       target_vel.z = base * (random.Next<short>() % 1024);
 
-       Controlled().Velocity(new_vel);
+       target_rot.x = base * (random.Next<short>() % 1024);
+       target_rot.y = base * (random.Next<short>() % 1024);
+       target_rot.z = base * (random.Next<short>() % 1024);
 }
 
 }