Expanded Collision Shape C# Interface

This commit is contained in:
Diren D Bharwani 2022-11-15 23:57:38 +08:00
parent cd164cc2d5
commit 1b2ff7f4a2
3 changed files with 179 additions and 38 deletions

View File

@ -20,7 +20,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* ColliderBound - Constructors */ /* CollisionShape - Constructors */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
CollisionShape::CollisionShape(int arrayIdx, Entity attachedEntity) CollisionShape::CollisionShape(int arrayIdx, Entity attachedEntity)
: arrayIndex { arrayIdx } : arrayIndex { arrayIdx }
@ -28,102 +28,183 @@ namespace SHADE
{} {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* ColliderBound - Setter Functions */ /* CollisionShape - Properties */
/*---------------------------------------------------------------------------------*/
bool CollisionShape::IsTrigger::get()
{
return getNativeCollisionShape().IsTrigger();
}
void CollisionShape::IsTrigger::set(bool value)
{
getNativeCollisionShape().SetIsTrigger(value);
}
Vector3 CollisionShape::PositionOffset::get()
{
return Convert::ToCLI(getNativeCollisionShape().GetPositionOffset());
}
void CollisionShape::PositionOffset::set(Vector3 value)
{
getNativeCollisionShape().SetPositionOffset(Convert::ToNative(value));
}
Vector3 CollisionShape::RotationOffset::get()
{
return Convert::ToCLI(getNativeCollisionShape().GetRotationOffset());
}
void CollisionShape::RotationOffset::set(Vector3 value)
{
getNativeCollisionShape().SetRotationOffset(Convert::ToNative(value));
}
float CollisionShape::Friction::get()
{
return getNativeCollisionShape().GetFriction();
}
void CollisionShape::Friction::set(float value)
{
getNativeCollisionShape().SetFriction(value);
}
float CollisionShape::Bounciness::get()
{
return getNativeCollisionShape().GetBounciness();
}
void CollisionShape::Bounciness::set(float value)
{
getNativeCollisionShape().SetBounciness(value);
}
float CollisionShape::Density::get()
{
return getNativeCollisionShape().GetDensity();
}
void CollisionShape::Density::set(float value)
{
getNativeCollisionShape().SetDensity(value);
}
/*---------------------------------------------------------------------------------*/
/* CollisionShape - helper Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void CollisionShape::updateArrayIndex(int index) void CollisionShape::updateArrayIndex(int index)
{ {
arrayIndex = index; arrayIndex = index;
} }
SHCollisionShape& SHADE::CollisionShape::getNativeCollisionShape()
{
SHColliderComponent* collider = SHComponentManager::GetComponent_s<SHColliderComponent>(entity);
if (!collider)
throw gcnew System::InvalidOperationException("Unable to retrieve Collider component!");
try
{
auto& shape = collider->GetCollisionShape(arrayIndex);
return shape;
}
catch (std::invalid_argument&)
{
throw gcnew System::IndexOutOfRangeException("Attempted to retrieve out of range CollisionShape!");
}
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* BoxColliderBound - Constructors */ /* BoxCollider - Constructors */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
BoxCollider::BoxCollider(int arrayIdx, Entity attachedEntity) BoxCollider::BoxCollider(int arrayIdx, Entity attachedEntity)
: CollisionShape { arrayIndex, attachedEntity } : CollisionShape { arrayIndex, attachedEntity }
{} {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* BoxColliderBound - Properties */ /* BoxCollider - Properties */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Vector3 BoxCollider::Center::get() Vector3 BoxCollider::Center::get()
{ {
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetCenter()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetCenter());
} }
void BoxCollider::Center::set(Vector3 value) void BoxCollider::Center::set(Vector3 value)
{ {
getNativeBoundObject<SHBox>().SetCenter(Convert::ToNative(value)); getNativeCollisionShape<SHBox>().SetCenter(Convert::ToNative(value));
} }
Vector3 BoxCollider::HalfExtents::get() Vector3 BoxCollider::HalfExtents::get()
{ {
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetWorldExtents()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetWorldExtents());
} }
void BoxCollider::HalfExtents::set(Vector3 value) void BoxCollider::HalfExtents::set(Vector3 value)
{ {
getNativeBoundObject<SHBox>().SetWorldExtents(Convert::ToNative(value)); getNativeCollisionShape<SHBox>().SetWorldExtents(Convert::ToNative(value));
} }
Vector3 BoxCollider::Min::get() Vector3 BoxCollider::Min::get()
{ {
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetMin()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetMin());
} }
void BoxCollider::Min::set(Vector3 value) void BoxCollider::Min::set(Vector3 value)
{ {
getNativeBoundObject<SHBox>().SetMin(Convert::ToNative(value)); getNativeCollisionShape<SHBox>().SetMin(Convert::ToNative(value));
} }
Vector3 BoxCollider::Max::get() Vector3 BoxCollider::Max::get()
{ {
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetMax()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetMax());
} }
void BoxCollider::Max::set(Vector3 value) void BoxCollider::Max::set(Vector3 value)
{ {
getNativeBoundObject<SHBox>().SetMax(Convert::ToNative(value)); getNativeCollisionShape<SHBox>().SetMax(Convert::ToNative(value));
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* BoxColliderBound - Usage Functions */ /* BoxCollider - Usage Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
bool BoxCollider::TestPoint(Vector3 point) bool BoxCollider::TestPoint(Vector3 point)
{ {
return getNativeBoundObject<SHBox>().TestPoint(Convert::ToNative(point)); return getNativeCollisionShape<SHBox>().TestPoint(Convert::ToNative(point));
} }
bool BoxCollider::Raycast(Ray ray, float maxDistance) bool BoxCollider::Raycast(Ray ray, float maxDistance)
{ {
return getNativeBoundObject<SHBox>().Raycast(Convert::ToNative(ray), maxDistance); return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray), maxDistance);
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* BoxColliderBound - Properties */ /* SphereCollider - Properties */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Vector3 SphereCollider::Center::get() Vector3 SphereCollider::Center::get()
{ {
return Convert::ToCLI(getNativeBoundObject<SHSphere>().GetCenter()); return Convert::ToCLI(getNativeCollisionShape<SHSphere>().GetCenter());
} }
void SphereCollider::Center::set(Vector3 value) void SphereCollider::Center::set(Vector3 value)
{ {
getNativeBoundObject<SHSphere>().SetCenter(Convert::ToNative(value)); getNativeCollisionShape<SHSphere>().SetCenter(Convert::ToNative(value));
} }
float SphereCollider::Radius::get() float SphereCollider::Radius::get()
{ {
return getNativeBoundObject<SHSphere>().GetWorldRadius(); return getNativeCollisionShape<SHSphere>().GetWorldRadius();
} }
void SphereCollider::Radius::set(float value) void SphereCollider::Radius::set(float value)
{ {
getNativeBoundObject<SHSphere>().SetWorldRadius(value); getNativeCollisionShape<SHSphere>().SetWorldRadius(value);
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* SphereColliderBound - Usage Functions */ /* SphereCollider - Usage Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
bool SphereCollider::TestPoint(Vector3 point) bool SphereCollider::TestPoint(Vector3 point)
{ {
return getNativeBoundObject<SHBox>().TestPoint(Convert::ToNative(point)); return getNativeCollisionShape<SHBox>().TestPoint(Convert::ToNative(point));
} }
bool SphereCollider::Raycast(Ray ray, float maxDistance) bool SphereCollider::Raycast(Ray ray, float maxDistance)
{ {
return getNativeBoundObject<SHBox>().Raycast(Convert::ToNative(ray), maxDistance); return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray), maxDistance);
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* SphereColliderBound - Constructors */ /* SphereCollider - Constructors */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SphereCollider::SphereCollider(int arrayIndex, Entity attachedEntity) SphereCollider::SphereCollider(int arrayIndex, Entity attachedEntity)
: CollisionShape{ arrayIndex, attachedEntity } : CollisionShape{ arrayIndex, attachedEntity }
@ -154,7 +235,7 @@ namespace SHADE
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Collider - ColliderBound Functions */ /* Collider - Collider Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
CollisionShape^ Collider::GetCollisionShape(int index) CollisionShape^ Collider::GetCollisionShape(int index)
{ {
@ -166,7 +247,7 @@ namespace SHADE
// Check if valid // Check if valid
if (index < 0 || index >= subColliderList->Count) if (index < 0 || index >= subColliderList->Count)
throw gcnew System::ArgumentException("[Collider] Invalid index for Collider Bound retrieval."); throw gcnew System::ArgumentException("[Collider] Invalid index for Collision Shape retrieval.");
// Return the bound // Return the bound
return subColliderList[index]; return subColliderList[index];
@ -217,7 +298,7 @@ namespace SHADE
{ {
collidersList->Remove(wr); collidersList->Remove(wr);
} }
SAFE_NATIVE_CALL_END("Collider.OnColliderBoundChanged") SAFE_NATIVE_CALL_END("Collider.OnCollisionShapeChanged")
} }
void Collider::updateSubColliderList() void Collider::updateSubColliderList()

View File

@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
template<typename CollisionShapeType> template<typename CollisionShapeType>
CollisionShapeType& SHADE::CollisionShape::getNativeBoundObject() CollisionShapeType& SHADE::CollisionShape::getNativeCollisionShape()
{ {
SHColliderComponent* collider = SHComponentManager::GetComponent_s<SHColliderComponent>(entity); SHColliderComponent* collider = SHComponentManager::GetComponent_s<SHColliderComponent>(entity);
if (!collider) if (!collider)
@ -29,13 +29,13 @@ namespace SHADE
{ {
auto& shape = collider->GetCollisionShape(arrayIndex); auto& shape = collider->GetCollisionShape(arrayIndex);
if (shape.GetType() != SHCollisionShape::Type::BOX) if (shape.GetType() != SHCollisionShape::Type::BOX)
throw gcnew System::InvalidOperationException("Attempted to retrieve invalid ColliderBound."); throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape.");
return reinterpret_cast<CollisionShapeType&>(shape); return reinterpret_cast<CollisionShapeType&>(shape);
} }
catch (std::invalid_argument&) catch (std::invalid_argument&)
{ {
throw gcnew System::IndexOutOfRangeException("Attempted to retrieve out of range ColliderBound!"); throw gcnew System::IndexOutOfRangeException("Attempted to retrieve out of range CollisionShape!");
} }
} }
} }

View File

@ -30,6 +30,61 @@ namespace SHADE
public ref class CollisionShape abstract public ref class CollisionShape abstract
{ {
public: public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Whether or not this CollisionShape is a trigger.
/// </summary>
property bool IsTrigger
{
bool get();
void set(bool value);
}
/// <summary>
/// The positional offset of this collision shape from the transform's position.
/// </summary>
property Vector3 PositionOffset
{
Vector3 get();
void set(Vector3 value);
}
/// <summary>
/// The rotational offset of this collision shape from the transform's rotation.
/// </summary>
property Vector3 RotationOffset
{
Vector3 get();
void set(Vector3 value);
}
// TODO(Diren): Swap this to Physics Materials once asset implementation for that is done
/// <summary>
/// The frictional coefficient of the shape. Clamped between 0 and 1.
/// </summary>
property float Friction
{
float get();
void set(float value);
}
/// <summary>
/// The bounciness factor of the shape. Clamped between 0 and 1.
/// </summary>
property float Bounciness
{
float get();
void set(float value);
}
/// <summary>
/// The mass density of this shape. Cannot be negative.
/// </summary>
property float Density
{
float get();
void set(float value);
}
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Usage Functions */ /* Usage Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -56,20 +111,25 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
int arrayIndex; // Index into the colliders vector on the native object int arrayIndex; // Index into the colliders vector on the native object
Entity entity; // Entity holding the collider component that this collider bounds is on Entity entity; // Entity holding the collider component that this collider bounds is on
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
template<typename CollisionShapeType> template<typename CollisionShapeType>
CollisionShapeType& getNativeBoundObject(); CollisionShapeType& getNativeCollisionShape();
internal: internal:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Setter Functions */ /* Setter Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
void updateArrayIndex(int index); void updateArrayIndex(int index);
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
SHCollisionShape& getNativeCollisionShape();
}; };
/// <summary> /// <summary>
@ -205,18 +265,18 @@ namespace SHADE
/* Usage Functions */ /* Usage Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary> /// <summary>
/// Retrieves a ColliderBound at the specified index in the ColliderBound list. /// Retrieves a CollisionShape at the specified index in the CollisionShapes list.
/// </summary> /// </summary>
/// <param name="index">Index to retrieve a ColliderBound from.</param> /// <param name="index">Index to retrieve a ColliderBound from.</param>
/// <returns>ColliderBound for the specified index.</returns> /// <returns>ColliderBound for the specified index.</returns>
CollisionShape^ GetCollisionShape(int index); CollisionShape^ GetCollisionShape(int index);
/// <summary> /// <summary>
/// Retrieves a ColliderBound at the specified index in the ColliderBound list /// Retrieves a CollisionShape at the specified index in the CollisionShapes list
/// and casts it to the appropriate type. /// and casts it to the appropriate type.
/// </summary> /// </summary>
/// <typeparam name="T">Type of the ColliderBound to cast to.</typeparam> /// <typeparam name="T">Type of the CollisionShape to cast to.</typeparam>
/// <param name="index">Index to retrieve a ColliderBound from.</param> /// <param name="index">Index to retrieve a CollisionShape from.</param>
/// <returns>ColliderBound for the specified index.</returns> /// <returns>CollisionShape for the specified index.</returns>
generic<typename T> where T:CollisionShape generic<typename T> where T:CollisionShape
T GetCollisionShape(int index); T GetCollisionShape(int index);