Refactor Collider base class to improve code sharing across collider shape types

This commit is contained in:
Wojtek Figat
2021-02-10 14:33:16 +01:00
parent 5cb2322270
commit 8bce445ab0
13 changed files with 154 additions and 351 deletions

View File

@@ -4,9 +4,6 @@
#include "Engine/Serialization/Serialization.h"
#include "Engine/Physics/Utilities.h"
#include <ThirdParty/PhysX/PxShape.h>
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
BoxCollider::BoxCollider(const SpawnParams& params)
: Collider(params)
@@ -123,26 +120,6 @@ void BoxCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* mod
DESERIALIZE_MEMBER(Size, _size);
}
#if USE_EDITOR
void BoxCollider::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<BoxCollider, &BoxCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnEnable();
}
void BoxCollider::OnDisable()
{
GetSceneRendering()->RemovePhysicsDebug<BoxCollider, &BoxCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnDisable();
}
#endif
void BoxCollider::UpdateBounds()
{
// Cache bounds
@@ -152,32 +129,11 @@ void BoxCollider::UpdateBounds()
BoundingSphere::FromBox(_box, _sphere);
}
void BoxCollider::CreateShape()
void BoxCollider::GetGeometry(PxGeometryHolder& geometry)
{
// Setup shape geometry
_cachedScale = GetScale();
Vector3 size = _size * _cachedScale;
size.Absolute();
const float minSize = 0.001f;
const PxBoxGeometry geometry(Math::Max(size.X * 0.5f, minSize), Math::Max(size.Y * 0.5f, minSize), Math::Max(size.Z * 0.5f, minSize));
// Setup shape
CreateShapeBase(geometry);
}
void BoxCollider::UpdateGeometry()
{
// Check if has no shape created
if (_shape == nullptr)
return;
// Setup shape geometry
_cachedScale = GetScale();
Vector3 size = _size * _cachedScale;
size.Absolute();
const float minSize = 0.001f;
const PxBoxGeometry geometry(Math::Max(size.X * 0.5f, minSize), Math::Max(size.Y * 0.5f, minSize), Math::Max(size.Z * 0.5f, minSize));
// Setup shape
_shape->setGeometry(geometry);
const PxBoxGeometry box(Math::Max(size.X * 0.5f, minSize), Math::Max(size.Y * 0.5f, minSize), Math::Max(size.Z * 0.5f, minSize));
geometry.storeAny(box);
}

View File

@@ -47,12 +47,6 @@ public:
return _bounds;
}
private:
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view);
#endif
public:
// [Collider]
@@ -67,11 +61,9 @@ public:
protected:
// [Collider]
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
#endif
void UpdateBounds() override;
void CreateShape() override;
void UpdateGeometry() override;
void GetGeometry(PxGeometryHolder& geometry) override;
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view) override;
#endif
};

View File

@@ -4,9 +4,6 @@
#include "Engine/Serialization/Serialization.h"
#include "Engine/Physics/Utilities.h"
#include <ThirdParty/PhysX/PxShape.h>
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
CapsuleCollider::CapsuleCollider(const SpawnParams& params)
: Collider(params)
@@ -93,26 +90,6 @@ void CapsuleCollider::Deserialize(DeserializeStream& stream, ISerializeModifier*
DESERIALIZE_MEMBER(Height, _height);
}
#if USE_EDITOR
void CapsuleCollider::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<CapsuleCollider, &CapsuleCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnEnable();
}
void CapsuleCollider::OnDisable()
{
GetSceneRendering()->RemovePhysicsDebug<CapsuleCollider, &CapsuleCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnDisable();
}
#endif
void CapsuleCollider::UpdateBounds()
{
// Cache bounds
@@ -123,34 +100,12 @@ void CapsuleCollider::UpdateBounds()
BoundingSphere::FromBox(_box, _sphere);
}
void CapsuleCollider::CreateShape()
void CapsuleCollider::GetGeometry(PxGeometryHolder& geometry)
{
// Setup shape geometry
_cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
const PxCapsuleGeometry geometry(radius, height * 0.5f);
// Setup shape
CreateShapeBase(geometry);
}
void CapsuleCollider::UpdateGeometry()
{
// Check if has no shape created
if (_shape == nullptr)
return;
// Setup shape geometry
_cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float minSize = 0.001f;
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
const PxCapsuleGeometry geometry(radius, height * 0.5f);
// Setup shape
_shape->setGeometry(geometry);
const PxCapsuleGeometry capsule(radius, height * 0.5f);
geometry.storeAny(capsule);
}

