]> git.localhorst.tv Git - blobs.git/commitdiff
face direction
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 29 Nov 2017 21:26:07 +0000 (22:26 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 29 Nov 2017 21:26:07 +0000 (22:26 +0100)
src/app/states.cpp
src/creature/Situation.hpp
src/creature/creature.cpp
src/graphics/viewport.cpp
src/math/glm.hpp
src/world/Planet.hpp

index cfac2fa8450565d4ccf5b4403f3c3ce4e9cb385d..ecba340312dd84a6fbca76c791c8cd7531c575e2 100644 (file)
@@ -19,8 +19,8 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
 , assets(assets)
 , sim(sim)
 , cam(sim.Root())
-, cam_dist(10.0)
-, cam_tgt_dist(10.0)
+, cam_dist(5.0)
+, cam_tgt_dist(5.0)
 , cam_orient(PI * 0.125, 0.0, 0.0)
 , cam_dragging(false)
 , cp(assets)
index f29d9b08b96b0e5c07c4bb588e5a1f0cfa896928..6b2bac0500f4dcc958040ce4fd8192693af51365 100644 (file)
@@ -16,20 +16,30 @@ class Situation {
 
 public:
        struct State {
+               // position
                glm::dvec3 pos;
+               // velocity
                glm::dvec3 vel;
+               // face direction, normalized
+               glm::dvec3 dir;
                State(
                        const glm::dvec3 &pos = glm::dvec3(0.0),
-                       const glm::dvec3 &vel = glm::dvec3(0.0))
-               : pos(pos), vel(vel) { }
+                       const glm::dvec3 &vel = glm::dvec3(0.0),
+                       const glm::dvec3 &dir = glm::dvec3(0.0, 0.0, -1.0))
+               : pos(pos), vel(vel), dir(dir) { }
        };
        struct Derivative {
+               // velocity
                glm::dvec3 vel;
+               // acceleration
                glm::dvec3 acc;
+               // orientation adjust
+               glm::dvec3 turn;
                Derivative(
                        const glm::dvec3 &vel = glm::dvec3(0.0),
-                       const glm::dvec3 &acc = glm::dvec3(0.0))
-               : vel(vel), acc(acc) { }
+                       const glm::dvec3 &acc = glm::dvec3(0.0),
+                       const glm::dvec3 &turn = glm::dvec3(0.0))
+               : vel(vel), acc(acc), turn(turn) { }
        };
 
 public:
@@ -59,6 +69,9 @@ public:
        const glm::dvec3 &Velocity() const noexcept { return state.vel; }
        bool Moving() const noexcept { return glm::length2(state.vel) < 0.00000001; }
        void Move(const glm::dvec3 &dp) noexcept;
+
+       const glm::dvec3 &Heading() const noexcept { return state.dir; }
+
        void SetPlanetSurface(world::Planet &, int srf, const glm::dvec3 &pos) noexcept;
 
 public:
index 456e08102ebbb2d5cd45a7337a7f02ea508dd27f..3066331b13b76db3019a4ca59afe5da44cb2f3b8 100644 (file)
@@ -19,6 +19,7 @@
 #include <algorithm>
 #include <sstream>
 #include <glm/gtx/transform.hpp>
+#include <glm/gtx/vector_angle.hpp>
 
 #include <iostream>
 #include <glm/gtx/io.hpp>
@@ -159,10 +160,13 @@ void Creature::Tick(double dt) {
                Situation::Derivative d(Step(c, dt));
                Situation::Derivative f(
                        (1.0 / 6.0) * (a.vel + 2.0 * (b.vel + c.vel) + d.vel),
-                       (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc)
+                       (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc),
+                       (1.0 / 6.0) * (a.turn + 2.0 * (b.turn + c.turn) + d.turn)
                );
                state.pos += f.vel * dt;
                state.vel += f.acc * dt;
+               constexpr double turn_speed = 10.0;
+               state.dir = glm::normalize(state.dir + f.turn * turn_speed * dt);
                situation.SetState(state);
        }
 
