, 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)
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:
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:
#include <algorithm>
#include <sstream>
#include <glm/gtx/transform.hpp>
+#include <glm/gtx/vector_angle.hpp>
#include <iostream>
#include <glm/gtx/io.hpp>
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);
}
}
// 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;
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 {
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));
}
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));
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>;
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;
}