Added Capsule Collider to C# #409

Merged
direnbharwani merged 6 commits from SP3-2-Physics into main 2023-03-09 16:12:21 +08:00
3 changed files with 101 additions and 88 deletions
Showing only changes of commit 621f8a5f38 - Show all commits

View File

@ -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<SHSphere>().GetWorldCentroid());
}
float SphereCollider::Radius::get()
{
return getNativeCollisionShape<SHSphere>().GetWorldRadius();
}
void SphereCollider::Radius::set(float value)
{
getNativeCollisionShape<SHSphere>().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<SHAABB>().TestPoint(Convert::ToNative(point));
return false;
return Convert::ToCLI(getNativeCollisionShape<SHCapsule>().GetWorldCentroid());
}
bool BoxCollider::Raycast(Ray ray, float maxDistance)
float CapsuleCollider::Radius::get()
{
//return getNativeCollisionShape<SHAABB>().Raycast(Convert::ToNative(ray));
return false;
return getNativeCollisionShape<SHCapsule>().GetWorldRadius();
}
void CapsuleCollider::Radius::set(float value)
{
getNativeCollisionShape<SHCapsule>().SetWorldRadius(value);
}
float CapsuleCollider::Height::get()
{
return getNativeCollisionShape<SHCapsule>().GetWorldHeight();
}
void CapsuleCollider::Height::set(float value)
{
getNativeCollisionShape<SHCapsule>().SetWorldHeight(value);
}
/*---------------------------------------------------------------------------------*/
/* SphereCollider - Properties */
/* CapsuleCollider - Constructors */
/*---------------------------------------------------------------------------------*/
Vector3 SphereCollider::Center::get()
{
return Convert::ToCLI(getNativeCollisionShape<SHSphere>().GetWorldCentroid());
}
float SphereCollider::Radius::get()
{
return getNativeCollisionShape<SHSphere>().GetWorldRadius();
}
void SphereCollider::Radius::set(float value)
{
getNativeCollisionShape<SHSphere>().SetWorldRadius(value);
}
/*---------------------------------------------------------------------------------*/
/* SphereCollider - Usage Functions */
/*---------------------------------------------------------------------------------*/
bool SphereCollider::TestPoint(Vector3 point)
{
//return getNativeCollisionShape<SHSphere>().TestPoint(Convert::ToNative(point));
return false;
}
bool SphereCollider::Raycast(Ray ray, float maxDistance)
{
//return getNativeCollisionShape<SHSphere>().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);
}
}
}

View File

@ -16,6 +16,7 @@ of DigiPen Institute of Technology is prohibited.
// Primary Include
#include "Component.hxx"
namespace SHADE
{
template<typename CollisionShapeType>
@ -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<CollisionShapeType&>(shape);

View File

@ -87,23 +87,6 @@ namespace SHADE
void set(float value);
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Checks if the specified point is within this shape's bounds.
/// </summary>
/// <param name="point">Point to test with.</param>
/// <returns>True if the point is in the shape's bounds.</returns>
virtual bool TestPoint(Vector3 point) = 0;
/// <summary>
/// Computes a Raycast and checks if there is a collision with any object.
/// </summary>
/// <param name="ray">The ray to cast.</param>
/// <param name="maxDistance">Maximum distance for the raycast check.</param>
/// <returns>True if the ray intersects with an object in the scene.</returns>
virtual bool Raycast(Ray ray, float maxDistance) = 0;
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */
@ -135,7 +118,39 @@ namespace SHADE
};
/// <summary>
/// Box-shaped Collider Bound.
/// A Sphere Collider
/// </summary>
public ref class SphereCollider : public CollisionShape
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Center of the sphere.
/// </summary>
property Vector3 Center
{
Vector3 get();
}
/// <summary>
/// Radius of the sphere/
/// </summary>
property float Radius
{
float get();
void set(float value);
}
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
SphereCollider(int arrayIndex, Entity attachedEntity);
};
/// <summary>
/// A Box Collider
/// </summary>
public ref class BoxCollider : public CollisionShape
{
@ -166,14 +181,6 @@ namespace SHADE
Quaternion get();
}
/*-----------------------------------------------------------------------------*/
/* ColliderBound Functions */
/*-----------------------------------------------------------------------------*/
/// <inheritdoc/>
bool TestPoint(Vector3 point) override;
/// <inheritdoc/>
bool Raycast(Ray ray, float maxDistance) override;
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
@ -182,45 +189,46 @@ namespace SHADE
};
/// <summary>
/// Sphere-shaped Collider Bound.
/// A Capsule Collider
/// </summary>
public ref class SphereCollider : public CollisionShape
public ref class CapsuleCollider : public CollisionShape
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Center of the sphere.
/// Center of the capsule.
/// </summary>
property Vector3 Center
{
Vector3 get();
}
/// <summary>
/// Radius of the Bounding Sphere formed by this bound.
/// Radius of the capsule.
/// </summary>
property float Radius
{
float get();
void set(float value);
}
/*-----------------------------------------------------------------------------*/
/* ColliderBound Functions */
/*-----------------------------------------------------------------------------*/
/// <inheritdoc/>
bool TestPoint(Vector3 point) override;
/// <inheritdoc/>
bool Raycast(Ray ray, float maxDistance) override;
/// <summary>
/// Height of the capsule.
/// </summary>
property float Height
{
float get();
void set(float value);
}
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
SphereCollider(int arrayIndex, Entity attachedEntity);
CapsuleCollider(int arrayIndex, Entity attachedEntity);
};
/// <summary>
/// CLR version of the the SHADE Engine's SHColliderComponent.
/// A single Collider component can contain one or multiple Collider Bounds.