Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the crash while deleting object and exiting #219

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ namespace Pilot
{
if (m_physics_actor)
{
g_runtime_global_context.m_legacy_physics_system->removePhyicsActor(m_physics_actor);

const uint32_t body_id = m_physics_actor->getBodyID();

std::shared_ptr<PhysicsScene> physics_scene =
Expand All @@ -47,6 +45,7 @@ namespace Pilot

physics_scene->removeRigidBody(body_id);

g_runtime_global_context.m_legacy_physics_system->removePhyicsActor(m_physics_actor);
m_physics_actor = nullptr;
}
}
Expand Down
5 changes: 3 additions & 2 deletions engine/source/runtime/function/global/global_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ namespace Pilot

m_window_system.reset();

m_scene_manager.reset();

m_world_manager->clear();
m_world_manager.reset();

m_legacy_physics_system.reset();

m_physics_manager->clear();
m_physics_manager.reset();

m_input_system->clear();
m_input_system.reset();

m_asset_manager.reset();
Expand Down
2 changes: 0 additions & 2 deletions engine/source/runtime/function/global/global_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Pilot
class AssetManager;
class ConfigManager;
class WorldManager;
class SceneManager;
class RenderSystem;
class WindowSystem;

Expand All @@ -34,7 +33,6 @@ namespace Pilot
std::shared_ptr<AssetManager> m_asset_manager;
std::shared_ptr<ConfigManager> m_config_manager;
std::shared_ptr<WorldManager> m_world_manager;
std::shared_ptr<SceneManager> m_scene_manager;
std::shared_ptr<PhysicsSystem> m_legacy_physics_system;
std::shared_ptr<PhysicsManager> m_physics_manager;
std::shared_ptr<WindowSystem> m_window_system;
Expand Down
26 changes: 19 additions & 7 deletions engine/source/runtime/function/physics/physics_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "runtime/function/physics/physics_config.h"

#include "Jolt/Jolt.h"
#include "Jolt/RegisterTypes.h"

#include "Jolt/Core/Factory.h"
#include "Jolt/Core/JobSystem.h"
#include "Jolt/Core/JobSystemThreadPool.h"
#include "Jolt/Core/TempAllocator.h"
Expand All @@ -20,8 +22,6 @@
#include "Jolt/Physics/Collision/NarrowPhaseQuery.h"
#include "Jolt/Physics/Collision/RayCast.h"
#include "Jolt/Physics/Collision/Shape/BoxShape.h"
#include "Jolt/Physics/Collision/Shape/CapsuleShape.h"
#include "Jolt/Physics/Collision/Shape/SphereShape.h"
#include "Jolt/Physics/Collision/Shape/StaticCompoundShape.h"
#include "Jolt/Physics/Collision/ShapeCast.h"
#include "Jolt/Physics/PhysicsSystem.h"
Expand All @@ -33,6 +33,9 @@ namespace Pilot

static_assert(k_invalid_rigidbody_id == JPH::BodyID::cInvalidBodyID);

JPH::Factory::sInstance = new JPH::Factory();
JPH::RegisterTypes();

m_physics.m_jolt_physics_system = new JPH::PhysicsSystem();
m_physics.m_jolt_broad_phase_layer_interface = new BPLayerInterfaceImpl();

Expand Down Expand Up @@ -64,6 +67,9 @@ namespace Pilot
delete m_physics.m_jolt_job_system;
delete m_physics.m_temp_allocator;
delete m_physics.m_jolt_broad_phase_layer_interface;

delete JPH::Factory::sInstance;
JPH::Factory::sInstance = nullptr;
}

uint32_t PhysicsScene::createRigidBody(const Transform& global_transform,
Expand Down Expand Up @@ -151,15 +157,12 @@ namespace Pilot
}

body_interface.AddBody(jph_body->GetID(), JPH::EActivation::Activate);
LOG_INFO("Add Body: {}", jph_body->GetID().GetIndexAndSequenceNumber());

return jph_body->GetID().GetIndexAndSequenceNumber();
}

void PhysicsScene::removeRigidBody(uint32_t body_id)
{
JPH::BodyInterface& body_interface = m_physics.m_jolt_physics_system->GetBodyInterface();
body_interface.RemoveBody(JPH::BodyID(body_id));
}
void PhysicsScene::removeRigidBody(uint32_t body_id) { m_pending_remove_bodies.push_back(body_id); }

void PhysicsScene::tick(float delta_time)
{
Expand All @@ -170,6 +173,15 @@ namespace Pilot
m_physics.m_integration_substeps,
m_physics.m_temp_allocator,
m_physics.m_jolt_job_system);

JPH::BodyInterface& body_interface = m_physics.m_jolt_physics_system->GetBodyInterface();
for (uint32_t body_id : m_pending_remove_bodies)
{
LOG_INFO("Remove Body {}", body_id)
body_interface.RemoveBody(JPH::BodyID(body_id));
body_interface.DestroyBody(JPH::BodyID(body_id));
}
m_pending_remove_bodies.clear();
}

bool PhysicsScene::raycast(Vector3 ray_origin,
Expand Down
2 changes: 2 additions & 0 deletions engine/source/runtime/function/physics/physics_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ namespace Pilot
JoltPhysics m_physics;

PhysicsConfig m_config;

std::vector<uint32_t> m_pending_remove_bodies;
};
} // namespace Pilot