Physics3D: Rename PhysObject to RigidBody3D

This commit is contained in:
Lynix 2016-10-13 08:06:14 +02:00
parent 3c4d084b27
commit 3765cba046
9 changed files with 74 additions and 77 deletions

View File

@ -13,7 +13,7 @@
namespace Nz namespace Nz
{ {
class PhysObject; class RigidBody3D;
} }
namespace Ndk namespace Ndk
@ -41,14 +41,14 @@ namespace Ndk
private: private:
void InitializeStaticBody(); void InitializeStaticBody();
Nz::PhysObject* GetStaticBody(); Nz::RigidBody3D* GetStaticBody();
void OnAttached() override; void OnAttached() override;
void OnComponentAttached(BaseComponent& component) override; void OnComponentAttached(BaseComponent& component) override;
void OnComponentDetached(BaseComponent& component) override; void OnComponentDetached(BaseComponent& component) override;
void OnDetached() override; void OnDetached() override;
std::unique_ptr<Nz::PhysObject> m_staticBody; std::unique_ptr<Nz::RigidBody3D> m_staticBody;
Nz::Collider3DRef m_geom; Nz::Collider3DRef m_geom;
bool m_bodyUpdated; bool m_bodyUpdated;
}; };

View File

@ -62,7 +62,7 @@ namespace Ndk
* \return A pointer to the entity * \return A pointer to the entity
*/ */
inline Nz::PhysObject* CollisionComponent::GetStaticBody() inline Nz::RigidBody3D* CollisionComponent::GetStaticBody()
{ {
return m_staticBody.get(); return m_staticBody.get();
} }

View File

@ -56,14 +56,14 @@ namespace Ndk
static ComponentIndex componentIndex; static ComponentIndex componentIndex;
private: private:
Nz::PhysObject& GetPhysObject(); Nz::RigidBody3D& GetPhysObject();
void OnAttached() override; void OnAttached() override;
void OnComponentAttached(BaseComponent& component) override; void OnComponentAttached(BaseComponent& component) override;
void OnComponentDetached(BaseComponent& component) override; void OnComponentDetached(BaseComponent& component) override;
void OnDetached() override; void OnDetached() override;
std::unique_ptr<Nz::PhysObject> m_object; std::unique_ptr<Nz::RigidBody3D> m_object;
}; };
} }

View File

@ -350,7 +350,7 @@ namespace Ndk
* \return A reference to the physics object * \return A reference to the physics object
*/ */
inline Nz::PhysObject& PhysicsComponent::GetPhysObject() inline Nz::RigidBody3D& PhysicsComponent::GetPhysObject()
{ {
return *m_object.get(); return *m_object.get();
} }

View File

@ -58,7 +58,7 @@ namespace Ndk
NazaraAssert(entityWorld->HasSystem<PhysicsSystem>(), "World must have a physics system"); NazaraAssert(entityWorld->HasSystem<PhysicsSystem>(), "World must have a physics system");
Nz::PhysWorld& physWorld = entityWorld->GetSystem<PhysicsSystem>().GetWorld(); Nz::PhysWorld& physWorld = entityWorld->GetSystem<PhysicsSystem>().GetWorld();
m_staticBody.reset(new Nz::PhysObject(&physWorld, m_geom)); m_staticBody.reset(new Nz::RigidBody3D(&physWorld, m_geom));
m_staticBody->EnableAutoSleep(false); m_staticBody->EnableAutoSleep(false);
} }

View File

@ -41,7 +41,7 @@ namespace Ndk
else else
matrix.MakeIdentity(); 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); m_object->SetMass(1.f);
} }

View File

