Sdk/PhysicsComponent3D: Add node synchronization control
This commit is contained in:
parent
f1b84bfc9e
commit
4eb3520be8
|
|
@ -19,7 +19,7 @@ namespace Ndk
|
||||||
friend class PhysicsSystem3D;
|
friend class PhysicsSystem3D;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PhysicsComponent3D() = default;
|
inline PhysicsComponent3D();
|
||||||
PhysicsComponent3D(const PhysicsComponent3D& physics);
|
PhysicsComponent3D(const PhysicsComponent3D& physics);
|
||||||
~PhysicsComponent3D() = default;
|
~PhysicsComponent3D() = default;
|
||||||
|
|
||||||
|
|
@ -28,6 +28,7 @@ namespace Ndk
|
||||||
void AddTorque(const Nz::Vector3f& torque, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
void AddTorque(const Nz::Vector3f& torque, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||||
|
|
||||||
void EnableAutoSleep(bool autoSleep);
|
void EnableAutoSleep(bool autoSleep);
|
||||||
|
void EnableNodeSynchronization(bool nodeSynchronization);
|
||||||
|
|
||||||
Nz::Boxf GetAABB() const;
|
Nz::Boxf GetAABB() const;
|
||||||
Nz::Vector3f GetAngularDamping() const;
|
Nz::Vector3f GetAngularDamping() const;
|
||||||
|
|
@ -43,6 +44,7 @@ namespace Ndk
|
||||||
|
|
||||||
bool IsAutoSleepEnabled() const;
|
bool IsAutoSleepEnabled() const;
|
||||||
bool IsMoveable() const;
|
bool IsMoveable() const;
|
||||||
|
bool IsNodeSynchronizationEnabled() const;
|
||||||
bool IsSleeping() const;
|
bool IsSleeping() const;
|
||||||
|
|
||||||
void SetAngularDamping(const Nz::Vector3f& angularDamping);
|
void SetAngularDamping(const Nz::Vector3f& angularDamping);
|
||||||
|
|
@ -69,6 +71,7 @@ namespace Ndk
|
||||||
void OnEntityEnabled() override;
|
void OnEntityEnabled() override;
|
||||||
|
|
||||||
std::unique_ptr<Nz::RigidBody3D> m_object;
|
std::unique_ptr<Nz::RigidBody3D> m_object;
|
||||||
|
bool m_nodeSynchronizationEnabled;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,18 @@
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
|
inline PhysicsComponent3D::PhysicsComponent3D() :
|
||||||
|
m_nodeSynchronizationEnabled(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Constructs a PhysicsComponent3D object by copy semantic
|
* \brief Constructs a PhysicsComponent3D object by copy semantic
|
||||||
*
|
*
|
||||||
* \param physics PhysicsComponent3D to copy
|
* \param physics PhysicsComponent3D to copy
|
||||||
*/
|
*/
|
||||||
|
inline PhysicsComponent3D::PhysicsComponent3D(const PhysicsComponent3D& physics) :
|
||||||
inline PhysicsComponent3D::PhysicsComponent3D(const PhysicsComponent3D& physics)
|
m_nodeSynchronizationEnabled(physics.m_nodeSynchronizationEnabled)
|
||||||
{
|
{
|
||||||
// No copy of physical object (because we only create it when attached to an entity)
|
// No copy of physical object (because we only create it when attached to an entity)
|
||||||
NazaraUnused(physics);
|
NazaraUnused(physics);
|
||||||
|
|
@ -75,7 +80,6 @@ namespace Ndk
|
||||||
*
|
*
|
||||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline void PhysicsComponent3D::EnableAutoSleep(bool autoSleep)
|
inline void PhysicsComponent3D::EnableAutoSleep(bool autoSleep)
|
||||||
{
|
{
|
||||||
NazaraAssert(m_object, "Invalid physics object");
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
@ -83,6 +87,22 @@ namespace Ndk
|
||||||
m_object->EnableAutoSleep(autoSleep);
|
m_object->EnableAutoSleep(autoSleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Enables position/rotation synchronization with the NodeComponent
|
||||||
|
*
|
||||||
|
* By default, at every update of the PhysicsSystem3D, the NodeComponent's position and rotation (if any) will be synchronized with
|
||||||
|
* the values of the PhysicsComponent3D. This function allows to enable/disable this behavior on a per-entity basis.
|
||||||
|
*
|
||||||
|
* \param nodeSynchronization Should synchronization occur between NodeComponent and PhysicsComponent3D
|
||||||
|
*/
|
||||||
|
inline void PhysicsComponent3D::EnableNodeSynchronization(bool nodeSynchronization)
|
||||||
|
{
|
||||||
|
m_nodeSynchronizationEnabled = nodeSynchronization;
|
||||||
|
|
||||||
|
if (m_entity)
|
||||||
|
m_entity->Invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Gets the AABB of the physics object
|
* \brief Gets the AABB of the physics object
|
||||||
* \return AABB of the object
|
* \return AABB of the object
|
||||||
|
|
@ -258,13 +278,23 @@ namespace Ndk
|
||||||
return m_object->IsMoveable();
|
return m_object->IsMoveable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Checks if position & rotation are synchronized with NodeComponent
|
||||||
|
* \return true If synchronization is enabled
|
||||||
|
*
|
||||||
|
* \see EnableNodeSynchronization
|
||||||
|
*/
|
||||||
|
inline bool PhysicsComponent3D::IsNodeSynchronizationEnabled() const
|
||||||
|
{
|
||||||
|
return m_nodeSynchronizationEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Checks whether the entity is currently sleeping
|
* \brief Checks whether the entity is currently sleeping
|
||||||
* \return true If it is the case
|
* \return true If it is the case
|
||||||
*
|
*
|
||||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline bool PhysicsComponent3D::IsSleeping() const
|
inline bool PhysicsComponent3D::IsSleeping() const
|
||||||
{
|
{
|
||||||
NazaraAssert(m_object, "Invalid physics object");
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
|
||||||
|
|
@ -47,16 +47,20 @@ namespace Ndk
|
||||||
|
|
||||||
void PhysicsSystem3D::OnEntityValidation(Entity* entity, bool justAdded)
|
void PhysicsSystem3D::OnEntityValidation(Entity* entity, bool justAdded)
|
||||||
{
|
{
|
||||||
// It's possible our entity got revalidated because of the addition/removal of a PhysicsComponent3D
|
if (entity->HasComponent<PhysicsComponent3D>())
|
||||||
if (!justAdded)
|
|
||||||
{
|
{
|
||||||
// We take the opposite array from which the entity should belong to
|
if (entity->GetComponent<PhysicsComponent3D>().IsNodeSynchronizationEnabled())
|
||||||
auto& entities = (entity->HasComponent<PhysicsComponent3D>()) ? m_staticObjects : m_dynamicObjects;
|
m_dynamicObjects.Insert(entity);
|
||||||
entities.Remove(entity);
|
else
|
||||||
}
|
m_dynamicObjects.Remove(entity);
|
||||||
|
|
||||||
auto& entities = (entity->HasComponent<PhysicsComponent3D>()) ? m_dynamicObjects : m_staticObjects;
|
m_staticObjects.Remove(entity);
|
||||||
entities.Insert(entity);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_dynamicObjects.Remove(entity);
|
||||||
|
m_staticObjects.Insert(entity);
|
||||||
|
}
|
||||||
|
|
||||||
if (!m_world)
|
if (!m_world)
|
||||||
CreatePhysWorld();
|
CreatePhysWorld();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue