SDK/PhysicsComponent2D: Add node synchronization
This commit is contained in:
@@ -24,7 +24,7 @@ namespace Ndk
|
||||
friend class ConstraintComponent2D;
|
||||
|
||||
public:
|
||||
PhysicsComponent2D() = default;
|
||||
PhysicsComponent2D();
|
||||
PhysicsComponent2D(const PhysicsComponent2D& physics);
|
||||
~PhysicsComponent2D() = default;
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace Ndk
|
||||
|
||||
inline bool ClosestPointQuery(const Nz::Vector2f& position, Nz::Vector2f* closestPoint, float* closestDistance) const;
|
||||
|
||||
inline void EnableNodeSynchronization(bool nodeSynchronization);
|
||||
|
||||
inline Nz::Rectf GetAABB() const;
|
||||
inline float GetAngularDamping() const;
|
||||
inline Nz::RadianAnglef GetAngularVelocity() const;
|
||||
@@ -52,6 +54,7 @@ namespace Ndk
|
||||
inline std::size_t GetShapeCount() const;
|
||||
inline Nz::Vector2f GetVelocity() const;
|
||||
|
||||
inline bool IsNodeSynchronizationEnabled() const;
|
||||
inline bool IsSleeping() const;
|
||||
|
||||
inline void SetAngularDamping(float angularDamping);
|
||||
@@ -81,6 +84,7 @@ namespace Ndk
|
||||
void OnEntityDestruction() override;
|
||||
|
||||
std::unique_ptr<Nz::RigidBody2D> m_object;
|
||||
bool m_nodeSynchronizationEnabled;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,19 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Constructs a PhysicsComponent2D object by default
|
||||
*/
|
||||
inline PhysicsComponent2D::PhysicsComponent2D() :
|
||||
m_nodeSynchronizationEnabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a PhysicsComponent2D object by copy semantic
|
||||
*
|
||||
* \param physics PhysicsComponent2D to copy
|
||||
*/
|
||||
|
||||
inline PhysicsComponent2D::PhysicsComponent2D(const PhysicsComponent2D& physics)
|
||||
{
|
||||
// No copy of physical object (because we only create it when attached to an entity)
|
||||
@@ -115,6 +122,22 @@ namespace Ndk
|
||||
return m_object->ClosestPointQuery(position, closestPoint, closestDistance);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enables position/rotation synchronization with the NodeComponent
|
||||
*
|
||||
* By default, at every update of the PhysicsSystem2D, the NodeComponent's position and rotation (if any) will be synchronized with
|
||||
* the values of the PhysicsComponent2D. This function allows to enable/disable this behavior on a per-entity basis.
|
||||
*
|
||||
* \param nodeSynchronization Should synchronization occur between NodeComponent and PhysicsComponent2D
|
||||
*/
|
||||
inline void PhysicsComponent2D::EnableNodeSynchronization(bool nodeSynchronization)
|
||||
{
|
||||
m_nodeSynchronizationEnabled = nodeSynchronization;
|
||||
|
||||
if (m_entity)
|
||||
m_entity->Invalidate();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the AABB of the physics object
|
||||
* \return AABB of the object
|
||||
@@ -314,13 +337,23 @@ namespace Ndk
|
||||
return m_object->GetVelocity();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks if position & rotation are synchronized with NodeComponent
|
||||
* \return true If synchronization is enabled
|
||||
*
|
||||
* \see EnableNodeSynchronization
|
||||
*/
|
||||
inline bool PhysicsComponent2D::IsNodeSynchronizationEnabled() const
|
||||
{
|
||||
return m_nodeSynchronizationEnabled;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the entity is currently sleeping
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline bool PhysicsComponent2D::IsSleeping() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
Reference in New Issue
Block a user