-void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
- // determine displacement for each cardinal axis and move entity accordingly
- glm::vec3 min_disp(0.0f);
- glm::vec3 max_disp(0.0f);
- for (const WorldCollision &c : col) {
- if (!c.Blocks()) continue;
- glm::vec3 local_disp(c.normal * c.depth);
- // swap if neccessary (normal may point away from the entity)
- if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
- local_disp *= -1;
+void World::Update(Entity &entity, float dt) {
+ EntityState state(entity.GetState());
+
+ EntityDerivative a(CalculateStep(entity, state, 0.0f, EntityDerivative()));
+ EntityDerivative b(CalculateStep(entity, state, dt * 0.5f, a));
+ EntityDerivative c(CalculateStep(entity, state, dt * 0.5f, b));
+ EntityDerivative d(CalculateStep(entity, state, dt, c));
+
+ EntityDerivative f;
+ constexpr float sixth = 1.0f / 6.0f;
+ f.position = sixth * ((a.position + 2.0f * (b.position + c.position)) + d.position);
+ f.velocity = sixth * ((a.velocity + 2.0f * (b.velocity + c.velocity)) + d.velocity);
+
+ state.block_pos += f.position * dt;
+ state.velocity += f.velocity * dt;
+ state.AdjustPosition();
+
+ entity.SetState(state);
+}
+
+EntityDerivative World::CalculateStep(
+ const Entity &entity,
+ const EntityState &cur,
+ float dt,
+ const EntityDerivative &delta
+) {
+ EntityState next(cur);
+ next.block_pos += delta.position * dt;
+ next.velocity += delta.velocity * dt;
+ next.AdjustPosition();
+
+ EntityDerivative out;
+ out.position = next.velocity;
+ out.velocity = CalculateForce(entity, next); // by mass = 1kg
+ return out;
+}
+
+glm::vec3 World::CalculateForce(
+ const Entity &entity,
+ const EntityState &state
+) {
+ return ControlForce(entity, state) + CollisionForce(entity, state) + Gravity(entity, state);
+}
+
+glm::vec3 World::ControlForce(
+ const Entity &entity,
+ const EntityState &state
+) {
+ return entity.ControlForce(state);
+}
+
+namespace {
+
+std::vector<WorldCollision> col;
+
+}
+
+glm::vec3 World::CollisionForce(
+ const Entity &entity,
+ const EntityState &state
+) {
+ col.clear();
+ if (entity.WorldCollidable() && Intersection(entity, state, col)) {
+ // determine displacement for each cardinal axis and move entity accordingly
+ glm::vec3 min_pen(0.0f);
+ glm::vec3 max_pen(0.0f);
+ for (const WorldCollision &c : col) {
+ if (!c.Blocks()) continue;
+ glm::vec3 local_pen(c.normal * c.depth);
+ // swap if neccessary (normal may point away from the entity)
+ if (dot(c.normal, state.RelativePosition(c.ChunkPos()) - c.BlockCoords()) > 0) {
+ local_pen *= -1;
+ }
+ min_pen = min(min_pen, local_pen);
+ max_pen = max(max_pen, local_pen);