mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-09 20:08:57 +03:00
Switched to using jolt virtual character instead of jolt character
This commit is contained in:
@@ -27,9 +27,10 @@
|
||||
|
||||
#include <Jolt/Physics/Body/BodyCreationSettings.h>
|
||||
#include <Jolt/Physics/Body/BodyActivationListener.h>
|
||||
#include <Jolt/Physics/Character/Character.h>
|
||||
#include <Jolt/Physics/Character/CharacterVirtual.h>
|
||||
#include <Jolt/Physics/Collision/RayCast.h>
|
||||
#include <Jolt/Physics/Collision/CastResult.h>
|
||||
#include <Jolt/Core/TempAllocator.h>
|
||||
|
||||
#include <dependencies/JoltPhysics/Jolt/Physics/Collision/CollisionCollectorImpl.h>
|
||||
|
||||
@@ -245,7 +246,7 @@ namespace Nuake
|
||||
{
|
||||
DynamicWorld::DynamicWorld() : _stepCount(0)
|
||||
{
|
||||
_registeredCharacters = std::map<uint32_t, JPH::Character*>();
|
||||
_registeredCharacters = std::map<uint32_t, JPH::CharacterVirtual*>();
|
||||
|
||||
// Initialize Jolt Physics
|
||||
const uint32_t MaxBodies = 2048;
|
||||
@@ -329,13 +330,19 @@ namespace Nuake
|
||||
|
||||
void DynamicWorld::AddCharacterController(Ref<CharacterController> cc)
|
||||
{
|
||||
JPH::Ref<JPH::CharacterSettings> settings = new JPH::CharacterSettings();
|
||||
settings->mMaxSlopeAngle = JPH::DegreesToRadians(cc->MaxSlopeAngle);
|
||||
settings->mLayer = Layers::MOVING;
|
||||
settings->mFriction = cc->Friction;
|
||||
settings->mShape = GetJoltShape(cc->Shape);
|
||||
settings->mGravityFactor = 0.0f;
|
||||
JPH::Ref<JPH::CharacterVirtualSettings> settings = new JPH::CharacterVirtualSettings();
|
||||
//settings->mMaxSlopeAngle = JPH::DegreesToRadians(cc->MaxSlopeAngle);
|
||||
//settings->mLayer = Layers::MOVING;
|
||||
//settings->mFriction = cc->Friction;
|
||||
//settings->mShape = GetJoltShape(cc->Shape);
|
||||
//settings->mGravityFactor = 0.0f;
|
||||
settings->mSupportingVolume = JPH::Plane(JPH::Vec3::sAxisY(), -0.5f);
|
||||
settings->mMaxSlopeAngle = JPH::DegreesToRadians(cc->MaxSlopeAngle);
|
||||
settings->mMaxStrength = 10.0;
|
||||
settings->mCharacterPadding = 0.01f;
|
||||
settings->mPenetrationRecoverySpeed = 1.0f;
|
||||
settings->mPredictiveContactDistance = 0.01f;
|
||||
settings->mShape = GetJoltShape(cc->Shape);
|
||||
|
||||
auto& joltPosition = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z);
|
||||
|
||||
@@ -346,9 +353,9 @@ namespace Nuake
|
||||
//bodyRotation = glm::normalize(bodyRotation * Quat(yOffset));
|
||||
|
||||
const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w);
|
||||
JPH::Character* character = new JPH::Character(settings, joltPosition, joltRotation, cc->GetEntity().GetID() , _JoltPhysicsSystem.get());
|
||||
JPH::CharacterVirtual* character = new JPH::CharacterVirtual(settings, joltPosition, joltRotation, _JoltPhysicsSystem.get());
|
||||
|
||||
character->AddToPhysicsSystem(JPH::EActivation::Activate);
|
||||
//character->AddToPhysicsSystem(JPH::EActivation::Activate);
|
||||
|
||||
// To get the jolt character control from a scene entity.
|
||||
_registeredCharacters[cc->Owner.GetHandle()] = character;
|
||||
@@ -452,7 +459,7 @@ namespace Nuake
|
||||
{
|
||||
Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()};
|
||||
|
||||
JPH::Character* characterController = e.second;
|
||||
JPH::CharacterVirtual* characterController = e.second;
|
||||
JPH::Mat44 joltTransform = characterController->GetWorldTransform();
|
||||
const auto bodyRotation = characterController->GetRotation();
|
||||
|
||||
@@ -506,12 +513,30 @@ namespace Nuake
|
||||
// Step the world
|
||||
try
|
||||
{
|
||||
_JoltPhysicsSystem->Update(ts, collisionSteps, new JPH::TempAllocatorMalloc(), _JoltJobSystem);
|
||||
// TODO: Potential memory leak with new keyword.
|
||||
|
||||
JPH::CharacterVirtual::ExtendedUpdateSettings update_settings;
|
||||
update_settings.mStickToFloorStepDown = -JPH::Vec3(0, 1, 0) * update_settings.mStickToFloorStepDown.Length();
|
||||
update_settings.mWalkStairsStepUp = JPH::Vec3(0, 1, 0) * update_settings.mWalkStairsStepUp.Length();
|
||||
update_settings.mWalkStairsMinStepForward = 0.10;
|
||||
update_settings.mWalkStairsStepForwardTest = 0.15f;
|
||||
|
||||
for (auto& c : _registeredCharacters)
|
||||
{
|
||||
c.second->PostSimulation(0.05f);
|
||||
//c.second->PostSimulation(0.05f);
|
||||
|
||||
c.second->ExtendedUpdate(ts,
|
||||
-JPH::Vec3(0, 0, 0) * _JoltPhysicsSystem->GetGravity().Length(),
|
||||
update_settings,
|
||||
_JoltPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING),
|
||||
_JoltPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING),
|
||||
{ },
|
||||
{ },
|
||||
*(new JPH::TempAllocatorMalloc()));
|
||||
}
|
||||
|
||||
_JoltPhysicsSystem->Update(ts, collisionSteps, new JPH::TempAllocatorMalloc(), _JoltJobSystem);
|
||||
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -538,7 +563,7 @@ namespace Nuake
|
||||
{
|
||||
for (auto& character : _registeredCharacters)
|
||||
{
|
||||
character.second->RemoveFromPhysicsSystem();
|
||||
//character.second->RemoveFromPhysicsSystem();
|
||||
}
|
||||
|
||||
_registeredCharacters.clear();
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace JPH
|
||||
class BodyActivationListener;
|
||||
class BodyInterface;
|
||||
class Shape;
|
||||
class Character;
|
||||
class CharacterVirtual;
|
||||
|
||||
template<class T>
|
||||
class Ref;
|
||||
@@ -48,7 +48,7 @@ namespace Nuake
|
||||
BPLayerInterfaceImpl* _JoltBroadphaseLayerInterface;
|
||||
|
||||
std::vector<uint32_t> _registeredBodies;
|
||||
std::map<uint32_t, JPH::Character*> _registeredCharacters;
|
||||
std::map<uint32_t, JPH::CharacterVirtual*> _registeredCharacters;
|
||||
|
||||
public:
|
||||
DynamicWorld();
|
||||
|
||||
@@ -45,9 +45,9 @@ namespace Nuake
|
||||
m_Environement = CreateRef<Environment>();
|
||||
|
||||
// Adding systems - Order is important
|
||||
m_Systems.push_back(CreateRef<PhysicsSystem>(this));
|
||||
m_Systems.push_back(CreateRef<ScriptingSystem>(this));
|
||||
m_Systems.push_back(CreateRef<TransformSystem>(this));
|
||||
m_Systems.push_back(CreateRef<PhysicsSystem>(this));
|
||||
m_Systems.push_back(CreateRef<ParticleSystem>(this));
|
||||
|
||||
m_SceneRenderer = new SceneRenderer();
|
||||
@@ -105,7 +105,6 @@ namespace Nuake
|
||||
{
|
||||
for (auto& system : m_Systems)
|
||||
{
|
||||
Logger::Log("Init system");
|
||||
if (!system->Init())
|
||||
{
|
||||
return false;
|
||||
|
||||
@@ -315,23 +315,27 @@ namespace Nuake
|
||||
Entity entity = Entity({ e, m_Scene });
|
||||
auto [transformComponent, characterControllerComponent] = characterControllerView.get<TransformComponent, CharacterControllerComponent>(e);
|
||||
|
||||
Ref<Physics::PhysicShape> shape;
|
||||
if (entity.HasComponent<CapsuleColliderComponent>())
|
||||
{
|
||||
const auto& capsuleColliderComponent = entity.GetComponent<CapsuleColliderComponent>();
|
||||
auto& capsule = capsuleColliderComponent.Capsule;
|
||||
|
||||
const float friction = characterControllerComponent.Friction;
|
||||
const float maxSlopeAngle = characterControllerComponent.MaxSlopeAngle;
|
||||
auto characterController = CreateRef<Physics::CharacterController>(capsule, friction, maxSlopeAngle);
|
||||
characterController->SetEntity(entity); // Used to link back to the entity
|
||||
characterController->Position = transformComponent.GetGlobalPosition();
|
||||
characterController->Rotation = transformComponent.GetGlobalRotation();
|
||||
|
||||
characterControllerComponent.CharacterController = characterController;
|
||||
PhysicsManager::Get().RegisterCharacterController(characterControllerComponent.CharacterController);
|
||||
shape = capsuleColliderComponent.Capsule;
|
||||
}
|
||||
else if (entity.HasComponent<BoxColliderComponent>())
|
||||
{
|
||||
const auto& capsuleColliderComponent = entity.GetComponent<BoxColliderComponent>();
|
||||
shape = capsuleColliderComponent.Box;
|
||||
}
|
||||
|
||||
// TODO: Other types of collider supported for character controller?
|
||||
const float friction = characterControllerComponent.Friction;
|
||||
const float maxSlopeAngle = characterControllerComponent.MaxSlopeAngle;
|
||||
auto characterController = CreateRef<Physics::CharacterController>(shape, friction, maxSlopeAngle);
|
||||
characterController->SetEntity(entity); // Used to link back to the entity
|
||||
characterController->Position = transformComponent.GetGlobalPosition();
|
||||
characterController->Rotation = transformComponent.GetGlobalRotation();
|
||||
|
||||
characterControllerComponent.CharacterController = characterController;
|
||||
PhysicsManager::Get().RegisterCharacterController(characterControllerComponent.CharacterController);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user