From efc81ff508170a2ede21cd048551985594e749fb Mon Sep 17 00:00:00 2001 From: SirLynix Date: Thu, 30 Mar 2023 13:23:58 +0200 Subject: [PATCH] BulletPhysics3D: Fix movement --- .../BulletPhysics3D/BulletConstraint3D.hpp | 2 ++ .../BulletPhysics3D/BulletConstraint3D.cpp | 21 +++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/Nazara/BulletPhysics3D/BulletConstraint3D.hpp b/include/Nazara/BulletPhysics3D/BulletConstraint3D.hpp index bcf3ba44d..7d48d54ee 100644 --- a/include/Nazara/BulletPhysics3D/BulletConstraint3D.hpp +++ b/include/Nazara/BulletPhysics3D/BulletConstraint3D.hpp @@ -49,6 +49,8 @@ namespace Nz template const T* GetConstraint() const; private: + void Destroy(); + std::unique_ptr m_constraint; bool m_bodyCollisionEnabled; }; diff --git a/src/Nazara/BulletPhysics3D/BulletConstraint3D.cpp b/src/Nazara/BulletPhysics3D/BulletConstraint3D.cpp index ec68bfa36..e9377712b 100644 --- a/src/Nazara/BulletPhysics3D/BulletConstraint3D.cpp +++ b/src/Nazara/BulletPhysics3D/BulletConstraint3D.cpp @@ -14,6 +14,8 @@ namespace Nz m_constraint(std::move(constraint)), m_bodyCollisionEnabled(!disableCollisions) { + m_constraint->setUserConstraintPtr(this); + btDynamicsWorld* world = GetWorld().GetDynamicsWorld(); world->addConstraint(m_constraint.get(), disableCollisions); } @@ -28,11 +30,7 @@ namespace Nz BulletConstraint3D::~BulletConstraint3D() { - if (m_constraint) - { - btDynamicsWorld* world = GetWorld().GetDynamicsWorld(); - world->removeConstraint(m_constraint.get()); - } + Destroy(); } BulletRigidBody3D& BulletConstraint3D::GetBodyA() @@ -74,7 +72,7 @@ namespace Nz BulletConstraint3D& BulletConstraint3D::operator=(BulletConstraint3D&& constraint) noexcept { - m_constraint.reset(); + Destroy(); m_constraint = std::move(constraint.m_constraint); m_bodyCollisionEnabled = constraint.m_bodyCollisionEnabled; @@ -85,6 +83,17 @@ namespace Nz return *this; } + void BulletConstraint3D::Destroy() + { + if (m_constraint) + { + btDynamicsWorld* world = GetWorld().GetDynamicsWorld(); + world->removeConstraint(m_constraint.get()); + + m_constraint.reset(); + } + } + BulletPivotConstraint3D::BulletPivotConstraint3D(BulletRigidBody3D& first, const Vector3f& pivot) : BulletConstraint3D(std::make_unique(*first.GetRigidBody(), ToBullet(first.ToLocal(pivot))))