Fixed various Physics bugs #217

Merged
direnbharwani merged 14 commits from SP3-2-Physics into main 2022-11-17 17:13:02 +08:00
3 changed files with 179 additions and 38 deletions
Showing only changes of commit 1b2ff7f4a2 - Show all commits

View File

@ -20,7 +20,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* ColliderBound - Constructors */
/* CollisionShape - Constructors */
/*---------------------------------------------------------------------------------*/
CollisionShape::CollisionShape(int arrayIdx, Entity attachedEntity)
: 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)
{
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)
: CollisionShape { arrayIndex, attachedEntity }
{}
/*---------------------------------------------------------------------------------*/
/* BoxColliderBound - Properties */
/* BoxCollider - Properties */
/*---------------------------------------------------------------------------------*/
Vector3 BoxCollider::Center::get()
{
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetCenter());
return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetCenter());
}
void BoxCollider::Center::set(Vector3 value)
{
getNativeBoundObject<SHBox>().SetCenter(Convert::ToNative(value));
getNativeCollisionShape<SHBox>().SetCenter(Convert::ToNative(value));
}
Vector3 BoxCollider::HalfExtents::get()
{
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetWorldExtents());
return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetWorldExtents());
}
void BoxCollider::HalfExtents::set(Vector3 value)
{
getNativeBoundObject<SHBox>().SetWorldExtents(Convert::ToNative(value));
getNativeCollisionShape<SHBox>().SetWorldExtents(Convert::ToNative(value));
}
Vector3 BoxCollider::Min::get()
{
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetMin());
return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetMin());
}
void BoxCollider::Min::set(Vector3 value)
{
getNativeBoundObject<SHBox>().SetMin(Convert::ToNative(value));
getNativeCollisionShape<SHBox>().SetMin(Convert::ToNative(value));
}
Vector3 BoxCollider::Max::get()
{
return Convert::ToCLI(getNativeBoundObject<SHBox>().GetMax());
return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetMax());
}
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)
{
return getNativeBoundObject<SHBox>().TestPoint(Convert::ToNative(point));
return getNativeCollisionShape<SHBox>().TestPoint(Convert::ToNative(point));
}
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()
{
return Convert::ToCLI(getNativeBoundObject<SHSphere>().GetCenter());
return Convert::ToCLI(getNativeCollisionShape<SHSphere>().GetCenter());
}
void SphereCollider::Center::set(Vector3 value)
{
getNativeBoundObject<SHSphere>().SetCenter(Convert::ToNative(value));
getNativeCollisionShape<SHSphere>().SetCenter(Convert::ToNative(value));
}
float SphereCollider::Radius::get()
{
return getNativeBoundObject<SHSphere>().GetWorldRadius();
return getNativeCollisionShape<SHSphere>().GetWorldRadius();
}
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)
{
return getNativeBoundObject<SHBox>().TestPoint(Convert::ToNative(point));
return getNativeCollisionShape<SHBox>().TestPoint(Convert::ToNative(point));
}
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)
: CollisionShape{ arrayIndex, attachedEntity }
@ -154,7 +235,7 @@ namespace SHADE
}
/*---------------------------------------------------------------------------------*/
/* Collider - ColliderBound Functions */
/* Collider - Collider Functions */
/*---------------------------------------------------------------------------------*/
CollisionShape^ Collider::GetCollisionShape(int index)
{
@ -166,7 +247,7 @@ namespace SHADE
// Check if valid
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 subColliderList[index];
@ -217,7 +298,7 @@ namespace SHADE
{
collidersList->Remove(wr);
}
SAFE_NATIVE_CALL_END("Collider.OnColliderBoundChanged")
SAFE_NATIVE_CALL_END("Collider.OnCollisionShapeChanged")
}
void Collider::updateSubColliderList()

View File

@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
template<typename CollisionShapeType>
CollisionShapeType& SHADE::CollisionShape::getNativeBoundObject()
CollisionShapeType& SHADE::CollisionShape::getNativeCollisionShape()
{
SHColliderComponent* collider = SHComponentManager::GetComponent_s<SHColliderComponent>(entity);
if (!collider)
@ -29,13 +29,13 @@ namespace SHADE
{
auto& shape = collider->GetCollisionShape(arrayIndex);
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);
}
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:
/*-----------------------------------------------------------------------------*/
/* 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 */
/*-----------------------------------------------------------------------------*/
@ -56,20 +111,25 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
int arrayIndex; // Index into the colliders vector on the native object
Entity entity; // Entity holding the collider component that this collider bounds is on
int arrayIndex; // Index into the colliders vector on the native object
Entity entity; // Entity holding the collider component that this collider bounds is on
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
template<typename CollisionShapeType>
CollisionShapeType& getNativeBoundObject();
CollisionShapeType& getNativeCollisionShape();
internal:
/*-----------------------------------------------------------------------------*/
/* Setter Functions */
/*-----------------------------------------------------------------------------*/
void updateArrayIndex(int index);
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
SHCollisionShape& getNativeCollisionShape();
};
/// <summary>
@ -205,18 +265,18 @@ namespace SHADE
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves a ColliderBound at the specified index in the ColliderBound list.
/// Retrieves a CollisionShape at the specified index in the CollisionShapes list.
/// </summary>
/// <param name="index">Index to retrieve a ColliderBound from.</param>
/// <returns>ColliderBound for the specified index.</returns>
CollisionShape^ GetCollisionShape(int index);
/// <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.
/// </summary>
/// <typeparam name="T">Type of the ColliderBound to cast to.</typeparam>
/// <param name="index">Index to retrieve a ColliderBound from.</param>
/// <returns>ColliderBound for the specified index.</returns>
/// <typeparam name="T">Type of the CollisionShape to cast to.</typeparam>
/// <param name="index">Index to retrieve a CollisionShape from.</param>
/// <returns>CollisionShape for the specified index.</returns>
generic<typename T> where T:CollisionShape
T GetCollisionShape(int index);