View File

@@ -63,12 +63,6 @@ public:
/// </remarks>
API_PROPERTY() void SetHeight(float value);
private:
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view);
#endif
public:
// [Collider]
@@ -82,11 +76,9 @@ public:
protected:
// [Collider]
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
#endif
void UpdateBounds() override;
void CreateShape() override;
void UpdateGeometry() override;
void GetGeometry(PxGeometryHolder& geometry) override;
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view) override;
#endif
};

View File

@@ -7,9 +7,6 @@
#include "Engine/Serialization/Serialization.h"
#include "Engine/Engine/Time.h"
#include "Engine/Physics/PhysicalMaterial.h"
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
#include <ThirdParty/PhysX/PxRigidActor.h>
#include <ThirdParty/PhysX/PxRigidDynamic.h>
#include <ThirdParty/PhysX/PxPhysics.h>
@@ -276,6 +273,11 @@ void CharacterController::UpdateGeometry()
UpdateSize();
}
void CharacterController::GetGeometry(PxGeometryHolder& geometry)
{
// Unused
}
void CharacterController::UpdateLayerBits()
{
// Base
@@ -309,26 +311,6 @@ void CharacterController::EndPlay()
_shape = nullptr;
}
#if USE_EDITOR
void CharacterController::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<CharacterController, &CharacterController::DrawPhysicsDebug>(this);
// Base
Collider::OnEnable();
}
void CharacterController::OnDisable()
{
GetSceneRendering()->RemovePhysicsDebug<CharacterController, &CharacterController::DrawPhysicsDebug>(this);
// Base
Collider::OnDisable();
}
#endif
void CharacterController::OnActiveInTreeChanged()
{
// Skip collider base

View File

@@ -212,12 +212,6 @@ protected:
/// </summary>
void UpdateSize() const;
private:
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view);
#endif
public:
// [Collider]
@@ -240,12 +234,12 @@ protected:
// [PhysicsActor]
void UpdateGeometry() override;
void GetGeometry(PxGeometryHolder& geometry) override;
void UpdateLayerBits() override;
void BeginPlay(SceneBeginData* data) override;
void EndPlay() override;
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
void DrawPhysicsDebug(RenderView& view) override;
#endif
void OnActiveInTreeChanged() override;
void OnParentChanged() override;

View File

