Refactor Collider base class to improve code sharing across collider shape types
This commit is contained in:
@@ -4,9 +4,6 @@
|
|||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Physics/Utilities.h"
|
#include "Engine/Physics/Utilities.h"
|
||||||
#include <ThirdParty/PhysX/PxShape.h>
|
#include <ThirdParty/PhysX/PxShape.h>
|
||||||
#if USE_EDITOR
|
|
||||||
#include "Engine/Level/Scene/SceneRendering.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
BoxCollider::BoxCollider(const SpawnParams& params)
|
BoxCollider::BoxCollider(const SpawnParams& params)
|
||||||
: Collider(params)
|
: Collider(params)
|
||||||
@@ -123,26 +120,6 @@ void BoxCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* mod
|
|||||||
DESERIALIZE_MEMBER(Size, _size);
|
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()
|
void BoxCollider::UpdateBounds()
|
||||||
{
|
{
|
||||||
// Cache bounds
|
// Cache bounds
|
||||||
@@ -152,32 +129,11 @@ void BoxCollider::UpdateBounds()
|
|||||||
BoundingSphere::FromBox(_box, _sphere);
|
BoundingSphere::FromBox(_box, _sphere);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoxCollider::CreateShape()
|
void BoxCollider::GetGeometry(PxGeometryHolder& geometry)
|
||||||
{
|
{
|
||||||
// Setup shape geometry
|
|
||||||
_cachedScale = GetScale();
|
|
||||||
Vector3 size = _size * _cachedScale;
|
Vector3 size = _size * _cachedScale;
|
||||||
size.Absolute();
|
size.Absolute();
|
||||||
const float minSize = 0.001f;
|
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));
|
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);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,12 +47,6 @@ public:
|
|||||||
return _bounds;
|
return _bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
#if USE_EDITOR
|
|
||||||
void DrawPhysicsDebug(RenderView& view);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
@@ -67,11 +61,9 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
#if USE_EDITOR
|
|
||||||
void OnEnable() override;
|
|
||||||
void OnDisable() override;
|
|
||||||
#endif
|
|
||||||
void UpdateBounds() override;
|
void UpdateBounds() override;
|
||||||
void CreateShape() override;
|
void GetGeometry(PxGeometryHolder& geometry) override;
|
||||||
void UpdateGeometry() override;
|
#if USE_EDITOR
|
||||||
|
void DrawPhysicsDebug(RenderView& view) override;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,9 +4,6 @@
|
|||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Physics/Utilities.h"
|
#include "Engine/Physics/Utilities.h"
|
||||||
#include <ThirdParty/PhysX/PxShape.h>
|
#include <ThirdParty/PhysX/PxShape.h>
|
||||||
#if USE_EDITOR
|
|
||||||
#include "Engine/Level/Scene/SceneRendering.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
CapsuleCollider::CapsuleCollider(const SpawnParams& params)
|
CapsuleCollider::CapsuleCollider(const SpawnParams& params)
|
||||||
: Collider(params)
|
: Collider(params)
|
||||||
@@ -93,26 +90,6 @@ void CapsuleCollider::Deserialize(DeserializeStream& stream, ISerializeModifier*
|
|||||||
DESERIALIZE_MEMBER(Height, _height);
|
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()
|
void CapsuleCollider::UpdateBounds()
|
||||||
{
|
{
|
||||||
// Cache bounds
|
// Cache bounds
|
||||||
@@ -123,34 +100,12 @@ void CapsuleCollider::UpdateBounds()
|
|||||||
BoundingSphere::FromBox(_box, _sphere);
|
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 scaling = _cachedScale.GetAbsolute().MaxValue();
|
||||||
const float minSize = 0.001f;
|
const float minSize = 0.001f;
|
||||||
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
|
const float radius = Math::Max(Math::Abs(_radius) * scaling, minSize);
|
||||||
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
|
const float height = Math::Max(Math::Abs(_height) * scaling, minSize);
|
||||||
const PxCapsuleGeometry geometry(radius, height * 0.5f);
|
const PxCapsuleGeometry capsule(radius, height * 0.5f);
|
||||||
|
geometry.storeAny(capsule);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,12 +63,6 @@ public:
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
API_PROPERTY() void SetHeight(float value);
|
API_PROPERTY() void SetHeight(float value);
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
#if USE_EDITOR
|
|
||||||
void DrawPhysicsDebug(RenderView& view);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
@@ -82,11 +76,9 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
#if USE_EDITOR
|
|
||||||
void OnEnable() override;
|
|
||||||
void OnDisable() override;
|
|
||||||
#endif
|
|
||||||
void UpdateBounds() override;
|
void UpdateBounds() override;
|
||||||
void CreateShape() override;
|
void GetGeometry(PxGeometryHolder& geometry) override;
|
||||||
void UpdateGeometry() override;
|
#if USE_EDITOR
|
||||||
|
void DrawPhysicsDebug(RenderView& view) override;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,9 +7,6 @@
|
|||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Engine/Time.h"
|
#include "Engine/Engine/Time.h"
|
||||||
#include "Engine/Physics/PhysicalMaterial.h"
|
#include "Engine/Physics/PhysicalMaterial.h"
|
||||||
#if USE_EDITOR
|
|
||||||
#include "Engine/Level/Scene/SceneRendering.h"
|
|
||||||
#endif
|
|
||||||
#include <ThirdParty/PhysX/PxRigidActor.h>
|
#include <ThirdParty/PhysX/PxRigidActor.h>
|
||||||
#include <ThirdParty/PhysX/PxRigidDynamic.h>
|
#include <ThirdParty/PhysX/PxRigidDynamic.h>
|
||||||
#include <ThirdParty/PhysX/PxPhysics.h>
|
#include <ThirdParty/PhysX/PxPhysics.h>
|
||||||
@@ -276,6 +273,11 @@ void CharacterController::UpdateGeometry()
|
|||||||
UpdateSize();
|
UpdateSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CharacterController::GetGeometry(PxGeometryHolder& geometry)
|
||||||
|
{
|
||||||
|
// Unused
|
||||||
|
}
|
||||||
|
|
||||||
void CharacterController::UpdateLayerBits()
|
void CharacterController::UpdateLayerBits()
|
||||||
{
|
{
|
||||||
// Base
|
// Base
|
||||||
@@ -309,26 +311,6 @@ void CharacterController::EndPlay()
|
|||||||
_shape = nullptr;
|
_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()
|
void CharacterController::OnActiveInTreeChanged()
|
||||||
{
|
{
|
||||||
// Skip collider base
|
// Skip collider base
|
||||||
|
|||||||
@@ -212,12 +212,6 @@ protected:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void UpdateSize() const;
|
void UpdateSize() const;
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
#if USE_EDITOR
|
|
||||||
void DrawPhysicsDebug(RenderView& view);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
@@ -240,12 +234,12 @@ protected:
|
|||||||
|
|
||||||
// [PhysicsActor]
|
// [PhysicsActor]
|
||||||
void UpdateGeometry() override;
|
void UpdateGeometry() override;
|
||||||
|
void GetGeometry(PxGeometryHolder& geometry) override;
|
||||||
void UpdateLayerBits() override;
|
void UpdateLayerBits() override;
|
||||||
void BeginPlay(SceneBeginData* data) override;
|
void BeginPlay(SceneBeginData* data) override;
|
||||||
void EndPlay() override;
|
void EndPlay() override;
|
||||||
#if USE_EDITOR
|
#if USE_EDITOR
|
||||||
void OnEnable() override;
|
void DrawPhysicsDebug(RenderView& view) override;
|
||||||
void OnDisable() override;
|
|
||||||
#endif
|
#endif
|
||||||
void OnActiveInTreeChanged() override;
|
void OnActiveInTreeChanged() override;
|
||||||
void OnParentChanged() override;
|
void OnParentChanged() override;
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include "Collider.h"
|
#include "Collider.h"
|
||||||
#include "Engine/Core/Log.h"
|
#include "Engine/Core/Log.h"
|
||||||
|
#if USE_EDITOR
|
||||||
|
#include "Engine/Level/Scene/SceneRendering.h"
|
||||||
|
#endif
|
||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Physics/Utilities.h"
|
#include "Engine/Physics/Utilities.h"
|
||||||
#include "Engine/Physics/PhysicsSettings.h"
|
#include "Engine/Physics/PhysicsSettings.h"
|
||||||
@@ -182,6 +185,16 @@ bool Collider::IsAttached() const
|
|||||||
return _shape && _shape->getActor() != nullptr;
|
return _shape && _shape->getActor() != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Collider::CanAttach(RigidBody* rigidBody) const
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Collider::CanBeTrigger() const
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
RigidBody* Collider::GetAttachedRigidBody() const
|
RigidBody* Collider::GetAttachedRigidBody() const
|
||||||
{
|
{
|
||||||
if (_shape && _staticActor == nullptr)
|
if (_shape && _staticActor == nullptr)
|
||||||
@@ -193,6 +206,26 @@ RigidBody* Collider::GetAttachedRigidBody() const
|
|||||||
return nullptr;
|
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)
|
void Collider::Attach(RigidBody* rigidBody)
|
||||||
{
|
{
|
||||||
ASSERT(CanAttach(rigidBody));
|
ASSERT(CanAttach(rigidBody));
|
||||||
@@ -238,11 +271,14 @@ void Collider::UpdateLayerBits()
|
|||||||
_shape->setQueryFilterData(filterData);
|
_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 bool isTrigger = _isTrigger && CanBeTrigger();
|
||||||
const PxShapeFlags shapeFlags = GetShapeFlags(isTrigger, IsActiveInHierarchy());
|
const PxShapeFlags shapeFlags = GetShapeFlags(isTrigger, IsActiveInHierarchy());
|
||||||
PxMaterial* material = Physics::GetDefaultMaterial();
|
PxMaterial* material = Physics::GetDefaultMaterial();
|
||||||
@@ -250,17 +286,62 @@ void Collider::CreateShapeBase(const PxGeometry& geometry)
|
|||||||
{
|
{
|
||||||
material = ((PhysicalMaterial*)Material->Instance)->GetPhysXMaterial();
|
material = ((PhysicalMaterial*)Material->Instance)->GetPhysXMaterial();
|
||||||
}
|
}
|
||||||
|
ASSERT(_shape == nullptr);
|
||||||
// Create shape
|
_shape = CPhysX->createShape(geometry.any(), *material, true, shapeFlags);
|
||||||
_shape = CPhysX->createShape(geometry, *material, true, shapeFlags);
|
|
||||||
ASSERT(_shape);
|
ASSERT(_shape);
|
||||||
_shape->userData = this;
|
_shape->userData = this;
|
||||||
|
|
||||||
// Setup properties
|
|
||||||
_shape->setContactOffset(Math::Max(_shape->getRestOffset() + ZeroTolerance, _contactOffset));
|
_shape->setContactOffset(Math::Max(_shape->getRestOffset() + ZeroTolerance, _contactOffset));
|
||||||
UpdateLayerBits();
|
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()
|
void Collider::CreateStaticActor()
|
||||||
{
|
{
|
||||||
ASSERT(_staticActor == nullptr);
|
ASSERT(_staticActor == nullptr);
|
||||||
@@ -285,6 +366,14 @@ void Collider::RemoveStaticActor()
|
|||||||
_staticActor = nullptr;
|
_staticActor = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if USE_EDITOR
|
||||||
|
|
||||||
|
void Collider::DrawPhysicsDebug(RenderView& view)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
void Collider::OnMaterialChanged()
|
void Collider::OnMaterialChanged()
|
||||||
{
|
{
|
||||||
// Update the shape material
|
// Update the shape material
|
||||||
|
|||||||
@@ -160,19 +160,13 @@ public:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="rigidBody">The rigid body.</param>
|
/// <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>
|
/// <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
|
virtual bool CanAttach(RigidBody* rigidBody) const;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether this collider can be a trigger shape.
|
/// Determines whether this collider can be a trigger shape.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns><c>true</c> if this collider can be trigger; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if this collider can be trigger; otherwise, <c>false</c>.</returns>
|
||||||
virtual bool CanBeTrigger() const
|
virtual bool CanBeTrigger() const;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attaches collider to the specified rigid body.
|
/// Attaches collider to the specified rigid body.
|
||||||
@@ -198,20 +192,20 @@ protected:
|
|||||||
virtual void UpdateBounds() = 0;
|
virtual void UpdateBounds() = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the collider shape.
|
/// Gets the collider shape geometry.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
virtual void CreateShape() = 0;
|
/// <param name="geometry">The output geometry.</param>
|
||||||
|
virtual void GetGeometry(PxGeometryHolder& geometry) = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the collider shape from the given geometry.
|
/// Creates the collider shape.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="geometry">The geometry.</param>
|
virtual void CreateShape();
|
||||||
void CreateShapeBase(const PxGeometry& geometry);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the shape geometry.
|
/// Updates the shape geometry.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
virtual void UpdateGeometry() = 0;
|
virtual void UpdateGeometry();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the static actor.
|
/// Creates the static actor.
|
||||||
@@ -223,6 +217,10 @@ protected:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void RemoveStaticActor();
|
void RemoveStaticActor();
|
||||||
|
|
||||||
|
#if USE_EDITOR
|
||||||
|
virtual void DrawPhysicsDebug(RenderView& view);
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void OnMaterialChanged();
|
void OnMaterialChanged();
|
||||||
@@ -237,6 +235,10 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
// [PhysicsColliderActor]
|
// [PhysicsColliderActor]
|
||||||
|
#if USE_EDITOR
|
||||||
|
void OnEnable() override;
|
||||||
|
void OnDisable() override;
|
||||||
|
#endif
|
||||||
void BeginPlay(SceneBeginData* data) override;
|
void BeginPlay(SceneBeginData* data) override;
|
||||||
void EndPlay() override;
|
void EndPlay() override;
|
||||||
void OnActiveInTreeChanged() override;
|
void OnActiveInTreeChanged() override;
|
||||||
|
|||||||
@@ -5,11 +5,6 @@
|
|||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Physics/Utilities.h"
|
#include "Engine/Physics/Utilities.h"
|
||||||
#include "Engine/Physics/Physics.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)
|
MeshCollider::MeshCollider(const SpawnParams& params)
|
||||||
: Collider(params)
|
: Collider(params)
|
||||||
@@ -117,26 +112,6 @@ void MeshCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* mo
|
|||||||
DESERIALIZE(CollisionData);
|
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()
|
void MeshCollider::UpdateBounds()
|
||||||
{
|
{
|
||||||
// Cache bounds
|
// Cache bounds
|
||||||
@@ -149,11 +124,10 @@ void MeshCollider::UpdateBounds()
|
|||||||
BoundingSphere::FromBox(_box, _sphere);
|
BoundingSphere::FromBox(_box, _sphere);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshCollider::CreateShape()
|
void MeshCollider::GetGeometry(PxGeometryHolder& geometry)
|
||||||
{
|
{
|
||||||
// Prepare scale
|
// Prepare scale
|
||||||
Vector3 scale = GetScale();
|
Vector3 scale = _cachedScale;
|
||||||
_cachedScale = scale;
|
|
||||||
scale.Absolute();
|
scale.Absolute();
|
||||||
const float minSize = 0.001f;
|
const float minSize = 0.001f;
|
||||||
scale = Vector3::Max(scale, minSize);
|
scale = Vector3::Max(scale, minSize);
|
||||||
@@ -165,100 +139,23 @@ void MeshCollider::CreateShape()
|
|||||||
if (type == CollisionDataType::ConvexMesh)
|
if (type == CollisionDataType::ConvexMesh)
|
||||||
{
|
{
|
||||||
// Convex mesh
|
// Convex mesh
|
||||||
PxConvexMeshGeometry geometry;
|
PxConvexMeshGeometry convexMesh;
|
||||||
geometry.scale.scale = C2P(scale);
|
convexMesh.scale.scale = C2P(scale);
|
||||||
geometry.convexMesh = CollisionData->GetConvex();
|
convexMesh.convexMesh = CollisionData->GetConvex();
|
||||||
CreateShapeBase(geometry);
|
geometry.storeAny(convexMesh);
|
||||||
}
|
}
|
||||||
else if (type == CollisionDataType::TriangleMesh)
|
else if (type == CollisionDataType::TriangleMesh)
|
||||||
{
|
{
|
||||||
// Triangle mesh
|
// Triangle mesh
|
||||||
PxTriangleMeshGeometry geometry;
|
PxTriangleMeshGeometry triangleMesh;
|
||||||
geometry.scale.scale = C2P(scale);
|
triangleMesh.scale.scale = C2P(scale);
|
||||||
geometry.triangleMesh = CollisionData->GetTriangle();
|
triangleMesh.triangleMesh = CollisionData->GetTriangle();
|
||||||
CreateShapeBase(geometry);
|
geometry.storeAny(triangleMesh);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Dummy geometry
|
// Dummy geometry
|
||||||
const PxSphereGeometry geometry(0.01f);
|
const PxSphereGeometry sphere(minSize);
|
||||||
CreateShapeBase(geometry);
|
geometry.storeAny(sphere);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,6 @@ private:
|
|||||||
|
|
||||||
void OnCollisionDataChanged();
|
void OnCollisionDataChanged();
|
||||||
void OnCollisionDataLoaded();
|
void OnCollisionDataLoaded();
|
||||||
#if USE_EDITOR
|
|
||||||
void DrawPhysicsDebug(RenderView& view);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -45,10 +42,8 @@ protected:
|
|||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
#if USE_EDITOR
|
#if USE_EDITOR
|
||||||
void OnEnable() override;
|
void DrawPhysicsDebug(RenderView& view) override;
|
||||||
void OnDisable() override;
|
|
||||||
#endif
|
#endif
|
||||||
void UpdateBounds() override;
|
void UpdateBounds() override;
|
||||||
void CreateShape() override;
|
void GetGeometry(PxGeometryHolder& geometry) override;
|
||||||
void UpdateGeometry() override;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,9 +4,6 @@
|
|||||||
#include "Engine/Serialization/Serialization.h"
|
#include "Engine/Serialization/Serialization.h"
|
||||||
#include "Engine/Physics/Utilities.h"
|
#include "Engine/Physics/Utilities.h"
|
||||||
#include <PxShape.h>
|
#include <PxShape.h>
|
||||||
#if USE_EDITOR
|
|
||||||
#include "Engine/Level/Scene/SceneRendering.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SphereCollider::SphereCollider(const SpawnParams& params)
|
SphereCollider::SphereCollider(const SpawnParams& params)
|
||||||
: Collider(params)
|
: Collider(params)
|
||||||
@@ -67,26 +64,6 @@ void SphereCollider::Deserialize(DeserializeStream& stream, ISerializeModifier*
|
|||||||
DESERIALIZE_MEMBER(Radius, _radius);
|
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()
|
void SphereCollider::UpdateBounds()
|
||||||
{
|
{
|
||||||
// Cache bounds
|
// Cache bounds
|
||||||
@@ -95,32 +72,11 @@ void SphereCollider::UpdateBounds()
|
|||||||
_sphere.GetBoundingBox(_box);
|
_sphere.GetBoundingBox(_box);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SphereCollider::CreateShape()
|
void SphereCollider::GetGeometry(PxGeometryHolder& geometry)
|
||||||
{
|
{
|
||||||
// Setup shape geometry
|
|
||||||
_cachedScale = GetScale();
|
|
||||||
const float scaling = _cachedScale.GetAbsolute().MaxValue();
|
const float scaling = _cachedScale.GetAbsolute().MaxValue();
|
||||||
const float radius = Math::Abs(_radius) * scaling;
|
const float radius = Math::Abs(_radius) * scaling;
|
||||||
const float minSize = 0.001f;
|
const float minSize = 0.001f;
|
||||||
const PxSphereGeometry geometry(Math::Max(radius, minSize));
|
const PxSphereGeometry sphere(Math::Max(radius, minSize));
|
||||||
|
geometry.storeAny(sphere);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,12 +37,6 @@ public:
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
API_PROPERTY() void SetRadius(float value);
|
API_PROPERTY() void SetRadius(float value);
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
#if USE_EDITOR
|
|
||||||
void DrawPhysicsDebug(RenderView& view);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
@@ -57,10 +51,8 @@ protected:
|
|||||||
|
|
||||||
// [Collider]
|
// [Collider]
|
||||||
#if USE_EDITOR
|
#if USE_EDITOR
|
||||||
void OnEnable() override;
|
void DrawPhysicsDebug(RenderView& view) override;
|
||||||
void OnDisable() override;
|
|
||||||
#endif
|
#endif
|
||||||
void UpdateBounds() override;
|
void UpdateBounds() override;
|
||||||
void CreateShape() override;
|
void GetGeometry(PxGeometryHolder& geometry) override;
|
||||||
void UpdateGeometry() override;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ namespace physx
|
|||||||
class PxFoundation;
|
class PxFoundation;
|
||||||
class PxShape;
|
class PxShape;
|
||||||
class PxGeometry;
|
class PxGeometry;
|
||||||
|
class PxGeometryHolder;
|
||||||
class PxProfileZoneManager;
|
class PxProfileZoneManager;
|
||||||
class PxMaterial;
|
class PxMaterial;
|
||||||
class PxPvd;
|
class PxPvd;
|
||||||
|
|||||||
Reference in New Issue
Block a user