JoltPhysics3D: Allow to construct components without using the system
This commit is contained in:
@@ -14,25 +14,11 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
JoltCharacter::JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position, const Quaternionf& rotation) :
|
||||
m_impl(physWorld.GetDefaultCharacterImpl()),
|
||||
m_collider(std::move(collider)),
|
||||
m_world(&physWorld)
|
||||
JoltCharacter::JoltCharacter() = default;
|
||||
|
||||
JoltCharacter::JoltCharacter(JoltPhysWorld3D& physWorld, const Settings& settings)
|
||||
{
|
||||
auto shapeResult = m_collider->GetShapeSettings()->Create();
|
||||
if (!shapeResult.IsValid())
|
||||
throw std::runtime_error("invalid shape");
|
||||
|
||||
JPH::CharacterSettings settings;
|
||||
settings.mShape = shapeResult.Get();
|
||||
settings.mLayer = 1;
|
||||
|
||||
m_character = std::make_unique<JPH::Character>(&settings, ToJolt(position), ToJolt(rotation), 0, m_world->GetPhysicsSystem());
|
||||
m_character->AddToPhysicsSystem();
|
||||
|
||||
m_bodyIndex = m_character->GetBodyID().GetIndex();
|
||||
|
||||
m_world->RegisterStepListener(this);
|
||||
Create(physWorld, settings);
|
||||
}
|
||||
|
||||
JoltCharacter::JoltCharacter(JoltCharacter&& character) noexcept :
|
||||
@@ -152,6 +138,28 @@ namespace Nz
|
||||
return *this;
|
||||
}
|
||||
|
||||
void JoltCharacter::Create(JoltPhysWorld3D& physWorld, const Settings& settings)
|
||||
{
|
||||
m_collider = settings.collider;
|
||||
m_impl = physWorld.GetDefaultCharacterImpl();
|
||||
m_world = &physWorld;
|
||||
|
||||
auto shapeResult = m_collider->GetShapeSettings()->Create();
|
||||
if (!shapeResult.IsValid())
|
||||
throw std::runtime_error("invalid shape");
|
||||
|
||||
JPH::CharacterSettings characterSettings;
|
||||
characterSettings.mShape = shapeResult.Get();
|
||||
characterSettings.mLayer = 1;
|
||||
|
||||
m_character = std::make_unique<JPH::Character>(&characterSettings, ToJolt(settings.position), ToJolt(settings.rotation), 0, m_world->GetPhysicsSystem());
|
||||
m_character->AddToPhysicsSystem();
|
||||
|
||||
m_bodyIndex = m_character->GetBodyID().GetIndex();
|
||||
|
||||
m_world->RegisterStepListener(this);
|
||||
}
|
||||
|
||||
void JoltCharacter::Destroy()
|
||||
{
|
||||
if (m_character)
|
||||
|
||||
@@ -16,44 +16,6 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
JoltRigidBody3D::JoltRigidBody3D(JoltPhysWorld3D& world, const DynamicSettings& settings) :
|
||||
m_geom(settings.geom),
|
||||
m_world(&world),
|
||||
m_isSimulationEnabled(false),
|
||||
m_isTrigger(settings.isTrigger)
|
||||
{
|
||||
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterface();
|
||||
|
||||
JPH::BodyCreationSettings creationSettings;
|
||||
BuildSettings(settings, creationSettings);
|
||||
|
||||
m_body = bodyInterface.CreateBody(creationSettings);
|
||||
JPH::BodyID bodyId = m_body->GetID();
|
||||
m_bodyIndex = bodyId.GetIndex();
|
||||
|
||||
if (settings.isSimulationEnabled)
|
||||
m_world->RegisterBody(bodyId, !settings.initiallySleeping, false);
|
||||
}
|
||||
|
||||
JoltRigidBody3D::JoltRigidBody3D(JoltPhysWorld3D& world, const StaticSettings& settings) :
|
||||
m_geom(settings.geom),
|
||||
m_world(&world),
|
||||
m_isSimulationEnabled(false),
|
||||
m_isTrigger(settings.isTrigger)
|
||||
{
|
||||
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterface();
|
||||
|
||||
JPH::BodyCreationSettings creationSettings;
|
||||
BuildSettings(settings, creationSettings);
|
||||
|
||||
m_body = bodyInterface.CreateBody(creationSettings);
|
||||
JPH::BodyID bodyId = m_body->GetID();
|
||||
m_bodyIndex = bodyId.GetIndex();
|
||||
|
||||
if (settings.isSimulationEnabled)
|
||||
m_world->RegisterBody(bodyId, false, false); //< static bodies cannot be activated
|
||||
}
|
||||
|
||||
JoltRigidBody3D::JoltRigidBody3D(JoltRigidBody3D&& body) noexcept :
|
||||
m_geom(std::move(body.m_geom)),
|
||||
m_body(std::move(body.m_body)),
|
||||
@@ -357,6 +319,47 @@ namespace Nz
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void JoltRigidBody3D::Create(JoltPhysWorld3D& world, const DynamicSettings& settings)
|
||||
{
|
||||
m_geom = settings.geom;
|
||||
m_isSimulationEnabled = settings.isSimulationEnabled;
|
||||
m_isTrigger = settings.isTrigger;
|
||||
m_world = &world;
|
||||
|
||||
JPH::BodyCreationSettings creationSettings;
|
||||
BuildSettings(settings, creationSettings);
|
||||
|
||||
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterface();
|
||||
m_body = bodyInterface.CreateBody(creationSettings);
|
||||
|
||||
JPH::BodyID bodyId = m_body->GetID();
|
||||
m_bodyIndex = bodyId.GetIndex();
|
||||
|
||||
if (settings.isSimulationEnabled)
|
||||
m_world->RegisterBody(bodyId, !settings.initiallySleeping, false);
|
||||
}
|
||||
|
||||
void JoltRigidBody3D::Create(JoltPhysWorld3D& world, const StaticSettings& settings)
|
||||
{
|
||||
m_geom = settings.geom;
|
||||
m_isSimulationEnabled = settings.isSimulationEnabled;
|
||||
m_isTrigger = settings.isTrigger;
|
||||
m_world = &world;
|
||||
|
||||
JPH::BodyCreationSettings creationSettings;
|
||||
BuildSettings(settings, creationSettings);
|
||||
|
||||
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterface();
|
||||
m_body = bodyInterface.CreateBody(creationSettings);
|
||||
|
||||
JPH::BodyID bodyId = m_body->GetID();
|
||||
m_bodyIndex = bodyId.GetIndex();
|
||||
|
||||
if (settings.isSimulationEnabled)
|
||||
m_world->RegisterBody(bodyId, false, false); //< static bodies cannot be activated
|
||||
}
|
||||
|
||||
void JoltRigidBody3D::Destroy(bool worldDestruction)
|
||||
{
|
||||
if (m_body)
|
||||
|
||||
@@ -15,8 +15,9 @@ namespace Nz
|
||||
m_characterConstructObserver(m_registry, entt::collector.group<JoltCharacterComponent, NodeComponent>(entt::exclude<DisabledComponent, JoltRigidBody3DComponent>)),
|
||||
m_rigidBodyConstructObserver(m_registry, entt::collector.group<JoltRigidBody3DComponent, NodeComponent>(entt::exclude<DisabledComponent, JoltCharacterComponent>))
|
||||
{
|
||||
m_constructConnection = registry.on_construct<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyConstruct>(this);
|
||||
m_destructConnection = registry.on_destroy<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyDestruct>(this);
|
||||
m_bodyConstructConnection = registry.on_construct<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyConstruct>(this);
|
||||
m_characterConstructConnection = registry.on_construct<JoltCharacterComponent>().connect<&JoltPhysics3DSystem::OnCharacterConstruct>(this);
|
||||
m_bodyDestructConnection = registry.on_destroy<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyDestruct>(this);
|
||||
|
||||
m_stepCount = 0;
|
||||
m_physicsTime = Time::Zero();
|
||||
@@ -161,6 +162,8 @@ namespace Nz
|
||||
{
|
||||
// Register rigid body owning entity
|
||||
JoltRigidBody3DComponent& rigidBody = registry.get<JoltRigidBody3DComponent>(entity);
|
||||
rigidBody.Construct(m_physWorld);
|
||||
|
||||
std::size_t uniqueIndex = rigidBody.GetBodyIndex();
|
||||
if (uniqueIndex >= m_physicsEntities.size())
|
||||
m_physicsEntities.resize(uniqueIndex + 1);
|
||||
@@ -168,6 +171,12 @@ namespace Nz
|
||||
m_physicsEntities[uniqueIndex] = entity;
|
||||
}
|
||||
|
||||
void JoltPhysics3DSystem::OnCharacterConstruct(entt::registry& registry, entt::entity entity)
|
||||
{
|
||||
JoltCharacterComponent& character = registry.get<JoltCharacterComponent>(entity);
|
||||
character.Construct(m_physWorld);
|
||||
}
|
||||
|
||||
void JoltPhysics3DSystem::OnBodyDestruct(entt::registry& registry, entt::entity entity)
|
||||
{
|
||||
// Unregister owning entity
|
||||
|
||||
Reference in New Issue
Block a user