@@ -2,6 +2,9 @@
#include "Collider.h"
#include "Engine/Core/Log.h"
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
#include "Engine/Serialization/Serialization.h"
#include "Engine/Physics/Utilities.h"
#include "Engine/Physics/PhysicsSettings.h"
@@ -182,6 +185,16 @@ bool Collider::IsAttached() const
return _shape && _shape->getActor() != nullptr;
}
bool Collider::CanAttach(RigidBody* rigidBody) const
{
return true;
}
bool Collider::CanBeTrigger() const
{
return true;
}
RigidBody* Collider::GetAttachedRigidBody() const
{
if (_shape && _staticActor == nullptr)
@@ -193,6 +206,26 @@ RigidBody* Collider::GetAttachedRigidBody() const
return nullptr;
}
#if USE_EDITOR
void Collider::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<Collider, &Collider::DrawPhysicsDebug>(this);
// Base
Actor::OnEnable();
}
void Collider::OnDisable()
{
// Base
Actor::OnDisable();
GetSceneRendering()->RemovePhysicsDebug<Collider, &Collider::DrawPhysicsDebug>(this);
}
#endif
void Collider::Attach(RigidBody* rigidBody)
{
ASSERT(CanAttach(rigidBody));
@@ -238,11 +271,14 @@ void Collider::UpdateLayerBits()
_shape->setQueryFilterData(filterData);
}
void Collider::CreateShapeBase(const PxGeometry& geometry)
void Collider::CreateShape()
{
ASSERT(_shape == nullptr);
// Setup shape geometry
_cachedScale = GetScale();
PxGeometryHolder geometry;
GetGeometry(geometry);
// Prepare
// Create shape
const bool isTrigger = _isTrigger && CanBeTrigger();
const PxShapeFlags shapeFlags = GetShapeFlags(isTrigger, IsActiveInHierarchy());
PxMaterial* material = Physics::GetDefaultMaterial();
@@ -250,17 +286,62 @@ void Collider::CreateShapeBase(const PxGeometry& geometry)
{
material = ((PhysicalMaterial*)Material->Instance)->GetPhysXMaterial();
}
// Create shape
_shape = CPhysX->createShape(geometry, *material, true, shapeFlags);
ASSERT(_shape == nullptr);
_shape = CPhysX->createShape(geometry.any(), *material, true, shapeFlags);
ASSERT(_shape);
_shape->userData = this;
// Setup properties
_shape->setContactOffset(Math::Max(_shape->getRestOffset() + ZeroTolerance, _contactOffset));
UpdateLayerBits();
}
void Collider::UpdateGeometry()
{
if (_shape == nullptr)
return;
// Setup shape geometry
_cachedScale = GetScale();
PxGeometryHolder geometry;
GetGeometry(geometry);
// Recreate shape if geometry has different type
if (_shape->getGeometryType() != geometry.getType())
{
// Detach from the actor
auto actor = _shape->getActor();
if (actor)
actor->detachShape(*_shape);
// Release shape
Physics::RemoveCollider(this);
_shape->release();
_shape = nullptr;
// Recreate shape
CreateShape();
// Reattach again (only if can, see CanAttach function)
if (actor)
{
const auto rigidBody = dynamic_cast<RigidBody*>(GetParent());
if (_staticActor != nullptr || (rigidBody && CanAttach(rigidBody)))
{
actor->attachShape(*_shape);
}
else
{
// Be static triangle mesh
CreateStaticActor();
}
}
return;
}
// Update shape
_shape->setGeometry(geometry.any());
}
void Collider::CreateStaticActor()
{
ASSERT(_staticActor == nullptr);
@@ -285,6 +366,14 @@ void Collider::RemoveStaticActor()
_staticActor = nullptr;
}
#if USE_EDITOR
void Collider::DrawPhysicsDebug(RenderView& view)
{
}
#endif
void Collider::OnMaterialChanged()
{
// Update the shape material

View File

@@ -160,19 +160,13 @@ public:
/// </summary>
/// <param name="rigidBody">The rigid body.</param>
/// <returns><c>true</c> if this collider can be attached the specified rigid body; otherwise, <c>false</c>.</returns>
virtual bool CanAttach(RigidBody* rigidBody) const
{
return true;
}
virtual bool CanAttach(RigidBody* rigidBody) const;
/// <summary>
/// Determines whether this collider can be a trigger shape.
/// </summary>
/// <returns><c>true</c> if this collider can be trigger; otherwise, <c>false</c>.</returns>
virtual bool CanBeTrigger() const
{
return true;
}
virtual bool CanBeTrigger() const;
/// <summary>
/// Attaches collider to the specified rigid body.
@@ -198,20 +192,20 @@ protected:
virtual void UpdateBounds() = 0;
/// <summary>
/// Creates the collider shape.
/// Gets the collider shape geometry.
/// </summary>
virtual void CreateShape() = 0;
/// <param name="geometry">The output geometry.</param>
virtual void GetGeometry(PxGeometryHolder& geometry) = 0;
/// <summary>
/// Creates the collider shape from the given geometry.
/// Creates the collider shape.
/// </summary>
/// <param name="geometry">The geometry.</param>
void CreateShapeBase(const PxGeometry& geometry);
virtual void CreateShape();
/// <summary>
/// Updates the shape geometry.
/// </summary>
virtual void UpdateGeometry() = 0;
virtual void UpdateGeometry();
/// <summary>
/// Creates the static actor.
@@ -223,6 +217,10 @@ protected:
/// </summary>
void RemoveStaticActor();
#if USE_EDITOR
virtual void DrawPhysicsDebug(RenderView& view);
#endif
private:
void OnMaterialChanged();
@@ -237,6 +235,10 @@ public:
protected:
// [PhysicsColliderActor]
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
#endif
void BeginPlay(SceneBeginData* data) override;
void EndPlay() override;
void OnActiveInTreeChanged() override;

View File

@@ -5,11 +5,6 @@
#include "Engine/Serialization/Serialization.h"
#include "Engine/Physics/Utilities.h"
#include "Engine/Physics/Physics.h"
#include <ThirdParty/PhysX/PxShape.h>
#include <ThirdParty/PhysX/PxRigidActor.h>
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
MeshCollider::MeshCollider(const SpawnParams& params)
: Collider(params)
@@ -117,26 +112,6 @@ void MeshCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* mo
DESERIALIZE(CollisionData);
}
#if USE_EDITOR
void MeshCollider::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<MeshCollider, &MeshCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnEnable();
}
void MeshCollider::OnDisable()
{
GetSceneRendering()->RemovePhysicsDebug<MeshCollider, &MeshCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnDisable();
}
#endif
void MeshCollider::UpdateBounds()
{
// Cache bounds
@@ -149,11 +124,10 @@ void MeshCollider::UpdateBounds()
BoundingSphere::FromBox(_box, _sphere);
}
void MeshCollider::CreateShape()
void MeshCollider::GetGeometry(PxGeometryHolder& geometry)
{
// Prepare scale
Vector3 scale = GetScale();
_cachedScale = scale;
Vector3 scale = _cachedScale;
scale.Absolute();
const float minSize = 0.001f;
scale = Vector3::Max(scale, minSize);
@@ -165,100 +139,23 @@ void MeshCollider::CreateShape()
if (type == CollisionDataType::ConvexMesh)
{
// Convex mesh
PxConvexMeshGeometry geometry;
geometry.scale.scale = C2P(scale);
geometry.convexMesh = CollisionData->GetConvex();
CreateShapeBase(geometry);
PxConvexMeshGeometry convexMesh;
convexMesh.scale.scale = C2P(scale);
convexMesh.convexMesh = CollisionData->GetConvex();
geometry.storeAny(convexMesh);
}
else if (type == CollisionDataType::TriangleMesh)
{
// Triangle mesh
PxTriangleMeshGeometry geometry;
geometry.scale.scale = C2P(scale);
geometry.triangleMesh = CollisionData->GetTriangle();
CreateShapeBase(geometry);
PxTriangleMeshGeometry triangleMesh;
triangleMesh.scale.scale = C2P(scale);
triangleMesh.triangleMesh = CollisionData->GetTriangle();
geometry.storeAny(triangleMesh);
}
else
{
// Dummy geometry
const PxSphereGeometry geometry(0.01f);
CreateShapeBase(geometry);
}
}
void MeshCollider::UpdateGeometry()
{
// Check if has no shape created
if (_shape == nullptr)
return;
// Recreate shape if geometry has different type
CollisionDataType type = CollisionDataType::None;
if (CollisionData && CollisionData->IsLoaded())
type = CollisionData->GetOptions().Type;
if ((type == CollisionDataType::ConvexMesh && _shape->getGeometryType() != PxGeometryType::eCONVEXMESH)
|| (type == CollisionDataType::TriangleMesh && _shape->getGeometryType() != PxGeometryType::eTRIANGLEMESH)
|| (type == CollisionDataType::None && _shape->getGeometryType() != PxGeometryType::eSPHERE)
)
{
// Detach from the actor
auto actor = _shape->getActor();
if (actor)
actor->detachShape(*_shape);
// Release shape
Physics::RemoveCollider(this);
_shape->release();
_shape = nullptr;
// Recreate shape
CreateShape();
// Reattach again (only if can, see CanAttach function)
if (actor)
{
if (_staticActor != nullptr || type != CollisionDataType::TriangleMesh)
{
actor->attachShape(*_shape);
}
else
{
// Be static triangle mesh
CreateStaticActor();
}
}
return;
}
// Prepare scale
Vector3 scale = GetScale();
_cachedScale = scale;
scale.Absolute();
const float minSize = 0.001f;
scale = Vector3::Max(scale, minSize);
// Setup shape (based on type)
if (type == CollisionDataType::ConvexMesh)
{
// Convex mesh
PxConvexMeshGeometry geometry;
geometry.scale.scale = C2P(scale);
geometry.convexMesh = CollisionData->GetConvex();
_shape->setGeometry(geometry);
}
else if (type == CollisionDataType::TriangleMesh)
{
// Triangle mesh
PxTriangleMeshGeometry geometry;
geometry.scale.scale = C2P(scale);
geometry.triangleMesh = CollisionData->GetTriangle();
_shape->setGeometry(geometry);
}
else
{
// Dummy geometry
const PxSphereGeometry geometry(0.01f);
_shape->setGeometry(geometry);
const PxSphereGeometry sphere(minSize);
geometry.storeAny(sphere);
}
}

