Files
CosmicEngine/src/Engine/Core/Physics/CTangibleWorld.cpp

50 lines
1.7 KiB
C++
Raw Normal View History

#include "CTangibleWorld.hpp"
#include "Jolt/Core/Memory.h"
#include "Jolt/RegisterTypes.h"
#include <memory>
#include "../Scene/CScene.hpp"
#include "../Component/Rigidbody/CRigidBody.hpp"
namespace CosmicCore {
CTangibleWorld::~CTangibleWorld()
{
}
void CTangibleWorld::initWorld()
{
JPH::RegisterDefaultAllocator();
JPH::Factory::sInstance = new JPH::Factory();
JPH::RegisterTypes();
m_tempAllocator = std::make_unique<JPH::TempAllocatorImpl>(10 * 1024 * 1024);
m_jobSystem = std::make_unique<JPH::JobSystemThreadPool>(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsBarriers, JPH::thread::hardware_concurrency() - 1);
m_physicsSystem.Init(cMaxBodies, cNumBodyMutexes, cMaxBodyPairs, cMaxContactConstraints, m_broadPhaseLayer, m_objectVsBroadPhase, m_objectLayerPairFilter);
}
void CTangibleWorld::updateWorld(float deltaTime){
m_physicsSystem.Update(deltaTime, 1, m_tempAllocator.get(), m_jobSystem.get());
auto& registry = getScene()->getECManager(); // accès via scene
auto view = registry.registry.view<CRigidBody>();
for (auto entity : view) {
auto& rb = registry.registry.get<CRigidBody>(entity);
// ne sync que les bodies dynamiques et kinématiques
if (rb.getBodyType() == EBodyType::Static) continue;
registry.registry.get<CRigidBody>(entity).syncTransform();
}
}
void CTangibleWorld::destroy(){
getScene()->getECManager().registry.view<CRigidBody>().each([](CRigidBody& rb) {
rb.destroy();
});
JPH::UnregisterTypes();
// Destroy the factory
delete JPH::Factory::sInstance;
JPH::Factory::sInstance = nullptr;
}
}