@ -83,7 +83,7 @@ namespace Ndk
NodeComponent& node = entity->GetComponent<NodeComponent>(); NodeComponent& node = entity->GetComponent<NodeComponent>();
PhysicsComponent& phys = entity->GetComponent<PhysicsComponent>(); PhysicsComponent& phys = entity->GetComponent<PhysicsComponent>();
Nz::PhysObject& physObj = phys.GetPhysObject(); Nz::RigidBody3D& physObj = phys.GetPhysObject();
node.SetRotation(physObj.GetRotation(), Nz::CoordSys_Global); node.SetRotation(physObj.GetRotation(), Nz::CoordSys_Global);
node.SetPosition(physObj.GetPosition(), Nz::CoordSys_Global); node.SetPosition(physObj.GetPosition(), Nz::CoordSys_Global);
} }
@ -94,7 +94,7 @@ namespace Ndk
CollisionComponent& collision = entity->GetComponent<CollisionComponent>(); CollisionComponent& collision = entity->GetComponent<CollisionComponent>();
NodeComponent& node = entity->GetComponent<NodeComponent>(); NodeComponent& node = entity->GetComponent<NodeComponent>();
Nz::PhysObject* physObj = collision.GetStaticBody(); Nz::RigidBody3D* physObj = collision.GetStaticBody();
Nz::Quaternionf oldRotation = physObj->GetRotation(); Nz::Quaternionf oldRotation = physObj->GetRotation();
Nz::Vector3f oldPosition = physObj->GetPosition(); Nz::Vector3f oldPosition = physObj->GetPosition();

View File

