From 621f8a5f381122d07f08387f5884e17839fa838c Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 9 Mar 2023 15:41:27 +0800 Subject: [PATCH] Added Capsule Collider to Managed --- SHADE_Managed/src/Components/Collider.cxx | 100 +++++++++++----------- SHADE_Managed/src/Components/Collider.h++ | 3 +- SHADE_Managed/src/Components/Collider.hxx | 86 ++++++++++--------- 3 files changed, 101 insertions(+), 88 deletions(-) diff --git a/SHADE_Managed/src/Components/Collider.cxx b/SHADE_Managed/src/Components/Collider.cxx index ac191cdd..fcabfb0a 100644 --- a/SHADE_Managed/src/Components/Collider.cxx +++ b/SHADE_Managed/src/Components/Collider.cxx @@ -16,6 +16,7 @@ of DigiPen Institute of Technology is prohibited. #include "Collider.hxx" #include "Physics/Collision/Shapes/SHBox.h" +#include "Physics/Collision/Shapes/SHCapsule.h" #include "Physics/Collision/Shapes/SHSphere.h" #include "Utility/Debug.hxx" @@ -118,6 +119,29 @@ namespace SHADE } } + /*---------------------------------------------------------------------------------*/ + /* SphereCollider - Properties */ + /*---------------------------------------------------------------------------------*/ + Vector3 SphereCollider::Center::get() + { + return Convert::ToCLI(getNativeCollisionShape().GetWorldCentroid()); + } + float SphereCollider::Radius::get() + { + return getNativeCollisionShape().GetWorldRadius(); + } + void SphereCollider::Radius::set(float value) + { + getNativeCollisionShape().SetWorldRadius(value); + } + + /*---------------------------------------------------------------------------------*/ + /* SphereCollider - Constructors */ + /*---------------------------------------------------------------------------------*/ + SphereCollider::SphereCollider(int arrayIndex, Entity attachedEntity) + : CollisionShape{ arrayIndex, attachedEntity } + {} + /*---------------------------------------------------------------------------------*/ /* BoxCollider - Constructors */ /*---------------------------------------------------------------------------------*/ @@ -147,53 +171,33 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* BoxCollider - Usage Functions */ + /* CapsuleCollider - Properties */ /*---------------------------------------------------------------------------------*/ - bool BoxCollider::TestPoint(Vector3 point) + Vector3 CapsuleCollider::Center::get() { - //return getNativeCollisionShape().TestPoint(Convert::ToNative(point)); - return false; + return Convert::ToCLI(getNativeCollisionShape().GetWorldCentroid()); } - bool BoxCollider::Raycast(Ray ray, float maxDistance) + float CapsuleCollider::Radius::get() { - //return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); - return false; + return getNativeCollisionShape().GetWorldRadius(); + } + void CapsuleCollider::Radius::set(float value) + { + getNativeCollisionShape().SetWorldRadius(value); + } + float CapsuleCollider::Height::get() + { + return getNativeCollisionShape().GetWorldHeight(); + } + void CapsuleCollider::Height::set(float value) + { + getNativeCollisionShape().SetWorldHeight(value); } /*---------------------------------------------------------------------------------*/ - /* SphereCollider - Properties */ + /* CapsuleCollider - Constructors */ /*---------------------------------------------------------------------------------*/ - Vector3 SphereCollider::Center::get() - { - return Convert::ToCLI(getNativeCollisionShape().GetWorldCentroid()); - } - float SphereCollider::Radius::get() - { - return getNativeCollisionShape().GetWorldRadius(); - } - void SphereCollider::Radius::set(float value) - { - getNativeCollisionShape().SetWorldRadius(value); - } - - /*---------------------------------------------------------------------------------*/ - /* SphereCollider - Usage Functions */ - /*---------------------------------------------------------------------------------*/ - bool SphereCollider::TestPoint(Vector3 point) - { - //return getNativeCollisionShape().TestPoint(Convert::ToNative(point)); - return false; - } - bool SphereCollider::Raycast(Ray ray, float maxDistance) - { - //return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); - return false; - } - - /*---------------------------------------------------------------------------------*/ - /* SphereCollider - Constructors */ - /*---------------------------------------------------------------------------------*/ - SphereCollider::SphereCollider(int arrayIndex, Entity attachedEntity) + CapsuleCollider::CapsuleCollider(int arrayIndex, Entity attachedEntity) : CollisionShape{ arrayIndex, attachedEntity } {} @@ -303,18 +307,18 @@ namespace SHADE int i = 0; for (const auto& collider : GetNativeComponent()->GetCollisionShapes()) { - CollisionShape^ bound = nullptr; + CollisionShape^ shape = nullptr; switch (collider->GetType()) { - case SHCollisionShape::Type::BOX: - bound = gcnew BoxCollider(i, Owner.GetEntity()); - break; case SHCollisionShape::Type::SPHERE: - bound = gcnew SphereCollider(i, Owner.GetEntity()); + shape = gcnew SphereCollider(i, Owner.GetEntity()); + break; + case SHCollisionShape::Type::BOX: + shape = gcnew BoxCollider(i, Owner.GetEntity()); + break; + case SHCollisionShape::Type::CAPSULE: + shape = gcnew CapsuleCollider(i, Owner.GetEntity()); break; - //case SHCollisionShape::Type::CAPSULE: - // // TODO - // break; default: Debug::LogWarning("[Collider] An invalid Collider Type was detected. Skipping."); break; @@ -322,7 +326,7 @@ namespace SHADE ++i; // Add into list - subColliderList->Add(bound); + subColliderList->Add(shape); } } } \ No newline at end of file diff --git a/SHADE_Managed/src/Components/Collider.h++ b/SHADE_Managed/src/Components/Collider.h++ index b16e8063..8dc13e01 100644 --- a/SHADE_Managed/src/Components/Collider.h++ +++ b/SHADE_Managed/src/Components/Collider.h++ @@ -16,6 +16,7 @@ of DigiPen Institute of Technology is prohibited. // Primary Include #include "Component.hxx" + namespace SHADE { template @@ -28,7 +29,7 @@ namespace SHADE try { auto& shape = collider->GetCollisionShape(arrayIndex); - if (shape.GetType() != SHCollisionShape::Type::BOX) + if (shape.GetType() == SHCollisionShape::Type::INVALID) throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); return dynamic_cast(shape); diff --git a/SHADE_Managed/src/Components/Collider.hxx b/SHADE_Managed/src/Components/Collider.hxx index 3c7b060e..d934affd 100644 --- a/SHADE_Managed/src/Components/Collider.hxx +++ b/SHADE_Managed/src/Components/Collider.hxx @@ -87,23 +87,6 @@ namespace SHADE void set(float value); } - /*-----------------------------------------------------------------------------*/ - /* Usage Functions */ - /*-----------------------------------------------------------------------------*/ - /// - /// Checks if the specified point is within this shape's bounds. - /// - /// Point to test with. - /// True if the point is in the shape's bounds. - virtual bool TestPoint(Vector3 point) = 0; - /// - /// Computes a Raycast and checks if there is a collision with any object. - /// - /// The ray to cast. - /// Maximum distance for the raycast check. - /// True if the ray intersects with an object in the scene. - virtual bool Raycast(Ray ray, float maxDistance) = 0; - protected: /*-----------------------------------------------------------------------------*/ /* Constructors */ @@ -135,7 +118,39 @@ namespace SHADE }; /// - /// Box-shaped Collider Bound. + /// A Sphere Collider + /// + public ref class SphereCollider : public CollisionShape + { + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Center of the sphere. + /// + property Vector3 Center + { + Vector3 get(); + } + /// + /// Radius of the sphere/ + /// + property float Radius + { + float get(); + void set(float value); + } + + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + SphereCollider(int arrayIndex, Entity attachedEntity); + }; + + /// + /// A Box Collider /// public ref class BoxCollider : public CollisionShape { @@ -166,14 +181,6 @@ namespace SHADE Quaternion get(); } - /*-----------------------------------------------------------------------------*/ - /* ColliderBound Functions */ - /*-----------------------------------------------------------------------------*/ - /// - bool TestPoint(Vector3 point) override; - /// - bool Raycast(Ray ray, float maxDistance) override; - internal: /*-----------------------------------------------------------------------------*/ /* Constructors */ @@ -182,44 +189,45 @@ namespace SHADE }; /// - /// Sphere-shaped Collider Bound. + /// A Capsule Collider /// - public ref class SphereCollider : public CollisionShape + public ref class CapsuleCollider : public CollisionShape { public: /*-----------------------------------------------------------------------------*/ /* Properties */ /*-----------------------------------------------------------------------------*/ /// - /// Center of the sphere. + /// Center of the capsule. /// property Vector3 Center { Vector3 get(); } /// - /// Radius of the Bounding Sphere formed by this bound. + /// Radius of the capsule. /// property float Radius { float get(); void set(float value); } - - /*-----------------------------------------------------------------------------*/ - /* ColliderBound Functions */ - /*-----------------------------------------------------------------------------*/ - /// - bool TestPoint(Vector3 point) override; - /// - bool Raycast(Ray ray, float maxDistance) override; + /// + /// Height of the capsule. + /// + property float Height + { + float get(); + void set(float value); + } internal: /*-----------------------------------------------------------------------------*/ /* Constructors */ /*-----------------------------------------------------------------------------*/ - SphereCollider(int arrayIndex, Entity attachedEntity); + CapsuleCollider(int arrayIndex, Entity attachedEntity); }; + /// /// CLR version of the the SHADE Engine's SHColliderComponent. -- 2.40.1