+ TickState(dt);
+ TickStats(dt);
+ TickBrain(dt);
+}
+
+void Creature::TickState(double dt) {
+ steering.MaxSpeed(Dexerty());
+ steering.MaxForce(Strength());
+ Situation::State state(situation.GetState());
+ Situation::Derivative a(Step(Situation::Derivative(), 0.0));
+ Situation::Derivative b(Step(a, dt * 0.5));
+ Situation::Derivative c(Step(b, dt * 0.5));
+ 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)
+ );
+ state.pos += f.vel * dt;
+ state.vel += f.acc * dt;
+ situation.EnforceConstraints(state);
+ if (length2(state.vel) > 0.000001) {
+ glm::dvec3 nvel(normalize(state.vel));
+ double ang = angle(nvel, state.dir);
+ double turn_rate = PI * 0.75 * dt;
+ if (ang < turn_rate) {
+ state.dir = normalize(state.vel);
+ } else if (std::abs(ang - PI) < 0.001) {
+ state.dir = rotate(state.dir, turn_rate, world::Planet::SurfaceNormal(situation.Surface()));
+ } else {
+ state.dir = rotate(state.dir, turn_rate, normalize(cross(state.dir, nvel)));
+ }
+ }
+ situation.SetState(state);
+ stats.Exhaustion().Add(length(f.acc) * Mass() / Stamina() * 0.5 * dt);
+}
+
+Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
+ Situation::State s = situation.GetState();
+ s.pos += ds.vel * dt;
+ s.vel += ds.acc * dt;
+ glm::dvec3 force(steering.Force(s));
+ // gravity = antinormal * mass * Gm / r²
+ double elevation = s.pos[(situation.Surface() + 2) % 3];
+ glm::dvec3 normal(world::Planet::SurfaceNormal(situation.Surface()));
+ force += glm::dvec3(
+ -normal
+ * Mass() * situation.GetPlanet().GravitationalParameter()
+ / (elevation * elevation));
+ // if net force is applied and in contact with surface
+ if (!allzero(force) && std::abs(std::abs(elevation) - situation.GetPlanet().Radius()) < 0.001) {
+ // apply friction = -|normal force| * tangential force * coefficient
+ glm::dvec3 fn(normal * dot(force, normal));
+ glm::dvec3 ft(force - fn);
+ double u = 0.4;
+ glm::dvec3 friction(-length(fn) * ft * u);
+ force += friction;
+ }
+ return {
+ s.vel,
+ force / Mass()
+ };
+}