From: Daniel Karbach Date: Mon, 19 Oct 2015 09:45:33 +0000 (+0200) Subject: penalty force collision response X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=8639a90bcbcd045c57cd489f02a25e0df4236deb;p=blank.git penalty force collision response feels kinda funny, let's see how it pans out --- diff --git a/src/client/net.cpp b/src/client/net.cpp index 73931f4..8a49304 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -380,11 +380,7 @@ void NetworkedInput::MergePlayerCorrection(uint16_t seq, const EntityState &corr while (entry != end) { replay.Velocity(entry->state.velocity); replay.TargetVelocity(entry->tgt_vel); - replay.Update(entry->delta_t); - if (GetWorld().Intersection(replay, col)) { - GetWorld().Resolve(replay, col); - col.clear(); - } + GetWorld().Update(replay, entry->delta_t); entry->state.chunk_pos = replay.GetState().chunk_pos; entry->state.block_pos = replay.GetState().block_pos; ++entry; diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index bf5fbe9..1ac9223 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -15,7 +15,6 @@ namespace blank { class DirectionalLighting; -struct EntityDerivative; class Shape; class Entity { @@ -80,20 +79,10 @@ public: bool Dead() const noexcept { return dead; } bool CanRemove() const noexcept { return dead && ref_count <= 0; } - void Update(int dt) noexcept; - void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept { if (model) model.Render(M, prog); } -private: - EntityDerivative CalculateStep( - const EntityState &cur, - float dt, - const EntityDerivative &prev - ) const noexcept; - glm::vec3 ControlForce(const EntityState &) const noexcept; - private: Instance model; diff --git a/src/world/World.hpp b/src/world/World.hpp index 848f801..c24ccf3 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -17,6 +17,7 @@ namespace blank { class BlockTypeRegistry; class EntityCollision; +struct EntityDerivative; class Viewport; class WorldCollision; @@ -61,8 +62,10 @@ public: EntityCollision &); /// check if given entity intersects with the world - bool Intersection(const Entity &e, std::vector &); - void Resolve(Entity &e, std::vector &); +// bool Intersection(const Entity &e, std::vector &col) { +// return Intersection(e, e.GetState(), col); +// } + bool Intersection(const Entity &e, const EntityState &, std::vector &); const BlockTypeRegistry &BlockTypes() noexcept { return block_type; } ChunkStore &Chunks() noexcept { return chunks; } @@ -87,6 +90,7 @@ public: const std::list &Entities() const noexcept { return entities; } void Update(int dt); + void Update(Entity &, float dt); void Render(Viewport &); @@ -94,6 +98,29 @@ private: using EntityHandle = std::list::iterator; EntityHandle RemoveEntity(EntityHandle &); + EntityDerivative CalculateStep( + const Entity &, + const EntityState &cur, + float dt, + const EntityDerivative &prev + ); + glm::vec3 CalculateForce( + const Entity &, + const EntityState &cur + ); + glm::vec3 ControlForce( + const Entity &, + const EntityState & + ); + glm::vec3 CollisionForce( + const Entity &, + const EntityState & + ); + glm::vec3 Gravity( + const Entity &, + const EntityState & + ); + private: Config config; diff --git a/src/world/WorldCollision.hpp b/src/world/WorldCollision.hpp index dd3f332..ac7dbbf 100644 --- a/src/world/WorldCollision.hpp +++ b/src/world/WorldCollision.hpp @@ -35,6 +35,8 @@ struct WorldCollision { bool Blocks() const noexcept { return chunk->Type(block).collide_block; } + const Chunk::Pos &ChunkPos() const noexcept { return GetChunk().Position(); } + glm::ivec3 BlockPos() const noexcept { return Chunk::ToPos(block); } glm::vec3 BlockCoords() const noexcept { return Chunk::ToCoords(block); } glm::mat4 BlockTransform() const noexcept { return GetChunk().ToTransform(BlockPos(), block); } diff --git a/src/world/world.cpp b/src/world/world.cpp index 8be2941..750cd2d 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -35,7 +35,6 @@ Entity::Entity() noexcept } - void Entity::Position(const glm::ivec3 &c, const glm::vec3 &b) noexcept { state.chunk_pos = c; state.block_pos = b; @@ -55,74 +54,6 @@ Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept { return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) }; } -namespace { - -glm::quat delta_rot(const glm::vec3 &av, float dt) { - glm::vec3 half(av * dt * 0.5f); - float mag = length(half); - if (mag > 0.0f) { - float smag = std::sin(mag) / mag; - return glm::quat(std::cos(mag), half * smag); - } else { - return glm::quat(1.0f, 0.0f, 0.0f, 0.0f); - } -} - -} - -void Entity::Update(int dt) noexcept { - float fdt = float(dt); - - // euler - //state.block_pos += state.velocity * fdt; - //state.velocity += ControlForce(state) * fdt; - //state.orient = delta_rot(state.ang_vel, fdt) * state.orient; - //state.AdjustPosition(); - - // RK4 - EntityDerivative a(CalculateStep(state, 0.0f, EntityDerivative())); - EntityDerivative b(CalculateStep(state, fdt * 0.5f, a)); - EntityDerivative c(CalculateStep(state, fdt * 0.5f, b)); - EntityDerivative d(CalculateStep(state, fdt, 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); - f.orient = sixth * ((a.orient + 2.0f * (b.orient + c.orient)) + d.orient); - - state.block_pos += f.position * fdt; - state.velocity += f.velocity * fdt; - state.orient = delta_rot(f.orient, fdt) * state.orient; - state.AdjustPosition(); -} - -EntityDerivative Entity::CalculateStep( - const EntityState &cur, - float dt, - const EntityDerivative &delta -) const noexcept { - EntityState next(cur); - next.block_pos += delta.position * dt; - next.velocity += delta.velocity * dt; - next.orient = delta_rot(cur.ang_vel, dt) * cur.orient; - next.AdjustPosition(); - - EntityDerivative out; - out.position = next.velocity; - out.velocity = ControlForce(next); // plus other forces and then by mass - return out; -} - -glm::vec3 Entity::ControlForce(const EntityState &cur) const noexcept { - constexpr float k = 1.0f; // spring constant - constexpr float b = 1.0f; // damper constant - constexpr float t = 0.01f; // 1/time constant - const glm::vec3 x(-tgt_vel); // endpoint displacement from equilibrium - const glm::vec3 v(cur.velocity); // relative velocity between endpoints - return (((-k) * x) - (b * v)) * t; // times mass = 1 -} - EntityState::EntityState() : chunk_pos(0) @@ -391,13 +322,13 @@ bool World::Intersection( return coll.entity; } -bool World::Intersection(const Entity &e, std::vector &col) { +bool World::Intersection(const Entity &e, const EntityState &s, std::vector &col) { AABB box = e.Bounds(); - Chunk::Pos reference = e.ChunkCoords(); - glm::mat4 M = e.Transform(reference); + Chunk::Pos reference = s.chunk_pos; + glm::mat4 M = s.Transform(reference); bool any = false; for (Chunk &cur_chunk : chunks) { - if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) { + if (manhattan_radius(cur_chunk.Position() - reference) > 1) { // chunk is not one of the 3x3x3 surrounding the entity // since there's no entity which can extent over 16 blocks, they can be skipped continue; @@ -410,22 +341,10 @@ bool World::Intersection(const Entity &e, std::vector &col) { } -namespace { - -std::vector col; - -} - void World::Update(int dt) { + float fdt(dt); for (Entity &entity : entities) { - entity.Update(dt); - } - for (Entity &entity : entities) { - col.clear(); - if (entity.WorldCollidable() && Intersection(entity, col)) { - // entity collides with the world - Resolve(entity, col); - } + Update(entity, fdt); } for (Player &player : players) { player.Update(dt); @@ -439,36 +358,126 @@ void World::Update(int dt) { } } -void World::Resolve(Entity &e, std::vector &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; - } - min_disp = min(min_disp, local_disp); - max_disp = max(max_disp, local_disp); +namespace { + +glm::quat delta_rot(const glm::vec3 &av, float dt) { + glm::vec3 half(av * dt * 0.5f); + float mag = length(half); + if (mag > 0.0f) { + float smag = std::sin(mag) / mag; + return glm::quat(std::cos(mag), half * smag); + } else { + return glm::quat(1.0f, 0.0f, 0.0f, 0.0f); } - // for each axis - // if only one direction is set, use that as the final - // if both directions are set, use average - glm::vec3 final_disp(0.0f); - for (int axis = 0; axis < 3; ++axis) { - if (std::abs(min_disp[axis]) > std::numeric_limits::epsilon()) { - if (std::abs(max_disp[axis]) > std::numeric_limits::epsilon()) { - final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f; - } else { - final_disp[axis] = min_disp[axis]; +} + +} + +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); + f.orient = sixth * ((a.orient + 2.0f * (b.orient + c.orient)) + d.orient); + + state.block_pos += f.position * dt; + state.velocity += f.velocity * dt; + state.orient = delta_rot(f.orient, dt) * state.orient; + 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.orient = delta_rot(cur.ang_vel, dt) * cur.orient; + next.AdjustPosition(); + + EntityDerivative out; + out.position = next.velocity; + out.velocity = CalculateForce(entity, next); // by mass = 1 + 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 +) { + constexpr float k = 1.0f; // spring constant + constexpr float b = 1.0f; // damper constant + constexpr float t = 0.01f; // 1/time constant + const glm::vec3 x(-entity.TargetVelocity()); // endpoint displacement from equilibrium + const glm::vec3 v(state.velocity); // relative velocity between endpoints + return (((-k) * x) - (b * v)) * t; // times mass = 1 +} + +namespace { + +std::vector 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; } - } else if (std::abs(max_disp[axis]) > std::numeric_limits::epsilon()) { - final_disp[axis] = max_disp[axis]; + min_pen = min(min_pen, local_pen); + max_pen = max(max_pen, local_pen); } + glm::vec3 penetration(min_pen + max_pen); + glm::vec3 normal(normalize(penetration) * -1.0f); + glm::vec3 normal_velocity(normal * dot(state.velocity, normal)); + // apply force proportional to penetration + // use velocity projected onto normal as damper + constexpr float k = 1.0f; // spring constant + constexpr float b = 1.0f; // damper constant + constexpr float t = 0.001f; // 1/time constant + const glm::vec3 x(penetration); // endpoint displacement from equilibrium + const glm::vec3 v(normal_velocity); // relative velocity between endpoints + return (((-k) * x) - (b * v)) * t; // times mass = 1 + } else { + return glm::vec3(0.0f); } - e.Position(e.Position() + final_disp); +} + +glm::vec3 World::Gravity( + const Entity &entity, + const EntityState &state +) { + return glm::vec3(0.0f); } World::EntityHandle World::RemoveEntity(EntityHandle &eh) {