@@ -182,20 +186,11 @@ void Creature::Tick(double dt) {
        }
        // if active goal can be interrupted, check priorities
        if (goals.size() > 1 && goals[0]->Interruptible()) {
-               Goal *old_top = &*goals[0];
                std::sort(goals.begin(), goals.end(), GoalCompare);
-               Goal *new_top = &*goals[0];
-               if (new_top != old_top) {
-                       std::cout << "[" << int(sim.Time()) << "s] " << name
-                               << " changing goal from " << old_top->Describe()
-                               << " to " << new_top->Describe() << std::endl;
-               }
        }
        goals[0]->Action();
        for (auto goal = goals.begin(); goal != goals.end();) {
                if ((*goal)->Complete()) {
-                       std::cout << "[" << int(sim.Time()) << "s] " << name
-                               << " complete goal: " << (*goal)->Describe() << std::endl;
                        goals.erase(goal);
                } else {
                        ++goal;
@@ -207,7 +202,12 @@ Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt)
        Situation::State s = situation.GetState();
        s.pos += ds.vel * dt;
        s.vel += ds.acc * dt;
-       return { s.vel, steering.Acceleration(s) };
+       s.dir = normalize(s.dir + ds.turn * dt);
+       return {
+               s.vel,
+               steering.Acceleration(s),
+               allzero(s.vel) ? glm::dvec3(0.0) : normalize(s.vel) - s.dir
+       };
 }
 
 glm::dmat4 Creature::LocalTransform() noexcept {
@@ -215,6 +215,8 @@ glm::dmat4 Creature::LocalTransform() noexcept {
        const double half_size = size * 0.5;
        const glm::dvec3 &pos = situation.Position();
        return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
+               * glm::dmat4(world::Planet::SurfaceOrientation(situation.Surface()))
+               * glm::rotate(glm::orientedAngle(glm::dvec3(0.0, 0.0, -1.0), situation.Heading(), glm::dvec3(0.0, 1.0, 0.0)), glm::dvec3(0.0, 1.0, 0.0))
                * glm::scale(glm::dvec3(half_size, half_size, half_size));
 }
 
index 6137eb2620d6f463445f45f80091088f62beb5ea..286fe517e47615342a4293d6ed2cc723658413fa 100644 (file)
@@ -132,11 +132,11 @@ Camera &Camera::Radial(const creature::Creature &c, double distance, const glm::
                up = world::Planet::SurfaceNormal(srf);
                dir =
                        world::Planet::SurfaceOrientation(srf)
-                       * glm::dmat3(glm::eulerAngleYX(angle.y, -angle.x))
+                       * glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x))
                        * dir;
        } else {
                up.y = 1.0;
-               dir = glm::dmat3(glm::eulerAngleYX(angle.y, -angle.x)) * dir;
+               dir = glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x)) * dir;
        }
        pos += up * (c.Size() * 0.5);
        up = glm::rotate(up, angle.z, glm::normalize(-dir));
index 80126aaa31908e7d309d68ac4fdbeba58da2013a..7bb321f712c20b4018c5b9507d7568627a53f2fd 100644 (file)
@@ -36,6 +36,17 @@ inline bool anynan(const T &v) noexcept {
        return glm::any(glm::isnan(v));
 }
 
+template<class Vec>
+inline Vec limit(const Vec &v, typename Vec::value_type max) noexcept {
+       typename Vec::value_type len2 = glm::length2(v);
+       typename Vec::value_type max2 = max * max;
+       if (len2 > max2) {
+               return glm::normalize(v) * max;
+       } else {
+               return v;
+       }
+}
+
 template<class T>
 inline T rgb2hsl(const T &rgb) {
        using Vec4 = glm::tvec4<typename T::value_type>;
index c109934d40e012989cfc6b806610df8821213327..7ac6b570256f7b256679b067ca8887489a4c3c59 100644 (file)
@@ -83,7 +83,7 @@ public:
        static glm::dmat3 SurfaceOrientation(int srf) noexcept {
                glm::dmat3 mat(0.0);
                mat[(srf + 0) % 3][0] = 1.0;
-               mat[(srf + 2) % 3][1] = srf < 3 ? 1.0 : -1.0;
+               mat[(srf + 2) % 3][1] = srf < 3 ? -1.0 : 1.0;
                mat[(srf + 1) % 3][2] = srf < 3 ? 1.0 : -1.0;
                return mat;
        }