X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2Fworld.cpp;h=925046cbd0c9f4a2559822b34821498d362c2f9a;hb=0ab149c70b3f984b2cc0c7a122b4aa347bc5fd79;hp=e77192b277b46c8a592c9049f00601c59be1a08f;hpb=150d065f431d665326fd8028748c48a74ad956bb;p=blank.git diff --git a/src/world/world.cpp b/src/world/world.cpp index e77192b..925046c 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -80,7 +80,7 @@ void Entity::UnsetController() noexcept { glm::vec3 Entity::ControlForce(const EntityState &s) const noexcept { if (HasController()) { - return GetController().ControlForce(s); + return GetController().ControlForce(*this, s); } else { return -s.velocity; } @@ -153,6 +153,29 @@ EntityController::~EntityController() { } +bool EntityController::MaxOutForce( + glm::vec3 &out, + const glm::vec3 &add, + float max +) noexcept { + if (iszero(add) || any(isnan(add))) { + return false; + } + float current = iszero(out) ? 0.0f : length(out); + float remain = max - current; + if (remain <= 0.0f) { + return true; + } + float additional = length(add); + if (additional > remain) { + out += normalize(add) * remain; + return true; + } else { + out += add; + return false; + } +} + EntityState::EntityState() : chunk_pos(0) @@ -507,6 +530,10 @@ EntityDerivative World::CalculateStep( next.velocity += delta.velocity * dt; next.AdjustPosition(); + if (dot(next.velocity, next.velocity) > entity.MaxVelocity() * entity.MaxVelocity()) { + next.velocity = normalize(next.velocity) * entity.MaxVelocity(); + } + EntityDerivative out; out.position = next.velocity; out.velocity = CalculateForce(entity, next); // by mass = 1kg @@ -517,7 +544,12 @@ glm::vec3 World::CalculateForce( const Entity &entity, const EntityState &state ) { - return ControlForce(entity, state) + CollisionForce(entity, state) + Gravity(entity, state); + glm::vec3 force(ControlForce(entity, state) + CollisionForce(entity, state) + Gravity(entity, state)); + if (dot(force, force) > entity.MaxControlForce() * entity.MaxControlForce()) { + return normalize(force) * entity.MaxControlForce(); + } else { + return force; + } } glm::vec3 World::ControlForce(