From 43494fb5904a62919b64d4efb5320da6942f32c9 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Fri, 7 Jul 2023 01:34:26 -0400 Subject: [PATCH] Added Character controller ground check --- Nuake/src/Core/Physics/DynamicWorld.cpp | 26 ++++++++++++++++++++++--- Nuake/src/Core/Physics/DynamicWorld.h | 3 ++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 3d2e6a3b..7e8c15c2 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -297,6 +297,19 @@ namespace Nuake _registeredCharacters[cc->Owner.GetHandle()] = character; } + bool DynamicWorld::IsCharacterGrounded(const Entity& entity) + { + const uint32_t entityHandle = entity.GetHandle(); + if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) + { + auto& characterController = _registeredCharacters[entityHandle]; + const auto groundState = characterController->GetGroundState(); + return groundState == JPH::CharacterBase::EGroundState::OnGround; + } + + assert("Entity doesn't have a character controller component."); + } + RaycastResult DynamicWorld::Raycast(glm::vec3 from, glm::vec3 to) { Vector3 localNorm = glm::vec3(0,0,0); @@ -389,13 +402,12 @@ namespace Nuake { // Next step ++_stepCount; - SyncEntitiesTranforms(); - SyncCharactersTransforms(); + // If you take larger steps than 1 / 60th of a second you need to do multiple collision steps in order to keep the simulation stable. // Do 1 collision step per 1 / 60th of a second (round up). int collisionSteps = 1; - constexpr float minStepDuration = 1.0f / 60.0f; + constexpr float minStepDuration = 1.0f / 90.0f; if(ts > minStepDuration) { collisionSteps = static_cast(ts) / minStepDuration; @@ -406,6 +418,14 @@ namespace Nuake // Step the world _JoltPhysicsSystem->Update(ts, collisionSteps, subSteps, new JPH::TempAllocatorMalloc(), _JoltJobSystem); + + for (auto& c : _registeredCharacters) + { + c.second->PostSimulation(0.01); + } + + SyncEntitiesTranforms(); + SyncCharactersTransforms(); } diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index 5d0da2ee..cfa25a48 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -57,9 +57,10 @@ namespace Nuake void AddGhostbody(Ref gb); void AddCharacterController(Ref cc); - + bool IsCharacterGrounded(const Entity& entity); // This is going to be ugly. TODO: Find a better way that passing itself as a parameter void MoveAndSlideCharacterController(const Entity& entity, const Vector3 velocity); + RaycastResult Raycast(glm::vec3 from, glm::vec3 to); void StepSimulation(Timestep ts);