View File

@@ -25,9 +25,6 @@ private:
void OnCollisionDataChanged();
void OnCollisionDataLoaded();
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view);
#endif
public:
@@ -45,10 +42,8 @@ protected:
// [Collider]
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
void DrawPhysicsDebug(RenderView& view) override;
#endif
void UpdateBounds() override;
void CreateShape() override;
void UpdateGeometry() override;
void GetGeometry(PxGeometryHolder& geometry) override;
};

View File

@@ -4,9 +4,6 @@
#include "Engine/Serialization/Serialization.h"
#include "Engine/Physics/Utilities.h"
#include <PxShape.h>
#if USE_EDITOR
#include "Engine/Level/Scene/SceneRendering.h"
#endif
SphereCollider::SphereCollider(const SpawnParams& params)
: Collider(params)
@@ -67,26 +64,6 @@ void SphereCollider::Deserialize(DeserializeStream& stream, ISerializeModifier*
DESERIALIZE_MEMBER(Radius, _radius);
}
#if USE_EDITOR
void SphereCollider::OnEnable()
{
GetSceneRendering()->AddPhysicsDebug<SphereCollider, &SphereCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnEnable();
}
void SphereCollider::OnDisable()
{
GetSceneRendering()->RemovePhysicsDebug<SphereCollider, &SphereCollider::DrawPhysicsDebug>(this);
// Base
Collider::OnDisable();
}
#endif
void SphereCollider::UpdateBounds()
{
// Cache bounds
@@ -95,32 +72,11 @@ void SphereCollider::UpdateBounds()
_sphere.GetBoundingBox(_box);
}
void SphereCollider::CreateShape()
void SphereCollider::GetGeometry(PxGeometryHolder& geometry)
{
// Setup shape geometry
_cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float radius = Math::Abs(_radius) * scaling;
const float minSize = 0.001f;
const PxSphereGeometry geometry(Math::Max(radius, minSize));
// Setup shape
CreateShapeBase(geometry);
}
void SphereCollider::UpdateGeometry()
{
// Check if has no shape created
if (_shape == nullptr)
return;
// Setup shape geometry
_cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue();
const float radius = Math::Abs(_radius) * scaling;
const float minSize = 0.001f;
const PxSphereGeometry geometry(Math::Max(radius, minSize));
// Setup shape
_shape->setGeometry(geometry);
const PxSphereGeometry sphere(Math::Max(radius, minSize));
geometry.storeAny(sphere);
}

View File

@@ -37,12 +37,6 @@ public:
/// </remarks>
API_PROPERTY() void SetRadius(float value);
private:
#if USE_EDITOR
void DrawPhysicsDebug(RenderView& view);
#endif
public:
// [Collider]
@@ -57,10 +51,8 @@ protected:
// [Collider]
#if USE_EDITOR
void OnEnable() override;
void OnDisable() override;
void DrawPhysicsDebug(RenderView& view) override;
#endif
void UpdateBounds() override;
void CreateShape() override;
void UpdateGeometry() override;
void GetGeometry(PxGeometryHolder& geometry) override;
};

View File

@@ -28,6 +28,7 @@ namespace physx
class PxFoundation;
class PxShape;
class PxGeometry;
class PxGeometryHolder;
class PxProfileZoneManager;
class PxMaterial;
class PxPvd;