Physics3D: Fix movement

This commit is contained in:
Jérôme Leclercq 2021-06-20 13:29:58 +02:00
parent 1ac0b2e11a
commit 3ef3580ee7
4 changed files with 59 additions and 28 deletions

View File

@ -33,7 +33,7 @@ namespace Nz
PhysWorld3D(); PhysWorld3D();
PhysWorld3D(const PhysWorld3D&) = delete; PhysWorld3D(const PhysWorld3D&) = delete;
PhysWorld3D(PhysWorld3D&&) noexcept = default; PhysWorld3D(PhysWorld3D&& ph) noexcept;
~PhysWorld3D(); ~PhysWorld3D();
int CreateMaterial(std::string name = {}); int CreateMaterial(std::string name = {});
@ -62,7 +62,7 @@ namespace Nz
void Step(float timestep); void Step(float timestep);
PhysWorld3D& operator=(const PhysWorld3D&) = delete; PhysWorld3D& operator=(const PhysWorld3D&) = delete;
PhysWorld3D& operator=(PhysWorld3D&&) noexcept = default; PhysWorld3D& operator=(PhysWorld3D&&) noexcept;
private: private:
struct Callback struct Callback

View File

@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Enums.hpp> #include <Nazara/Core/Enums.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Quaternion.hpp> #include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
@ -27,7 +28,7 @@ namespace Nz
RigidBody3D(PhysWorld3D* world, const Matrix4f& mat = Matrix4f::Identity()); RigidBody3D(PhysWorld3D* world, const Matrix4f& mat = Matrix4f::Identity());
RigidBody3D(PhysWorld3D* world, std::shared_ptr<Collider3D> geom, const Matrix4f& mat = Matrix4f::Identity()); RigidBody3D(PhysWorld3D* world, std::shared_ptr<Collider3D> geom, const Matrix4f& mat = Matrix4f::Identity());
RigidBody3D(const RigidBody3D& object); RigidBody3D(const RigidBody3D& object);
RigidBody3D(RigidBody3D&& object); RigidBody3D(RigidBody3D&& object) noexcept;
~RigidBody3D(); ~RigidBody3D();
void AddForce(const Vector3f& force, CoordSys coordSys = CoordSys::Global); void AddForce(const Vector3f& force, CoordSys coordSys = CoordSys::Global);
@ -74,7 +75,7 @@ namespace Nz
void SetUserdata(void* ud); void SetUserdata(void* ud);
RigidBody3D& operator=(const RigidBody3D& object); RigidBody3D& operator=(const RigidBody3D& object);
RigidBody3D& operator=(RigidBody3D&& object); RigidBody3D& operator=(RigidBody3D&& object) noexcept;
private: private:
void UpdateBody(); void UpdateBody();
@ -83,9 +84,9 @@ namespace Nz
std::shared_ptr<Collider3D> m_geom; std::shared_ptr<Collider3D> m_geom;
Matrix4f m_matrix; Matrix4f m_matrix;
MovablePtr<NewtonBody> m_body;
Vector3f m_forceAccumulator; Vector3f m_forceAccumulator;
Vector3f m_torqueAccumulator; Vector3f m_torqueAccumulator;
NewtonBody* m_body;
PhysWorld3D* m_world; PhysWorld3D* m_world;
void* m_userdata; void* m_userdata;
float m_gravityFactor; float m_gravityFactor;

View File

