Sdk/PhysicsComponent3D: Fix cloning by preserving physics states
This commit is contained in:
@@ -60,7 +60,10 @@ namespace Ndk
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
void ApplyPhysicsState(Nz::RigidBody3D& rigidBody) const;
|
||||
void CopyPhysicsState(const Nz::RigidBody3D& rigidBody);
|
||||
Nz::RigidBody3D& GetRigidBody();
|
||||
const Nz::RigidBody3D& GetRigidBody() const;
|
||||
|
||||
void OnAttached() override;
|
||||
void OnComponentAttached(BaseComponent& component) override;
|
||||
@@ -70,7 +73,19 @@ namespace Ndk
|
||||
void OnEntityDisabled() override;
|
||||
void OnEntityEnabled() override;
|
||||
|
||||
struct PendingPhysObjectStates
|
||||
{
|
||||
Nz::Vector3f angularDamping;
|
||||
Nz::Vector3f massCenter;
|
||||
bool autoSleep;
|
||||
bool valid = false;
|
||||
float gravityFactor;
|
||||
float linearDamping;
|
||||
float mass;
|
||||
};
|
||||
|
||||
std::unique_ptr<Nz::RigidBody3D> m_object;
|
||||
PendingPhysObjectStates m_pendingStates;
|
||||
bool m_nodeSynchronizationEnabled;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace Ndk
|
||||
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)
|
||||
NazaraUnused(physics);
|
||||
// We can't make a copy of the RigidBody3D, as we are not attached yet (and will possibly be attached to another world)
|
||||
CopyPhysicsState(physics.GetRigidBody());
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -383,7 +383,8 @@ namespace Ndk
|
||||
inline void PhysicsComponent3D::SetMass(float mass)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(mass > 0.f, "Mass should be positive");
|
||||
NazaraAssert(mass >= 0.f, "Mass must be positive and finite");
|
||||
NazaraAssert(std::isfinite(mass), "Mass must be positive and finite");
|
||||
|
||||
m_object->SetMass(mass);
|
||||
}
|
||||
@@ -433,13 +434,44 @@ namespace Ndk
|
||||
m_object->SetRotation(rotation);
|
||||
}
|
||||
|
||||
inline void PhysicsComponent3D::ApplyPhysicsState(Nz::RigidBody3D& rigidBody) const
|
||||
{
|
||||
assert(m_pendingStates.valid);
|
||||
|
||||
rigidBody.EnableAutoSleep(m_pendingStates.autoSleep);
|
||||
rigidBody.SetAngularDamping(m_pendingStates.angularDamping);
|
||||
rigidBody.SetGravityFactor(m_pendingStates.gravityFactor);
|
||||
rigidBody.SetLinearDamping(m_pendingStates.linearDamping);
|
||||
rigidBody.SetMass(m_pendingStates.mass);
|
||||
rigidBody.SetMassCenter(m_pendingStates.massCenter);
|
||||
}
|
||||
|
||||
inline void PhysicsComponent3D::CopyPhysicsState(const Nz::RigidBody3D& rigidBody)
|
||||
{
|
||||
m_pendingStates.autoSleep = rigidBody.IsAutoSleepEnabled();
|
||||
m_pendingStates.angularDamping = rigidBody.GetAngularDamping();
|
||||
m_pendingStates.gravityFactor = rigidBody.GetGravityFactor();
|
||||
m_pendingStates.linearDamping = rigidBody.GetLinearDamping();
|
||||
m_pendingStates.mass = rigidBody.GetMass();
|
||||
m_pendingStates.massCenter = rigidBody.GetMassCenter(Nz::CoordSys_Local);
|
||||
m_pendingStates.valid = true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the underlying physics object
|
||||
* \return A reference to the physics object
|
||||
*/
|
||||
|
||||
inline Nz::RigidBody3D& PhysicsComponent3D::GetRigidBody()
|
||||
{
|
||||
return *m_object.get();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the underlying physics object
|
||||
* \return A reference to the physics object
|
||||
*/
|
||||
inline const Nz::RigidBody3D& PhysicsComponent3D::GetRigidBody() const
|
||||
{
|
||||
return *m_object.get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user