Merge branch 'SP3-2-Physics' into PlayerControllerWIthNewPhysics

This commit is contained in:
Glence 2023-03-04 21:28:19 +08:00
commit 799cac0924
11 changed files with 81 additions and 33 deletions

View File

@ -150,8 +150,8 @@
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Auto Mass: false
Mass: 1 Mass: 1
Auto Mass: false
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.00999999978 Angular Drag: 0.00999999978
Use Gravity: true Use Gravity: true
@ -190,8 +190,8 @@
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Auto Mass: false
Mass: 1 Mass: 1
Auto Mass: false
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.00999999978 Angular Drag: 0.00999999978
Use Gravity: true Use Gravity: true
@ -230,8 +230,8 @@
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Auto Mass: false
Mass: 1 Mass: 1
Auto Mass: false
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.00999999978 Angular Drag: 0.00999999978
Use Gravity: true Use Gravity: true
@ -270,8 +270,8 @@
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
Type: Dynamic Type: Dynamic
Auto Mass: false
Mass: 1 Mass: 1
Auto Mass: false
Drag: 0.00999999978 Drag: 0.00999999978
Angular Drag: 0.00999999978 Angular Drag: 0.00999999978
Use Gravity: true Use Gravity: true
@ -290,7 +290,7 @@
- Is Trigger: false - Is Trigger: false
Collision Tag: 0 Collision Tag: 0
Type: Box Type: Box
Half Extents: {x: 1, y: 1, z: 1} Half Extents: {x: 1, y: 2, z: 1}
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1

View File

@ -105,6 +105,12 @@ namespace SHADE
template <IsFloatingPoint T = float> template <IsFloatingPoint T = float>
[[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON); [[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON);
template <IsArithmetic T>
[[nodiscard]] static bool IsInfinity (T value);
template <IsArithmetic T>
[[nodiscard]] static bool IsNaN (T value);
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Static Data Members */ /* Static Data Members */

View File

@ -126,4 +126,21 @@ namespace SHADE
return std::fabs(lhs - rhs) <= Max(absTolerance, RTOL); return std::fabs(lhs - rhs) <= Max(absTolerance, RTOL);
} }
template <IsArithmetic T>
bool SHMath::IsInfinity(T value)
{
const float MAX_VALUE = std::numeric_limits<T>::max();
const float MIN_VALUE = std::numeric_limits<T>::min();
return !(MIN_VALUE <= value && value <= MAX_VALUE);
}
template <IsArithmetic T>
bool SHMath::IsNaN(T value)
{
return value != value;
}
} // namespace SHADE } // namespace SHADE

View File