@ -22,9 +22,22 @@ namespace Nz
m_materialIds.emplace("default", NewtonMaterialGetDefaultGroupID(m_world)); m_materialIds.emplace("default", NewtonMaterialGetDefaultGroupID(m_world));
} }
PhysWorld3D::PhysWorld3D(PhysWorld3D&& physWorld) noexcept :
m_callbacks(std::move(physWorld.m_callbacks)),
m_materialIds(std::move(physWorld.m_materialIds)),
m_maxStepCount(std::move(physWorld.m_maxStepCount)),
m_world(std::move(physWorld.m_world)),
m_gravity(std::move(physWorld.m_gravity)),
m_stepSize(std::move(physWorld.m_stepSize)),
m_timestepAccumulator(std::move(physWorld.m_timestepAccumulator))
{
NewtonWorldSetUserData(m_world, this);
}
PhysWorld3D::~PhysWorld3D() PhysWorld3D::~PhysWorld3D()
{ {
NewtonDestroy(m_world); if (m_world)
NewtonDestroy(m_world);
} }
int PhysWorld3D::CreateMaterial(std::string name) int PhysWorld3D::CreateMaterial(std::string name)
@ -159,6 +172,24 @@ namespace Nz
} }
} }
PhysWorld3D& PhysWorld3D::operator=(PhysWorld3D&& physWorld) noexcept
{
if (m_world)
NewtonDestroy(m_world);
m_callbacks = std::move(physWorld.m_callbacks);
m_materialIds = std::move(physWorld.m_materialIds);
m_maxStepCount = std::move(physWorld.m_maxStepCount);
m_world = std::move(physWorld.m_world);
m_gravity = std::move(physWorld.m_gravity);
m_stepSize = std::move(physWorld.m_stepSize);
m_timestepAccumulator = std::move(physWorld.m_timestepAccumulator);
NewtonWorldSetUserData(m_world, this);
return *this;
}
int PhysWorld3D::OnAABBOverlap(const NewtonJoint* const contactJoint, float /*timestep*/, int /*threadIndex*/) int PhysWorld3D::OnAABBOverlap(const NewtonJoint* const contactJoint, float /*timestep*/, int /*threadIndex*/)
{ {
RigidBody3D* bodyA = static_cast<RigidBody3D*>(NewtonBodyGetUserData(NewtonJointGetBody0(contactJoint))); RigidBody3D* bodyA = static_cast<RigidBody3D*>(NewtonBodyGetUserData(NewtonJointGetBody0(contactJoint)));

View File

@ -61,17 +61,17 @@ namespace Nz
SetRotation(object.GetRotation()); SetRotation(object.GetRotation());
} }
RigidBody3D::RigidBody3D(RigidBody3D&& object) : RigidBody3D::RigidBody3D(RigidBody3D&& object) noexcept :
m_geom(std::move(object.m_geom)), m_geom(std::move(object.m_geom)),
m_matrix(std::move(object.m_matrix)), 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(std::move(object.m_body)),
m_world(object.m_world), m_world(object.m_world),
m_gravityFactor(object.m_gravityFactor), m_gravityFactor(object.m_gravityFactor),
m_mass(object.m_mass) m_mass(object.m_mass)
{ {
object.m_body = nullptr; NewtonBodySetUserData(m_body, this);
} }
RigidBody3D::~RigidBody3D() RigidBody3D::~RigidBody3D()
@ -380,6 +380,24 @@ namespace Nz
return operator=(std::move(physObj)); return operator=(std::move(physObj));
} }
RigidBody3D& RigidBody3D::operator=(RigidBody3D&& object) noexcept
{
if (m_body)
NewtonDestroyBody(m_body);
m_body = std::move(object.m_body);
m_forceAccumulator = std::move(object.m_forceAccumulator);
m_geom = std::move(object.m_geom);
m_gravityFactor = object.m_gravityFactor;
m_mass = object.m_mass;
m_matrix = std::move(object.m_matrix);
m_torqueAccumulator = std::move(object.m_torqueAccumulator);
m_world = object.m_world;
NewtonBodySetUserData(m_body, this);
return *this;
}
void RigidBody3D::UpdateBody() void RigidBody3D::UpdateBody()
{ {
NewtonBodySetMatrix(m_body, m_matrix); NewtonBodySetMatrix(m_body, m_matrix);
@ -401,25 +419,6 @@ namespace Nz
} }
} }
RigidBody3D& RigidBody3D::operator=(RigidBody3D&& object)
{
if (m_body)
NewtonDestroyBody(m_body);
m_body = object.m_body;
m_forceAccumulator = std::move(object.m_forceAccumulator);
m_geom = std::move(object.m_geom);
m_gravityFactor = object.m_gravityFactor;
m_mass = object.m_mass;
m_matrix = std::move(object.m_matrix);
m_torqueAccumulator = std::move(object.m_torqueAccumulator);
m_world = object.m_world;
object.m_body = nullptr;
return *this;
}
void RigidBody3D::ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex) void RigidBody3D::ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex)
{ {
NazaraUnused(timeStep); NazaraUnused(timeStep);