From 9581e9ac84ce6ff1d3a4bb8faa2f4f5178b3dfea Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 9 Mar 2016 17:51:26 +0100 Subject: [PATCH] Don't set the player to previous position when uncrouching. --- include/Engine/Input/FirstPersonInputController.h | 3 +++ resources/Schema/Components/Collidable.xml | 1 + resources/Schema/Components/Collidable.xsd | 5 +++++ src/Engine/Collision/CollisionSystem.cpp | 5 +++-- src/Game/Systems/PlayerMovementSystem.cpp | 9 +++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h index 00f36ac7..f91db610 100644 --- a/include/Engine/Input/FirstPersonInputController.h +++ b/include/Engine/Input/FirstPersonInputController.h @@ -18,6 +18,7 @@ public: virtual const glm::vec3 Rotation() const { return m_Rotation; } virtual bool Jumping() const { return m_Jumping; } virtual bool Crouching() const { return m_Crouching; } + virtual bool CrouchingLastFrame() const { return m_CrouchingLastFrame; } virtual bool DoubleJumping() const { return m_DoubleJumping; } virtual void SetDoubleJumping(bool isDoubleJumping) { m_DoubleJumping = isDoubleJumping; @@ -43,6 +44,7 @@ protected: bool m_Jumping = false; bool m_DoubleJumping = false; bool m_Crouching = false; + bool m_CrouchingLastFrame = false; //assault dash membervariables - needed to calculate the doubletap- and dashlogic double m_AssaultDashDoubleTapDeltaTime = 0.0; double m_DashEffectResetTimer = 0.0; @@ -82,6 +84,7 @@ void FirstPersonInputController::Reset() { m_Rotation = glm::vec3(0.f, 0.f, 0.f); m_Jumping = false; + m_CrouchingLastFrame = m_Crouching; } template diff --git a/resources/Schema/Components/Collidable.xml b/resources/Schema/Components/Collidable.xml index 9046ea99..7bd60939 100644 --- a/resources/Schema/Components/Collidable.xml +++ b/resources/Schema/Components/Collidable.xml @@ -1,3 +1,4 @@ + true \ No newline at end of file diff --git a/resources/Schema/Components/Collidable.xsd b/resources/Schema/Components/Collidable.xsd index 93f7ac24..2dd49a20 100644 --- a/resources/Schema/Components/Collidable.xsd +++ b/resources/Schema/Components/Collidable.xsd @@ -7,5 +7,10 @@ Needs a Model or AABB component to work, uses AABB if both are attached. + + + + + \ No newline at end of file diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 53cabfac..8333a9ae 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -66,7 +66,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c } } } - + bool& jitterGuard = (bool&)entity["Collidable"]["JitterGuard"]; // Collide against octree items m_OctreeResult.clear(); m_Octree->ObjectsInSameRegion(*boundingBox, m_OctreeResult); @@ -93,7 +93,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c float verticalStepHeight = (float)(double)cPhysics["VerticalStepHeight"]; if (Collision::AABBvsTriangles(boxA, model->Vertices(), model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) { //Move the position to previous position if it is not moving in the xz-plane, else resolve with the resolution vector. - (glm::vec3&)cTransform["Position"] += notMovingxz ? prevPosIt->second - boxA.Origin() : resolutionVector; + (glm::vec3&)cTransform["Position"] += jitterGuard && notMovingxz ? prevPosIt->second - boxA.Origin() : resolutionVector; boxA = *Collision::EntityAbsoluteAABB(entity); cPhysics["Velocity"] = inOutVelocity; if (isOnGround) { @@ -118,6 +118,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c (bool)cPhysics["IsOnGround"] = false; } + jitterGuard = true; m_PrevPositions[entity] = boxA.Origin(); } } diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index b927856a..b0ad37ea 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -273,6 +273,15 @@ void PlayerMovementSystem::updateMovementControllers(double dt) size = glm::vec3(1.f, 1.f, 1.f); } else { size = glm::vec3(1.f, 1.6f, 1.f); + if (controller->CrouchingLastFrame() && player.HasComponent("Collidable")) { + // Disable jitter guard so player doesn't get stuck in the ground. + (bool&)player["Collidable"]["JitterGuard"] = false; + if (isOnGround) { + // The collision should resolve this anyway, but + // this is more reliable, since the box gets larger. + ((glm::vec3&)cTransform["Position"]).y += 0.3f; + } + } } }