From b9af958c3f80a966bd31c622a45d0f3375bf2c3a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 29 Nov 2017 22:26:07 +0100 Subject: [PATCH] face direction --- src/app/states.cpp | 4 ++-- src/creature/Situation.hpp | 21 +++++++++++++++++---- src/creature/creature.cpp | 24 +++++++++++++----------- src/graphics/viewport.cpp | 4 ++-- src/math/glm.hpp | 11 +++++++++++ src/world/Planet.hpp | 2 +- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/app/states.cpp b/src/app/states.cpp index cfac2fa..ecba340 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -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) diff --git a/src/creature/Situation.hpp b/src/creature/Situation.hpp index f29d9b0..6b2bac0 100644 --- a/src/creature/Situation.hpp +++ b/src/creature/Situation.hpp @@ -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: diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 456e081..3066331 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -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)); } diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index 6137eb2..286fe51 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -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)); diff --git a/src/math/glm.hpp b/src/math/glm.hpp index 80126aa..7bb321f 100644 --- a/src/math/glm.hpp +++ b/src/math/glm.hpp @@ -36,6 +36,17 @@ inline bool anynan(const T &v) noexcept { return glm::any(glm::isnan(v)); } +template +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 inline T rgb2hsl(const T &rgb) { using Vec4 = glm::tvec4; diff --git a/src/world/Planet.hpp b/src/world/Planet.hpp index c109934..7ac6b57 100644 --- a/src/world/Planet.hpp +++ b/src/world/Planet.hpp @@ -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; } -- 2.39.2