From 9581e9ac84ce6ff1d3a4bb8faa2f4f5178b3dfea Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 9 Mar 2016 17:51:26 +0100 Subject: [PATCH 1/2] 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; + } + } } } From 8936b3d6c3af82f8f2f0b018fb550048d274e84b Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 9 Mar 2016 19:45:26 +0100 Subject: [PATCH 2/2] Probably fixed all the jittering, won't need the jitter guard anymore. --- resources/Schema/Components/Collidable.xml | 1 - resources/Schema/Components/Collidable.xsd | 5 ----- src/Engine/Collision/Collision.cpp | 1 + src/Engine/Collision/CollisionSystem.cpp | 6 ++---- src/Game/Systems/PlayerMovementSystem.cpp | 12 ++++-------- 5 files changed, 7 insertions(+), 18 deletions(-) diff --git a/resources/Schema/Components/Collidable.xml b/resources/Schema/Components/Collidable.xml index 7bd60939..9046ea99 100644 --- a/resources/Schema/Components/Collidable.xml +++ b/resources/Schema/Components/Collidable.xml @@ -1,4 +1,3 @@ - true \ No newline at end of file diff --git a/resources/Schema/Components/Collidable.xsd b/resources/Schema/Components/Collidable.xsd index 2dd49a20..93f7ac24 100644 --- a/resources/Schema/Components/Collidable.xsd +++ b/resources/Schema/Components/Collidable.xsd @@ -7,10 +7,5 @@ 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/Collision.cpp b/src/Engine/Collision/Collision.cpp index 68d99d92..31768efd 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -470,6 +470,7 @@ bool AABBvsTriangle(const AABB& box, } glm::vec3 cornerResolution = (1+t) * diagonal; + cornerResolution = glm::dot(cornerResolution, triNormal) * triNormal; //Overwrite the smallest resolution if cornerResolution is smaller. float lenSq = glm::length2(cornerResolution); if (lenSq < resolveShortest.DistanceSq) { diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 8333a9ae..1c52209f 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); @@ -88,12 +88,11 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c glm::mat4 modelMatrix = Transform::ModelMatrix(boxB.Entity); glm::vec3 inOutVelocity = (glm::vec3)cPhysics["Velocity"]; - bool notMovingxz = glm::all(glm::lessThan(glm::abs(glm::vec2(inOutVelocity.x, inOutVelocity.z)), glm::vec2(0.01f))) && prevPosIt != m_PrevPositions.end(); bool isOnGround = (bool)cPhysics["IsOnGround"]; 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"] += jitterGuard && notMovingxz ? prevPosIt->second - boxA.Origin() : resolutionVector; + (glm::vec3&)cTransform["Position"] += resolutionVector; boxA = *Collision::EntityAbsoluteAABB(entity); cPhysics["Velocity"] = inOutVelocity; if (isOnGround) { @@ -118,7 +117,6 @@ 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 b0ad37ea..743a8af2 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -273,14 +273,10 @@ 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; - } + if (controller->CrouchingLastFrame() && isOnGround) { + // The collision should resolve this anyway, but + // this is more reliable, since the box gets larger. + ((glm::vec3&)cTransform["Position"]).y += 0.3f; } } }