@ -67,8 +67,8 @@ namespace SHADE
bool SHCollision::ConvexVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept bool SHCollision::ConvexVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept
{ {
static constexpr float ABSOLUTE_TOLERANCE = 0.0005f; static constexpr float ABSOLUTE_TOLERANCE = 0.01f;
static constexpr float RELATIVE_TOLERANCE = 1.002f; static constexpr float RELATIVE_TOLERANCE = 0.95f;
const SHConvexPolyhedron& POLY_A = reinterpret_cast<const SHConvexPolyhedron&>(A); const SHConvexPolyhedron& POLY_A = reinterpret_cast<const SHConvexPolyhedron&>(A);
const SHConvexPolyhedron& POLY_B = reinterpret_cast<const SHConvexPolyhedron&>(B); const SHConvexPolyhedron& POLY_B = reinterpret_cast<const SHConvexPolyhedron&>(B);
@ -130,7 +130,7 @@ namespace SHADE
const SHConvexPolyhedron* incidentPoly = nullptr; const SHConvexPolyhedron* incidentPoly = nullptr;
SHCollisionUtils::FaceQuery minFaceQuery; SHCollisionUtils::FaceQuery minFaceQuery;
if (FACE_QUERY_A.bestDistance > FACE_QUERY_B.bestDistance * RELATIVE_TOLERANCE + ABSOLUTE_TOLERANCE) if (FACE_QUERY_A.bestDistance + ABSOLUTE_TOLERANCE > FACE_QUERY_B.bestDistance * RELATIVE_TOLERANCE)
{ {
minFaceQuery = FACE_QUERY_A; minFaceQuery = FACE_QUERY_A;
referencePoly = &POLY_A; referencePoly = &POLY_A;
@ -157,7 +157,7 @@ namespace SHADE
// If an edge pair contains the closest distance,vwe ignore any face queries and find the closest points on // If an edge pair contains the closest distance,vwe ignore any face queries and find the closest points on
// each edge and use that as the contact point. // each edge and use that as the contact point.
if (EDGE_QUERY.bestDistance * RELATIVE_TOLERANCE + ABSOLUTE_TOLERANCE > minFaceQuery.bestDistance + ABSOLUTE_TOLERANCE) if (EDGE_QUERY.bestDistance * RELATIVE_TOLERANCE > minFaceQuery.bestDistance + ABSOLUTE_TOLERANCE)
{ {
const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_A = POLY_A.GetHalfEdge(EDGE_QUERY.halfEdgeA); const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_A = POLY_A.GetHalfEdge(EDGE_QUERY.halfEdgeA);
const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_B = POLY_B.GetHalfEdge(EDGE_QUERY.halfEdgeB); const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_B = POLY_B.GetHalfEdge(EDGE_QUERY.halfEdgeB);

View File

@ -326,6 +326,17 @@ namespace SHADE
shape->Update(); shape->Update();
} }
void SHCollider::UpdateBroadphase() noexcept
{
flags |= MOVED_FLAG;
}
void SHCollider::UpdateBody() noexcept
{
if (rigidBody)
rigidBody->ComputeMassData();
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Private Member Function Definitions */ /* Private Member Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -149,6 +149,18 @@ namespace SHADE
*/ */
void Update () noexcept; void Update () noexcept;
/**
* @brief
* Updates the broadphase proxy of this collider.
*/
void UpdateBroadphase() noexcept;
/**
* @brief
* Recomputes the mass of the body if one exists.
*/
void UpdateBody() noexcept;
protected: protected:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */

View File

@ -207,9 +207,11 @@ namespace SHADE
// Recompute Relative radius // Recompute Relative radius
relativeExtents = 2.0f * Extents / scale; relativeExtents = 2.0f * Extents / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept
@ -219,9 +221,11 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Extents = relativeExtents * scale * 0.5f; Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
void SHBox::SetScale(const SHVec3& newScale) noexcept void SHBox::SetScale(const SHVec3& newScale) noexcept
@ -231,9 +235,11 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Extents = relativeExtents * scale * 0.5f; Extents = relativeExtents * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -168,9 +168,11 @@ namespace SHADE
// Recompute Relative radius // Recompute Relative radius
relativeRadius = 2.0f * Radius / scale; relativeRadius = 2.0f * Radius / scale;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept
@ -180,9 +182,11 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Radius = relativeRadius * scale * 0.5f; Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
void SHSphere::SetScale(float maxScale) noexcept void SHSphere::SetScale(float maxScale) noexcept
@ -192,9 +196,11 @@ namespace SHADE
// Recompute world radius // Recompute world radius
Radius = relativeRadius * scale * 0.5f; Radius = relativeRadius * scale * 0.5f;
// Hack: Indicate that the collider needs to update the broadphase proxy
if (collider) if (collider)
collider->SetScale(collider->GetScale()); {
collider->UpdateBroadphase();
collider->UpdateBody();
}
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -18,14 +18,6 @@
#include "Physics/SHPhysicsConstants.h" #include "Physics/SHPhysicsConstants.h"
#include "Physics/Collision/Narrowphase/SHCollisionUtils.h" #include "Physics/Collision/Narrowphase/SHCollisionUtils.h"
bool IsInfinity(float value)
{
const float MAX_VAL = std::numeric_limits<float>::max();
const float MIN_VAL = -MAX_VAL;
return !(MIN_VAL <= value && value <= MAX_VAL);
}
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -227,7 +219,7 @@ namespace SHADE
errorBias = -SHPHYSICS_BAUMGARTE * INV_DT * std::max(0.0f, contact.penetration - SHPHYSICS_LINEAR_SLOP); errorBias = -SHPHYSICS_BAUMGARTE * INV_DT * std::max(0.0f, contact.penetration - SHPHYSICS_LINEAR_SLOP);
// TODO: This should never occur. Move this check into the ConvexVSConvex to find the error. // TODO: This should never occur. Move this check into the ConvexVSConvex to find the error.
if (IsInfinity(errorBias)) if (SHMath::IsInfinity(errorBias))
errorBias = 0.0f; errorBias = 0.0f;
// Warm starting // Warm starting

View File

@ -40,8 +40,6 @@ namespace SHADE
flags |= 1U << 0; // Body is active flags |= 1U << 0; // Body is active
flags |= 1U << 2; // Sleeping is enabled flags |= 1U << 2; // Sleeping is enabled
flags |= 1U << 3; // Gravity is enabled flags |= 1U << 3; // Gravity is enabled
// TODO: Compute inertia if body is dynamic
} }
SHRigidBody::SHRigidBody(const SHRigidBody& rhs) noexcept SHRigidBody::SHRigidBody(const SHRigidBody& rhs) noexcept

View File

@ -360,8 +360,8 @@ RTTR_REGISTRATION
registration::class_<SHRigidBodyComponent>("RigidBody Component") registration::class_<SHRigidBodyComponent>("RigidBody Component")
.property("Type" , &SHRigidBodyComponent::GetType , &SHRigidBodyComponent::SetType ) .property("Type" , &SHRigidBodyComponent::GetType , &SHRigidBodyComponent::SetType )
.property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass )
.property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass ) .property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass )
.property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass )
.property("Drag" , &SHRigidBodyComponent::GetDrag , &SHRigidBodyComponent::SetDrag ) .property("Drag" , &SHRigidBodyComponent::GetDrag , &SHRigidBodyComponent::SetDrag )
.property("Angular Drag" , &SHRigidBodyComponent::GetAngularDrag , &SHRigidBodyComponent::SetAngularDrag ) .property("Angular Drag" , &SHRigidBodyComponent::GetAngularDrag , &SHRigidBodyComponent::SetAngularDrag )
.property("Use Gravity" , &SHRigidBodyComponent::IsGravityEnabled , &SHRigidBodyComponent::SetIsGravityEnabled ) .property("Use Gravity" , &SHRigidBodyComponent::IsGravityEnabled , &SHRigidBodyComponent::SetIsGravityEnabled )