JoltPhysics3D: Fix crash when freeing compoites colliders

This commit is contained in:
SirLynix 2023-03-25 14:19:05 +01:00 committed by Jérôme Leclercq
parent b3595178a1
commit 2b0239b8f0
3 changed files with 22 additions and 3 deletions

View File

@ -52,6 +52,7 @@ namespace Nz
protected: protected:
template<typename T> const T* GetShapeSettingsAs() const; template<typename T> const T* GetShapeSettingsAs() const;
void ResetShapeSettings();
template<typename T> void SetupShapeSettings(std::unique_ptr<T> shapeSettings); template<typename T> void SetupShapeSettings(std::unique_ptr<T> shapeSettings);
private: private:
@ -80,7 +81,7 @@ namespace Nz
struct ChildCollider; struct ChildCollider;
JoltCompoundCollider3D(std::vector<ChildCollider> childs); JoltCompoundCollider3D(std::vector<ChildCollider> childs);
~JoltCompoundCollider3D() = default; ~JoltCompoundCollider3D();
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override; void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;
@ -118,7 +119,7 @@ namespace Nz
inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation); inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation);
inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Quaternionf& rotation); inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Quaternionf& rotation);
JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation, const Quaternionf& rotation); JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation, const Quaternionf& rotation);
~JoltTranslatedRotatedCollider3D() = default; ~JoltTranslatedRotatedCollider3D();
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override; void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;

View File

@ -22,8 +22,9 @@ namespace Nz
template<typename T> template<typename T>
void JoltCollider3D::SetupShapeSettings(std::unique_ptr<T> shapeSettings) void JoltCollider3D::SetupShapeSettings(std::unique_ptr<T> shapeSettings)
{ {
assert(!m_shapeSettings);
shapeSettings->SetEmbedded(); // Call SetEmbedded on the template type to prevent compiler to resolve it outside of a file including Jolt shapeSettings->SetEmbedded(); // Call SetEmbedded on the template type to prevent compiler to resolve it outside of a file including Jolt
assert(!m_shapeSettings);
m_shapeSettings = std::move(shapeSettings); m_shapeSettings = std::move(shapeSettings);
} }

View File

@ -61,6 +61,11 @@ namespace Nz
else else
return nullptr;// std::make_shared<NullCollider3D>(); //< TODO return nullptr;// std::make_shared<NullCollider3D>(); //< TODO
} }
void JoltCollider3D::ResetShapeSettings()
{
m_shapeSettings.reset();
}
std::shared_ptr<JoltCollider3D> JoltCollider3D::CreateGeomFromPrimitive(const Primitive& primitive) std::shared_ptr<JoltCollider3D> JoltCollider3D::CreateGeomFromPrimitive(const Primitive& primitive)
{ {
@ -156,6 +161,12 @@ namespace Nz
SetupShapeSettings(std::move(shapeSettings)); SetupShapeSettings(std::move(shapeSettings));
} }
JoltCompoundCollider3D::~JoltCompoundCollider3D()
{
// We have to destroy shape settings first as it carries references on the inner colliders
ResetShapeSettings();
}
void JoltCompoundCollider3D::BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const void JoltCompoundCollider3D::BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const
{ {
for (const auto& child : m_childs) for (const auto& child : m_childs)
@ -199,6 +210,12 @@ namespace Nz
SetupShapeSettings(std::make_unique<JPH::RotatedTranslatedShapeSettings>(ToJolt(translation), ToJolt(rotation), m_collider->GetShapeSettings())); SetupShapeSettings(std::make_unique<JPH::RotatedTranslatedShapeSettings>(ToJolt(translation), ToJolt(rotation), m_collider->GetShapeSettings()));
} }
JoltTranslatedRotatedCollider3D::~JoltTranslatedRotatedCollider3D()
{
// We have to destroy shape settings first as it carries references on the inner collider
ResetShapeSettings();
}
void JoltTranslatedRotatedCollider3D::BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const void JoltTranslatedRotatedCollider3D::BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const
{ {
const JPH::RotatedTranslatedShapeSettings* settings = GetShapeSettingsAs<JPH::RotatedTranslatedShapeSettings>(); const JPH::RotatedTranslatedShapeSettings* settings = GetShapeSettingsAs<JPH::RotatedTranslatedShapeSettings>();