Fixed fatal bug that caused a crash on changing scenes with the editor enabled #255

Merged
direnbharwani merged 3 commits from SP3-2-Physics into main 2022-11-23 02:45:51 +08:00
7 changed files with 39 additions and 21 deletions

View File

@ -178,14 +178,23 @@ namespace SHADE
/* Public Function Member Definitions */ /* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
bool SHBox::TestPoint(const SHVec3& point) noexcept bool SHBox::TestPoint(const SHVec3& point) const noexcept
{ {
return BoundingBox::Contains(point); return BoundingBox::Contains(point);
} }
bool SHBox::Raycast(const SHRay& ray, float& distance) noexcept SHRaycastResult SHBox::Raycast(const SHRay& ray) const noexcept
{ {
return BoundingBox::Intersects(ray.position, ray.direction, distance); SHRaycastResult result;
result.hit = Intersects(ray.position, ray.direction, result.distance);
if (result.hit)
{
result.position = ray.position + ray.direction * result.distance;
result.angle = SHVec3::Angle(ray.position, result.position);
}
return result;
} }
bool SHBox::Contains(const SHBox& rhs) const noexcept bool SHBox::Contains(const SHBox& rhs) const noexcept

View File

@ -76,8 +76,8 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override;
[[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; [[nodiscard]] SHRaycastResult Raycast(const SHRay& ray) const noexcept override;
[[nodiscard]] bool Contains (const SHBox& rhs) const noexcept; [[nodiscard]] bool Contains (const SHBox& rhs) const noexcept;
[[nodiscard]] float Volume () const noexcept; [[nodiscard]] float Volume () const noexcept;

View File

@ -69,8 +69,8 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; [[nodiscard]] virtual bool TestPoint (const SHVec3& point) const noexcept = 0;
[[nodiscard]] virtual bool Raycast (const SHRay& ray, float& distance) noexcept = 0; [[nodiscard]] virtual SHRaycastResult Raycast (const SHRay& ray) const noexcept = 0;
protected: protected:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -138,14 +138,23 @@ namespace SHADE
/* Public Function Member Definitions */ /* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
bool SHSphere::TestPoint(const SHVec3& point) noexcept bool SHSphere::TestPoint(const SHVec3& point) const noexcept
{ {
return BoundingSphere::Contains(point); return BoundingSphere::Contains(point);
} }
bool SHSphere::Raycast(const SHRay& ray, float& distance) noexcept SHRaycastResult SHSphere::Raycast(const SHRay& ray) const noexcept
{ {
return Intersects(ray.position, ray.direction, distance); SHRaycastResult result;
result.hit = Intersects(ray.position, ray.direction, result.distance);
if (result.hit)
{
result.position = ray.position + ray.direction * result.distance;
result.angle = SHVec3::Angle(ray.position, result.position);
}
return result;
} }
bool SHSphere::Contains(const SHSphere& rhs) const noexcept bool SHSphere::Contains(const SHSphere& rhs) const noexcept

View File

@ -64,12 +64,12 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override;
[[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; [[nodiscard]] SHRaycastResult Raycast(const SHRay& ray) const noexcept override;
[[nodiscard]] bool Contains (const SHSphere& rhs) const noexcept; [[nodiscard]] bool Contains (const SHSphere& rhs) const noexcept;
[[nodiscard]] float Volume () const noexcept; [[nodiscard]] float Volume () const noexcept;
[[nodiscard]] float SurfaceArea () const noexcept; [[nodiscard]] float SurfaceArea () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -265,11 +265,11 @@ namespace SHADE
return; return;
} }
const int NUM_SHAPES = static_cast<int>(componentGroup.colliderComponent->GetCollisionShapes().size()); //const int NUM_SHAPES = static_cast<int>(componentGroup.colliderComponent->GetCollisionShapes().size());
for (int i = 0; i < NUM_SHAPES; ++i) //for (int i = 0; i < NUM_SHAPES; ++i)
physicsObject->AddCollisionShape(i); // physicsObject->AddCollisionShape(i);
physicsObject->SyncColliders(*componentGroup.colliderComponent); //physicsObject->SyncColliders(*componentGroup.colliderComponent);
} }
void SHPhysicsObjectManager::removeRigidBody(const QueueCommand&, SHPhysicsObject* physicsObject, const PhysicsComponentGroup& componentGroup) void SHPhysicsObjectManager::removeRigidBody(const QueueCommand&, SHPhysicsObject* physicsObject, const PhysicsComponentGroup& componentGroup)

View File

@ -168,7 +168,7 @@ namespace SHADE
} }
bool BoxCollider::Raycast(Ray ray, float maxDistance) bool BoxCollider::Raycast(Ray ray, float maxDistance)
{ {
return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray), maxDistance); return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray));
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -200,7 +200,7 @@ namespace SHADE
} }
bool SphereCollider::Raycast(Ray ray, float maxDistance) bool SphereCollider::Raycast(Ray ray, float maxDistance)
{ {
return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray), maxDistance); return getNativeCollisionShape<SHBox>().Raycast(Convert::ToNative(ray));
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/