diff --git a/SDK/include/NDK/Components/CollisionComponent.hpp b/SDK/include/NDK/Components/CollisionComponent.hpp index 722a5476e..565a60871 100644 --- a/SDK/include/NDK/Components/CollisionComponent.hpp +++ b/SDK/include/NDK/Components/CollisionComponent.hpp @@ -13,7 +13,7 @@ namespace Nz { - class PhysObject; + class RigidBody3D; } namespace Ndk @@ -41,14 +41,14 @@ namespace Ndk private: void InitializeStaticBody(); - Nz::PhysObject* GetStaticBody(); + Nz::RigidBody3D* GetStaticBody(); void OnAttached() override; void OnComponentAttached(BaseComponent& component) override; void OnComponentDetached(BaseComponent& component) override; void OnDetached() override; - std::unique_ptr m_staticBody; + std::unique_ptr m_staticBody; Nz::Collider3DRef m_geom; bool m_bodyUpdated; }; diff --git a/SDK/include/NDK/Components/CollisionComponent.inl b/SDK/include/NDK/Components/CollisionComponent.inl index c34b1e219..7243996fb 100644 --- a/SDK/include/NDK/Components/CollisionComponent.inl +++ b/SDK/include/NDK/Components/CollisionComponent.inl @@ -62,7 +62,7 @@ namespace Ndk * \return A pointer to the entity */ - inline Nz::PhysObject* CollisionComponent::GetStaticBody() + inline Nz::RigidBody3D* CollisionComponent::GetStaticBody() { return m_staticBody.get(); } diff --git a/SDK/include/NDK/Components/PhysicsComponent.hpp b/SDK/include/NDK/Components/PhysicsComponent.hpp index 97d2c62f6..b6abe24e1 100644 --- a/SDK/include/NDK/Components/PhysicsComponent.hpp +++ b/SDK/include/NDK/Components/PhysicsComponent.hpp @@ -56,14 +56,14 @@ namespace Ndk static ComponentIndex componentIndex; private: - Nz::PhysObject& GetPhysObject(); + Nz::RigidBody3D& GetPhysObject(); void OnAttached() override; void OnComponentAttached(BaseComponent& component) override; void OnComponentDetached(BaseComponent& component) override; void OnDetached() override; - std::unique_ptr m_object; + std::unique_ptr m_object; }; } diff --git a/SDK/include/NDK/Components/PhysicsComponent.inl b/SDK/include/NDK/Components/PhysicsComponent.inl index 5682603e0..009d62ba5 100644 --- a/SDK/include/NDK/Components/PhysicsComponent.inl +++ b/SDK/include/NDK/Components/PhysicsComponent.inl @@ -350,7 +350,7 @@ namespace Ndk * \return A reference to the physics object */ - inline Nz::PhysObject& PhysicsComponent::GetPhysObject() + inline Nz::RigidBody3D& PhysicsComponent::GetPhysObject() { return *m_object.get(); } diff --git a/SDK/src/NDK/Components/CollisionComponent.cpp b/SDK/src/NDK/Components/CollisionComponent.cpp index 130eae7dd..44d70be24 100644 --- a/SDK/src/NDK/Components/CollisionComponent.cpp +++ b/SDK/src/NDK/Components/CollisionComponent.cpp @@ -58,7 +58,7 @@ namespace Ndk NazaraAssert(entityWorld->HasSystem(), "World must have a physics system"); Nz::PhysWorld& physWorld = entityWorld->GetSystem().GetWorld(); - m_staticBody.reset(new Nz::PhysObject(&physWorld, m_geom)); + m_staticBody.reset(new Nz::RigidBody3D(&physWorld, m_geom)); m_staticBody->EnableAutoSleep(false); } diff --git a/SDK/src/NDK/Components/PhysicsComponent.cpp b/SDK/src/NDK/Components/PhysicsComponent.cpp index 668dbcd79..2ac6e3dab 100644 --- a/SDK/src/NDK/Components/PhysicsComponent.cpp +++ b/SDK/src/NDK/Components/PhysicsComponent.cpp @@ -41,7 +41,7 @@ namespace Ndk else matrix.MakeIdentity(); - m_object.reset(new Nz::PhysObject(&world, geom, matrix)); + m_object.reset(new Nz::RigidBody3D(&world, geom, matrix)); m_object->SetMass(1.f); } diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index f6621261a..83a368b37 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -83,7 +83,7 @@ namespace Ndk NodeComponent& node = entity->GetComponent(); PhysicsComponent& phys = entity->GetComponent(); - Nz::PhysObject& physObj = phys.GetPhysObject(); + Nz::RigidBody3D& physObj = phys.GetPhysObject(); node.SetRotation(physObj.GetRotation(), Nz::CoordSys_Global); node.SetPosition(physObj.GetPosition(), Nz::CoordSys_Global); } @@ -94,7 +94,7 @@ namespace Ndk CollisionComponent& collision = entity->GetComponent(); NodeComponent& node = entity->GetComponent(); - Nz::PhysObject* physObj = collision.GetStaticBody(); + Nz::RigidBody3D* physObj = collision.GetStaticBody(); Nz::Quaternionf oldRotation = physObj->GetRotation(); Nz::Vector3f oldPosition = physObj->GetPosition(); diff --git a/include/Nazara/Physics3D/PhysObject.hpp b/include/Nazara/Physics3D/RigidBody3D.hpp similarity index 80% rename from include/Nazara/Physics3D/PhysObject.hpp rename to include/Nazara/Physics3D/RigidBody3D.hpp index a2894d0ce..0a92cb310 100644 --- a/include/Nazara/Physics3D/PhysObject.hpp +++ b/include/Nazara/Physics3D/RigidBody3D.hpp @@ -4,8 +4,8 @@ #pragma once -#ifndef NAZARA_PHYSOBJECT_HPP -#define NAZARA_PHYSOBJECT_HPP +#ifndef NAZARA_RIGIDBODY3D_HPP +#define NAZARA_RIGIDBODY3D_HPP #include #include @@ -21,14 +21,14 @@ namespace Nz { class PhysWorld; - class NAZARA_PHYSICS3D_API PhysObject + class NAZARA_PHYSICS3D_API RigidBody3D { public: - PhysObject(PhysWorld* world, const Matrix4f& mat = Matrix4f::Identity()); - PhysObject(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat = Matrix4f::Identity()); - PhysObject(const PhysObject& object); - PhysObject(PhysObject&& object); - ~PhysObject(); + RigidBody3D(PhysWorld* world, const Matrix4f& mat = Matrix4f::Identity()); + RigidBody3D(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat = Matrix4f::Identity()); + RigidBody3D(const RigidBody3D& object); + RigidBody3D(RigidBody3D&& object); + ~RigidBody3D(); void AddForce(const Vector3f& force, CoordSys coordSys = CoordSys_Global); void AddForce(const Vector3f& force, const Vector3f& point, CoordSys coordSys = CoordSys_Global); @@ -61,16 +61,16 @@ namespace Nz void SetRotation(const Quaternionf& rotation); void SetVelocity(const Vector3f& velocity); - PhysObject& operator=(const PhysObject& object); - PhysObject& operator=(PhysObject&& object); + RigidBody3D& operator=(const RigidBody3D& object); + RigidBody3D& operator=(RigidBody3D&& object); private: void UpdateBody(); static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex); static void TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex); - Matrix4f m_matrix; Collider3DRef m_geom; + Matrix4f m_matrix; Vector3f m_forceAccumulator; Vector3f m_torqueAccumulator; NewtonBody* m_body; @@ -80,4 +80,4 @@ namespace Nz }; } -#endif // NAZARA_PHYSOBJECT_HPP +#endif // NAZARA_RIGIDBODY3D_HPP diff --git a/src/Nazara/Physics3D/PhysObject.cpp b/src/Nazara/Physics3D/RigidBody3D.cpp similarity index 70% rename from src/Nazara/Physics3D/PhysObject.cpp rename to src/Nazara/Physics3D/RigidBody3D.cpp index 283d7ac84..9a2d3d545 100644 --- a/src/Nazara/Physics3D/PhysObject.cpp +++ b/src/Nazara/Physics3D/RigidBody3D.cpp @@ -2,7 +2,7 @@ // This file is part of the "Nazara Engine - Physics 3D module" // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #include #include #include @@ -12,14 +12,14 @@ namespace Nz { - PhysObject::PhysObject(PhysWorld* world, const Matrix4f& mat) : - PhysObject(world, NullCollider3D::New(), mat) + RigidBody3D::RigidBody3D(PhysWorld* world, const Matrix4f& mat) : + RigidBody3D(world, NullCollider3D::New(), mat) { } - PhysObject::PhysObject(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat) : - m_matrix(mat), + RigidBody3D::RigidBody3D(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat) : m_geom(std::move(geom)), + m_matrix(mat), m_forceAccumulator(Vector3f::Zero()), m_torqueAccumulator(Vector3f::Zero()), m_world(world), @@ -35,9 +35,9 @@ namespace Nz NewtonBodySetUserData(m_body, this); } - PhysObject::PhysObject(const PhysObject& object) : - m_matrix(object.m_matrix), + RigidBody3D::RigidBody3D(const RigidBody3D& object) : m_geom(object.m_geom), + m_matrix(object.m_matrix), m_forceAccumulator(Vector3f::Zero()), m_torqueAccumulator(Vector3f::Zero()), m_world(object.m_world), @@ -52,9 +52,9 @@ namespace Nz SetMass(object.m_mass); } - PhysObject::PhysObject(PhysObject&& object) : - m_matrix(std::move(object.m_matrix)), + RigidBody3D::RigidBody3D(RigidBody3D&& object) : m_geom(std::move(object.m_geom)), + m_matrix(std::move(object.m_matrix)), m_forceAccumulator(std::move(object.m_forceAccumulator)), m_torqueAccumulator(std::move(object.m_torqueAccumulator)), m_body(object.m_body), @@ -65,13 +65,13 @@ namespace Nz object.m_body = nullptr; } - PhysObject::~PhysObject() + RigidBody3D::~RigidBody3D() { if (m_body) NewtonDestroyBody(m_body); } - void PhysObject::AddForce(const Vector3f& force, CoordSys coordSys) + void RigidBody3D::AddForce(const Vector3f& force, CoordSys coordSys) { switch (coordSys) { @@ -88,7 +88,7 @@ namespace Nz NewtonBodySetSleepState(m_body, 0); } - void PhysObject::AddForce(const Vector3f& force, const Vector3f& point, CoordSys coordSys) + void RigidBody3D::AddForce(const Vector3f& force, const Vector3f& point, CoordSys coordSys) { switch (coordSys) { @@ -105,7 +105,7 @@ namespace Nz NewtonBodySetSleepState(m_body, 0); } - void PhysObject::AddTorque(const Vector3f& torque, CoordSys coordSys) + void RigidBody3D::AddTorque(const Vector3f& torque, CoordSys coordSys) { switch (coordSys) { @@ -122,12 +122,12 @@ namespace Nz NewtonBodySetSleepState(m_body, 0); } - void PhysObject::EnableAutoSleep(bool autoSleep) + void RigidBody3D::EnableAutoSleep(bool autoSleep) { NewtonBodySetAutoSleep(m_body, autoSleep); } - Boxf PhysObject::GetAABB() const + Boxf RigidBody3D::GetAABB() const { Vector3f min, max; NewtonBodyGetAABB(m_body, min, max); @@ -135,7 +135,7 @@ namespace Nz return Boxf(min, max); } - Vector3f PhysObject::GetAngularVelocity() const + Vector3f RigidBody3D::GetAngularVelocity() const { Vector3f angularVelocity; NewtonBodyGetOmega(m_body, angularVelocity); @@ -143,27 +143,27 @@ namespace Nz return angularVelocity; } - const Collider3DRef& PhysObject::GetGeom() const + const Collider3DRef& RigidBody3D::GetGeom() const { return m_geom; } - float PhysObject::GetGravityFactor() const + float RigidBody3D::GetGravityFactor() const { return m_gravityFactor; } - NewtonBody* PhysObject::GetHandle() const + NewtonBody* RigidBody3D::GetHandle() const { return m_body; } - float PhysObject::GetMass() const + float RigidBody3D::GetMass() const { return m_mass; } - Vector3f PhysObject::GetMassCenter(CoordSys coordSys) const + Vector3f RigidBody3D::GetMassCenter(CoordSys coordSys) const { Vector3f center; NewtonBodyGetCentreOfMass(m_body, center); @@ -181,22 +181,22 @@ namespace Nz return center; } - const Matrix4f& PhysObject::GetMatrix() const + const Matrix4f& RigidBody3D::GetMatrix() const { return m_matrix; } - Vector3f PhysObject::GetPosition() const + Vector3f RigidBody3D::GetPosition() const { return m_matrix.GetTranslation(); } - Quaternionf PhysObject::GetRotation() const + Quaternionf RigidBody3D::GetRotation() const { return m_matrix.GetRotation(); } - Vector3f PhysObject::GetVelocity() const + Vector3f RigidBody3D::GetVelocity() const { Vector3f velocity; NewtonBodyGetVelocity(m_body, velocity); @@ -204,27 +204,27 @@ namespace Nz return velocity; } - bool PhysObject::IsAutoSleepEnabled() const + bool RigidBody3D::IsAutoSleepEnabled() const { return NewtonBodyGetAutoSleep(m_body) != 0; } - bool PhysObject::IsMoveable() const + bool RigidBody3D::IsMoveable() const { return m_mass > 0.f; } - bool PhysObject::IsSleeping() const + bool RigidBody3D::IsSleeping() const { return NewtonBodyGetSleepState(m_body) != 0; } - void PhysObject::SetAngularVelocity(const Vector3f& angularVelocity) + void RigidBody3D::SetAngularVelocity(const Vector3f& angularVelocity) { NewtonBodySetOmega(m_body, angularVelocity); } - void PhysObject::SetGeom(Collider3DRef geom) + void RigidBody3D::SetGeom(Collider3DRef geom) { if (m_geom.Get() != geom) { @@ -237,17 +237,19 @@ namespace Nz } } - void PhysObject::SetGravityFactor(float gravityFactor) + void RigidBody3D::SetGravityFactor(float gravityFactor) { m_gravityFactor = gravityFactor; } - void PhysObject::SetMass(float mass) + void RigidBody3D::SetMass(float mass) { if (m_mass > 0.f) { + // If we already have a mass, we already have an inertial matrix as well, just rescale it float Ix, Iy, Iz; NewtonBodyGetMassMatrix(m_body, &m_mass, &Ix, &Iy, &Iz); + float scale = mass/m_mass; NewtonBodySetMassMatrix(m_body, mass, Ix*scale, Iy*scale, Iz*scale); } @@ -265,41 +267,44 @@ namespace Nz m_mass = mass; } - void PhysObject::SetMassCenter(const Vector3f& center) + void RigidBody3D::SetMassCenter(const Vector3f& center) { if (m_mass > 0.f) NewtonBodySetCentreOfMass(m_body, center); } - void PhysObject::SetPosition(const Vector3f& position) + void RigidBody3D::SetPosition(const Vector3f& position) { m_matrix.SetTranslation(position); + UpdateBody(); } - void PhysObject::SetRotation(const Quaternionf& rotation) + void RigidBody3D::SetRotation(const Quaternionf& rotation) { m_matrix.SetRotation(rotation); + UpdateBody(); } - void PhysObject::SetVelocity(const Vector3f& velocity) + void RigidBody3D::SetVelocity(const Vector3f& velocity) { NewtonBodySetVelocity(m_body, velocity); } - PhysObject& PhysObject::operator=(const PhysObject& object) + RigidBody3D& RigidBody3D::operator=(const RigidBody3D& object) { - PhysObject physObj(object); + RigidBody3D physObj(object); return operator=(std::move(physObj)); } - void PhysObject::UpdateBody() + void RigidBody3D::UpdateBody() { NewtonBodySetMatrix(m_body, m_matrix); if (NumberEquals(m_mass, 0.f)) { + // Moving a static body in Newton does not update bodies at the target location // http://newtondynamics.com/wiki/index.php5?title=Can_i_dynamicly_move_a_TriMesh%3F Vector3f min, max; NewtonBodyGetAABB(m_body, min, max); @@ -312,11 +317,9 @@ namespace Nz }, nullptr); } - /*for (std::set::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) - (*it)->PhysObjectOnUpdate(this);*/ } - PhysObject& PhysObject::operator=(PhysObject&& object) + RigidBody3D& RigidBody3D::operator=(RigidBody3D&& object) { if (m_body) NewtonDestroyBody(m_body); @@ -335,36 +338,30 @@ namespace Nz return *this; } - void PhysObject::ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex) + void RigidBody3D::ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex) { NazaraUnused(timeStep); NazaraUnused(threadIndex); - PhysObject* me = static_cast(NewtonBodyGetUserData(body)); + RigidBody3D* me = static_cast(NewtonBodyGetUserData(body)); if (!NumberEquals(me->m_gravityFactor, 0.f)) me->m_forceAccumulator += me->m_world->GetGravity() * me->m_gravityFactor * me->m_mass; - /*for (std::set::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it) - (*it)->PhysObjectApplyForce(me);*/ - NewtonBodySetForce(body, me->m_forceAccumulator); NewtonBodySetTorque(body, me->m_torqueAccumulator); me->m_torqueAccumulator.Set(0.f); me->m_forceAccumulator.Set(0.f); - ///TODO: Implanter la force gyroscopique? + ///TODO: Implement gyroscopic force? } - void PhysObject::TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex) + void RigidBody3D::TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex) { NazaraUnused(threadIndex); - PhysObject* me = static_cast(NewtonBodyGetUserData(body)); + RigidBody3D* me = static_cast(NewtonBodyGetUserData(body)); me->m_matrix.Set(matrix); - - /*for (std::set::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it) - (*it)->PhysObjectOnUpdate(me);*/ - } + } }