@ -4,8 +4,8 @@
#pragma once #pragma once
#ifndef NAZARA_PHYSOBJECT_HPP #ifndef NAZARA_RIGIDBODY3D_HPP
#define NAZARA_PHYSOBJECT_HPP #define NAZARA_RIGIDBODY3D_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Enums.hpp> #include <Nazara/Core/Enums.hpp>
@ -21,14 +21,14 @@ namespace Nz
{ {
class PhysWorld; class PhysWorld;
class NAZARA_PHYSICS3D_API PhysObject class NAZARA_PHYSICS3D_API RigidBody3D
{ {
public: public:
PhysObject(PhysWorld* world, const Matrix4f& mat = Matrix4f::Identity()); RigidBody3D(PhysWorld* world, const Matrix4f& mat = Matrix4f::Identity());
PhysObject(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat = Matrix4f::Identity()); RigidBody3D(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat = Matrix4f::Identity());
PhysObject(const PhysObject& object); RigidBody3D(const RigidBody3D& object);
PhysObject(PhysObject&& object); RigidBody3D(RigidBody3D&& object);
~PhysObject(); ~RigidBody3D();
void AddForce(const Vector3f& force, CoordSys coordSys = CoordSys_Global); void AddForce(const Vector3f& force, CoordSys coordSys = CoordSys_Global);
void AddForce(const Vector3f& force, const Vector3f& point, 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 SetRotation(const Quaternionf& rotation);
void SetVelocity(const Vector3f& velocity); void SetVelocity(const Vector3f& velocity);
PhysObject& operator=(const PhysObject& object); RigidBody3D& operator=(const RigidBody3D& object);
PhysObject& operator=(PhysObject&& object); RigidBody3D& operator=(RigidBody3D&& object);
private: private:
void UpdateBody(); void UpdateBody();
static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex); static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex);
static void TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex); static void TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex);
Matrix4f m_matrix;
Collider3DRef m_geom; Collider3DRef m_geom;
Matrix4f m_matrix;
Vector3f m_forceAccumulator; Vector3f m_forceAccumulator;
Vector3f m_torqueAccumulator; Vector3f m_torqueAccumulator;
NewtonBody* m_body; NewtonBody* m_body;
@ -80,4 +80,4 @@ namespace Nz
}; };
} }
#endif // NAZARA_PHYSOBJECT_HPP #endif // NAZARA_RIGIDBODY3D_HPP

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Physics 3D module" // This file is part of the "Nazara Engine - Physics 3D module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics3D/PhysObject.hpp> #include <Nazara/Physics3D/RigidBody3D.hpp>
#include <Nazara/Math/Algorithm.hpp> #include <Nazara/Math/Algorithm.hpp>
#include <Nazara/Physics3D/Config.hpp> #include <Nazara/Physics3D/Config.hpp>
#include <Nazara/Physics3D/PhysWorld.hpp> #include <Nazara/Physics3D/PhysWorld.hpp>
@ -12,14 +12,14 @@
namespace Nz namespace Nz
{ {
PhysObject::PhysObject(PhysWorld* world, const Matrix4f& mat) : RigidBody3D::RigidBody3D(PhysWorld* world, const Matrix4f& mat) :
PhysObject(world, NullCollider3D::New(), mat) RigidBody3D(world, NullCollider3D::New(), mat)
{ {
} }
PhysObject::PhysObject(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat) : RigidBody3D::RigidBody3D(PhysWorld* world, Collider3DRef geom, const Matrix4f& mat) :
m_matrix(mat),
m_geom(std::move(geom)), m_geom(std::move(geom)),
m_matrix(mat),
m_forceAccumulator(Vector3f::Zero()), m_forceAccumulator(Vector3f::Zero()),
m_torqueAccumulator(Vector3f::Zero()), m_torqueAccumulator(Vector3f::Zero()),
m_world(world), m_world(world),
@ -35,9 +35,9 @@ namespace Nz
NewtonBodySetUserData(m_body, this); NewtonBodySetUserData(m_body, this);
} }
PhysObject::PhysObject(const PhysObject& object) : RigidBody3D::RigidBody3D(const RigidBody3D& object) :
m_matrix(object.m_matrix),
m_geom(object.m_geom), m_geom(object.m_geom),
m_matrix(object.m_matrix),
m_forceAccumulator(Vector3f::Zero()), m_forceAccumulator(Vector3f::Zero()),
m_torqueAccumulator(Vector3f::Zero()), m_torqueAccumulator(Vector3f::Zero()),
m_world(object.m_world), m_world(object.m_world),
@ -52,9 +52,9 @@ namespace Nz
SetMass(object.m_mass); SetMass(object.m_mass);
} }
PhysObject::PhysObject(PhysObject&& object) : RigidBody3D::RigidBody3D(RigidBody3D&& object) :
m_matrix(std::move(object.m_matrix)),
m_geom(std::move(object.m_geom)), m_geom(std::move(object.m_geom)),
m_matrix(std::move(object.m_matrix)),
m_forceAccumulator(std::move(object.m_forceAccumulator)), m_forceAccumulator(std::move(object.m_forceAccumulator)),
m_torqueAccumulator(std::move(object.m_torqueAccumulator)), m_torqueAccumulator(std::move(object.m_torqueAccumulator)),
m_body(object.m_body), m_body(object.m_body),
@ -65,13 +65,13 @@ namespace Nz
object.m_body = nullptr; object.m_body = nullptr;
} }
PhysObject::~PhysObject() RigidBody3D::~RigidBody3D()
{ {
if (m_body) if (m_body)
NewtonDestroyBody(m_body); NewtonDestroyBody(m_body);
} }
void PhysObject::AddForce(const Vector3f& force, CoordSys coordSys) void RigidBody3D::AddForce(const Vector3f& force, CoordSys coordSys)
{ {
switch (coordSys) switch (coordSys)
{ {
@ -88,7 +88,7 @@ namespace Nz
NewtonBodySetSleepState(m_body, 0); 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) switch (coordSys)
{ {
@ -105,7 +105,7 @@ namespace Nz
NewtonBodySetSleepState(m_body, 0); NewtonBodySetSleepState(m_body, 0);
} }
void PhysObject::AddTorque(const Vector3f& torque, CoordSys coordSys) void RigidBody3D::AddTorque(const Vector3f& torque, CoordSys coordSys)
{ {
switch (coordSys) switch (coordSys)
{ {
@ -122,12 +122,12 @@ namespace Nz
NewtonBodySetSleepState(m_body, 0); NewtonBodySetSleepState(m_body, 0);
} }
void PhysObject::EnableAutoSleep(bool autoSleep) void RigidBody3D::EnableAutoSleep(bool autoSleep)
{ {
NewtonBodySetAutoSleep(m_body, autoSleep); NewtonBodySetAutoSleep(m_body, autoSleep);
} }
Boxf PhysObject::GetAABB() const Boxf RigidBody3D::GetAABB() const
{ {
Vector3f min, max; Vector3f min, max;
NewtonBodyGetAABB(m_body, min, max); NewtonBodyGetAABB(m_body, min, max);
@ -135,7 +135,7 @@ namespace Nz
return Boxf(min, max); return Boxf(min, max);
} }
Vector3f PhysObject::GetAngularVelocity() const Vector3f RigidBody3D::GetAngularVelocity() const
{ {
Vector3f angularVelocity; Vector3f angularVelocity;
NewtonBodyGetOmega(m_body, angularVelocity); NewtonBodyGetOmega(m_body, angularVelocity);
@ -143,27 +143,27 @@ namespace Nz
return angularVelocity; return angularVelocity;
} }
const Collider3DRef& PhysObject::GetGeom() const const Collider3DRef& RigidBody3D::GetGeom() const
{ {
return m_geom; return m_geom;
} }
float PhysObject::GetGravityFactor() const float RigidBody3D::GetGravityFactor() const
{ {
return m_gravityFactor; return m_gravityFactor;
} }
NewtonBody* PhysObject::GetHandle() const NewtonBody* RigidBody3D::GetHandle() const
{ {
return m_body; return m_body;
} }
float PhysObject::GetMass() const float RigidBody3D::GetMass() const
{ {
return m_mass; return m_mass;
} }
Vector3f PhysObject::GetMassCenter(CoordSys coordSys) const Vector3f RigidBody3D::GetMassCenter(CoordSys coordSys) const
{ {
Vector3f center; Vector3f center;
NewtonBodyGetCentreOfMass(m_body, center); NewtonBodyGetCentreOfMass(m_body, center);
@ -181,22 +181,22 @@ namespace Nz
return center; return center;
} }
const Matrix4f& PhysObject::GetMatrix() const const Matrix4f& RigidBody3D::GetMatrix() const
{ {
return m_matrix; return m_matrix;
} }
Vector3f PhysObject::GetPosition() const Vector3f RigidBody3D::GetPosition() const
{ {
return m_matrix.GetTranslation(); return m_matrix.GetTranslation();
} }
Quaternionf PhysObject::GetRotation() const Quaternionf RigidBody3D::GetRotation() const
{ {
return m_matrix.GetRotation(); return m_matrix.GetRotation();
} }
Vector3f PhysObject::GetVelocity() const Vector3f RigidBody3D::GetVelocity() const
{ {
Vector3f velocity; Vector3f velocity;
NewtonBodyGetVelocity(m_body, velocity); NewtonBodyGetVelocity(m_body, velocity);
@ -204,27 +204,27 @@ namespace Nz
return velocity; return velocity;
} }
bool PhysObject::IsAutoSleepEnabled() const bool RigidBody3D::IsAutoSleepEnabled() const
{ {
return NewtonBodyGetAutoSleep(m_body) != 0; return NewtonBodyGetAutoSleep(m_body) != 0;
} }
bool PhysObject::IsMoveable() const bool RigidBody3D::IsMoveable() const
{ {
return m_mass > 0.f; return m_mass > 0.f;
} }
bool PhysObject::IsSleeping() const bool RigidBody3D::IsSleeping() const
{ {
return NewtonBodyGetSleepState(m_body) != 0; return NewtonBodyGetSleepState(m_body) != 0;
} }
void PhysObject::SetAngularVelocity(const Vector3f& angularVelocity) void RigidBody3D::SetAngularVelocity(const Vector3f& angularVelocity)
{ {
NewtonBodySetOmega(m_body, angularVelocity); NewtonBodySetOmega(m_body, angularVelocity);
} }
void PhysObject::SetGeom(Collider3DRef geom) void RigidBody3D::SetGeom(Collider3DRef geom)
{ {
if (m_geom.Get() != 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; m_gravityFactor = gravityFactor;
} }
void PhysObject::SetMass(float mass) void RigidBody3D::SetMass(float mass)
{ {
if (m_mass > 0.f) 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; float Ix, Iy, Iz;
NewtonBodyGetMassMatrix(m_body, &m_mass, &Ix, &Iy, &Iz); NewtonBodyGetMassMatrix(m_body, &m_mass, &Ix, &Iy, &Iz);
float scale = mass/m_mass; float scale = mass/m_mass;
NewtonBodySetMassMatrix(m_body, mass, Ix*scale, Iy*scale, Iz*scale); NewtonBodySetMassMatrix(m_body, mass, Ix*scale, Iy*scale, Iz*scale);
} }
@ -265,41 +267,44 @@ namespace Nz
m_mass = mass; m_mass = mass;
} }
void PhysObject::SetMassCenter(const Vector3f& center) void RigidBody3D::SetMassCenter(const Vector3f& center)
{ {
if (m_mass > 0.f) if (m_mass > 0.f)
NewtonBodySetCentreOfMass(m_body, center); NewtonBodySetCentreOfMass(m_body, center);
} }
void PhysObject::SetPosition(const Vector3f& position) void RigidBody3D::SetPosition(const Vector3f& position)
{ {
m_matrix.SetTranslation(position); m_matrix.SetTranslation(position);
UpdateBody(); UpdateBody();
} }
void PhysObject::SetRotation(const Quaternionf& rotation) void RigidBody3D::SetRotation(const Quaternionf& rotation)
{ {
m_matrix.SetRotation(rotation); m_matrix.SetRotation(rotation);
UpdateBody(); UpdateBody();
} }
void PhysObject::SetVelocity(const Vector3f& velocity) void RigidBody3D::SetVelocity(const Vector3f& velocity)
{ {
NewtonBodySetVelocity(m_body, 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)); return operator=(std::move(physObj));
} }
void PhysObject::UpdateBody() void RigidBody3D::UpdateBody()
{ {
NewtonBodySetMatrix(m_body, m_matrix); NewtonBodySetMatrix(m_body, m_matrix);
if (NumberEquals(m_mass, 0.f)) 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 // http://newtondynamics.com/wiki/index.php5?title=Can_i_dynamicly_move_a_TriMesh%3F
Vector3f min, max; Vector3f min, max;
NewtonBodyGetAABB(m_body, min, max); NewtonBodyGetAABB(m_body, min, max);
@ -312,11 +317,9 @@ namespace Nz
}, },
nullptr); nullptr);
} }
/*for (std::set<PhysObjectListener*>::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) if (m_body)
NewtonDestroyBody(m_body); NewtonDestroyBody(m_body);
@ -335,36 +338,30 @@ namespace Nz
return *this; 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(timeStep);
NazaraUnused(threadIndex); NazaraUnused(threadIndex);
PhysObject* me = static_cast<PhysObject*>(NewtonBodyGetUserData(body)); RigidBody3D* me = static_cast<RigidBody3D*>(NewtonBodyGetUserData(body));
if (!NumberEquals(me->m_gravityFactor, 0.f)) if (!NumberEquals(me->m_gravityFactor, 0.f))
me->m_forceAccumulator += me->m_world->GetGravity() * me->m_gravityFactor * me->m_mass; me->m_forceAccumulator += me->m_world->GetGravity() * me->m_gravityFactor * me->m_mass;
/*for (std::set<PhysObjectListener*>::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it)
(*it)->PhysObjectApplyForce(me);*/
NewtonBodySetForce(body, me->m_forceAccumulator); NewtonBodySetForce(body, me->m_forceAccumulator);
NewtonBodySetTorque(body, me->m_torqueAccumulator); NewtonBodySetTorque(body, me->m_torqueAccumulator);
me->m_torqueAccumulator.Set(0.f); me->m_torqueAccumulator.Set(0.f);
me->m_forceAccumulator.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); NazaraUnused(threadIndex);
PhysObject* me = static_cast<PhysObject*>(NewtonBodyGetUserData(body)); RigidBody3D* me = static_cast<RigidBody3D*>(NewtonBodyGetUserData(body));
me->m_matrix.Set(matrix); me->m_matrix.Set(matrix);
/*for (std::set<PhysObjectListener*>::iterator it = me->m_listeners.begin(); it != me->m_listeners.end(); ++it)
(*it)->PhysObjectOnUpdate(me);*/
} }
} }