jolt physics RAII

This commit is contained in:
Antoine Pilote
2023-10-02 01:36:43 -04:00
parent bd8cba07d0
commit c16cd3431b
4 changed files with 24 additions and 12 deletions

View File

@@ -249,7 +249,7 @@ namespace Nuake
{
DynamicWorld::DynamicWorld() : _stepCount(0)
{
_registeredCharacters = std::map<uint32_t, JPH::CharacterVirtual*>();
_registeredCharacters = std::map<uint32_t, Ref<JPH::CharacterVirtual>>();
// Initialize Jolt Physics
const uint32_t MaxBodies = 2048;
@@ -339,10 +339,15 @@ namespace Nuake
bodySettings.mMassPropertiesOverride.mMass = mass;
}
if (rb->GetEntity().GetID() == 0)
{
Logger::Log("Entity with ID 0 detected. Name: " + rb->GetEntity().GetComponent<NameComponent>().Name, "DEBUG");
}
bodySettings.mUserData = rb->GetEntity().GetID();
// Create the actual rigid body
JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr
_registeredBodies.push_back((uint32_t)body.GetIndexAndSequenceNumber());
_registeredBodies.push_back((uint32_t)body.GetIndex());
}
void DynamicWorld::AddGhostbody(Ref<GhostObject> gb)
@@ -364,7 +369,7 @@ namespace Nuake
const Quat& bodyRotation = cc->Rotation;
const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w);
auto character = new JPH::CharacterVirtual(settings, std::move(joltPosition), std::move(joltRotation), _JoltPhysicsSystem.get());
auto character = CreateRef<JPH::CharacterVirtual>(settings, std::move(joltPosition), std::move(joltRotation), _JoltPhysicsSystem.get());
// To get the jolt character control from a scene entity.
_registeredCharacters[cc->Owner.GetHandle()] = character;
@@ -468,7 +473,7 @@ namespace Nuake
{
Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()};
JPH::CharacterVirtual* characterController = e.second;
Ref<JPH::CharacterVirtual> characterController = e.second;
JPH::Mat44 joltTransform = characterController->GetWorldTransform();
const auto bodyRotation = characterController->GetRotation();
@@ -584,13 +589,17 @@ namespace Nuake
if (!_registeredBodies.empty())
{
_JoltBodyInterface->RemoveBodies(reinterpret_cast<JPH::BodyID*>(_registeredBodies.data()), _registeredBodies.size());
for (auto& body : _registeredBodies)
{
_JoltBodyInterface->RemoveBody(static_cast<JPH::BodyID>(body));
}
_registeredBodies.clear();
}
if (!_registeredCharacters.empty())
{
_registeredCharacters.clear();
}
}
@@ -664,6 +673,7 @@ namespace Nuake
break;
case RigidbodyShapes::MESH:
{
assert(true);
MeshShape* meshShape = (MeshShape*)shape.get();
const auto& mesh = meshShape->GetMesh();
const auto& vertices = mesh->GetVertices();

View File

@@ -42,13 +42,13 @@ namespace Nuake
Ref<JPH::PhysicsSystem> _JoltPhysicsSystem;
JPH::JobSystemThreadPool* _JoltJobSystem;
Scope<MyContactListener> _contactListener;
Scope<MyBodyActivationListener> _bodyActivationListener;
Ref<MyContactListener> _contactListener;
Ref<MyBodyActivationListener> _bodyActivationListener;
JPH::BodyInterface* _JoltBodyInterface;
BPLayerInterfaceImpl* _JoltBroadphaseLayerInterface;
std::vector<uint32_t> _registeredBodies;
std::map<uint32_t, JPH::CharacterVirtual*> _registeredCharacters;
std::map<uint32_t, Ref<JPH::CharacterVirtual>> _registeredCharacters;
public:
DynamicWorld();

View File

@@ -55,7 +55,7 @@ namespace Nuake
JPH::Factory::sInstance = new JPH::Factory();
JPH::RegisterTypes();
m_World = new Physics::DynamicWorld();
m_World = CreateRef<Physics::DynamicWorld>();
m_World->SetGravity(Vector3(0, -3, 0));
m_IsRunning = false;

View File

@@ -11,11 +11,12 @@ namespace Nuake
class PhysicsManager
{
private:
Physics::DynamicWorld* m_World;
Ref<Physics::DynamicWorld> m_World;
bool m_IsRunning = false;
bool m_DrawDebug = false;
static PhysicsManager* m_Instance;
public:
static PhysicsManager& Get()
{
@@ -23,11 +24,12 @@ namespace Nuake
return instance;
}
Physics::DynamicWorld* GetWorld() { return m_World; }
Ref<Physics::DynamicWorld> GetWorld() { return m_World; }
PhysicsManager() { if (!m_Instance) m_Instance = this; }
void SetDrawDebug(bool value) {
void SetDrawDebug(bool value)
{
m_DrawDebug = value;
}