Implemented a custom physics engine #316

Merged
direnbharwani merged 95 commits from SHPhysics into main 2023-01-23 15:55:45 +08:00
9 changed files with 73 additions and 33 deletions
Showing only changes of commit 89f1f60064 - Show all commits

View File

@ -4,7 +4,7 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 0, y: 3, z: 0} Translate: {x: 0.0700113177, y: 2.5, z: 0}
Rotate: {x: -0, y: 0, z: -0} Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
@ -19,14 +19,14 @@
Interpolate: true Interpolate: true
Sleeping Enabled: true Sleeping Enabled: true
Freeze Position X: false Freeze Position X: false
Freeze Position Y: true Freeze Position Y: false
Freeze Position Z: false Freeze Position Z: false
Freeze Rotation X: false Freeze Rotation X: false
Freeze Rotation Y: false Freeze Rotation Y: false
Freeze Rotation Z: false Freeze Rotation Z: false
IsActive: true IsActive: true
Collider Component: Collider Component:
DrawColliders: true DrawColliders: false
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Sphere Type: Sphere
@ -65,8 +65,8 @@
NumberOfChildren: 0 NumberOfChildren: 0
Components: Components:
Transform Component: Transform Component:
Translate: {x: 0, y: -2.5, z: 0} Translate: {x: 0, y: 0, z: 0}
Rotate: {x: 0, y: 0, z: 0} Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1} Scale: {x: 1, y: 1, z: 1}
IsActive: true IsActive: true
RigidBody Component: RigidBody Component:
@ -87,11 +87,11 @@
Freeze Rotation Z: false Freeze Rotation Z: false
IsActive: true IsActive: true
Collider Component: Collider Component:
DrawColliders: true DrawColliders: false
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Sphere Type: Sphere
Radius: 5 Radius: 2.5
Friction: 0.400000006 Friction: 0.400000006
Bounciness: 0 Bounciness: 0
Density: 1 Density: 1
@ -127,7 +127,7 @@
Freeze Rotation Z: false Freeze Rotation Z: false
IsActive: true IsActive: true
Collider Component: Collider Component:
DrawColliders: true DrawColliders: false
Colliders: Colliders:
- Is Trigger: false - Is Trigger: false
Type: Sphere Type: Sphere

View File

@ -185,12 +185,6 @@ namespace Sandbox
#endif #endif
SHSceneManager::SceneUpdate(0.016f); SHSceneManager::SceneUpdate(0.016f);
#ifdef SHEDITOR #ifdef SHEDITOR
static bool drawBP = false;
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::RIGHT_CTRL))
{
drawBP = !drawBP;
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE, drawBP);
}
SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, SHFrameRateController::GetRawDeltaTime()); SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, SHFrameRateController::GetRawDeltaTime());
editor->PollPicking(); editor->PollPicking();

View File

@ -25,6 +25,7 @@
#include "Serialization/SHSerialization.h" #include "Serialization/SHSerialization.h"
#include "Serialization/Configurations/SHConfigurationManager.h" #include "Serialization/Configurations/SHConfigurationManager.h"
#include "Editor/EditorWindow/SHEditorWindowManager.h" #include "Editor/EditorWindow/SHEditorWindowManager.h"
#include "Physics/System/SHPhysicsDebugDrawSystem.h"
const std::string LAYOUT_FOLDER_PATH{ std::string(ASSET_ROOT) + "/Editor/Layouts" }; const std::string LAYOUT_FOLDER_PATH{ std::string(ASSET_ROOT) + "/Editor/Layouts" };
@ -202,6 +203,26 @@ namespace SHADE
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Physics Settings"))
{
if (auto* physicsDebugDraw = SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>())
{
bool drawColliders = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::COLLIDERS);
if (ImGui::Checkbox("Draw Colliders", &drawColliders))
physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::COLLIDERS, drawColliders);
bool drawContactPoints = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACTS);
if (ImGui::Checkbox("Draw Contact Points", &drawContactPoints))
physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACTS, drawContactPoints);
bool drawBroadphase = physicsDebugDraw->GetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE);
if (ImGui::Checkbox("Draw Broadphase", &drawBroadphase))
physicsDebugDraw->SetFlagState(SHPhysicsDebugDrawSystem::DebugDrawFlags::BROADPHASE, drawBroadphase);
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar(); ImGui::EndMainMenuBar();
} }

View File

@ -35,6 +35,11 @@ namespace SHADE
/* Getter Functions Definitions */ /* Getter Functions Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
const SHContactManager::ContactPoints& SHPhysicsWorld::GetContactPoints() const noexcept
{
return contactManager.GetContactPoints();
}
const SHContactManager::TriggerEvents& SHPhysicsWorld::GetTriggerEvents() const noexcept const SHContactManager::TriggerEvents& SHPhysicsWorld::GetTriggerEvents() const noexcept
{ {
return contactManager.GetTriggerEvents(); return contactManager.GetTriggerEvents();

View File

@ -72,6 +72,7 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
const SHContactManager::ContactPoints& GetContactPoints () const noexcept;
const SHContactManager::TriggerEvents& GetTriggerEvents () const noexcept; const SHContactManager::TriggerEvents& GetTriggerEvents () const noexcept;
const SHContactManager::CollisionEvents& GetCollisionEvents () const noexcept; const SHContactManager::CollisionEvents& GetCollisionEvents () const noexcept;

View File

@ -38,7 +38,7 @@ namespace SHADE
{ {
auto* physicsDebugDrawSystem = reinterpret_cast<SHPhysicsDebugDrawSystem*>(GetSystem()); auto* physicsDebugDrawSystem = reinterpret_cast<SHPhysicsDebugDrawSystem*>(GetSystem());
if (!physicsDebugDrawSystem->GetFlagState(DebugDrawFlags::ACTIVE)) if (!physicsDebugDrawSystem->IsDebugDrawActive())
return; return;
auto* debugDrawSystem = SHSystemManager::GetSystem<SHDebugDrawSystem>(); auto* debugDrawSystem = SHSystemManager::GetSystem<SHDebugDrawSystem>();
@ -74,7 +74,14 @@ namespace SHADE
if (DRAW_CONTACTS) if (DRAW_CONTACTS)
{ {
// TODO const SHColour& CONTACT_COLOUR = physicsDebugDrawSystem->DEBUG_DRAW_COLOURS[SHUtilities::ConvertEnum(Colours::CONTACT)];
const auto& CONTACT_POINTS = physicsSystem->physicsWorld->GetContactPoints();
for (auto& contactPoint : CONTACT_POINTS)
{
const SHMatrix TRS = SHMatrix::Transform(contactPoint, SHQuaternion::Identity, SHVec3{ 0.1f });
debugDrawSystem->DrawCube(TRS, CONTACT_COLOUR);
}
} }
if (DRAW_RAYCASTS) if (DRAW_RAYCASTS)

View File

@ -44,6 +44,11 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
bool SHPhysicsDebugDrawSystem::IsDebugDrawActive() const noexcept
{
return flags & ACTIVE_FLAG;
}
bool SHPhysicsDebugDrawSystem::GetFlagState(DebugDrawFlags flag) const noexcept bool SHPhysicsDebugDrawSystem::GetFlagState(DebugDrawFlags flag) const noexcept
{ {
const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag); const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag);
@ -58,6 +63,10 @@ namespace SHADE
{ {
const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag); const uint8_t ENUM_VALUE = SHUtilities::ConvertEnum(flag);
state ? flags |= ENUM_VALUE : flags &= ~ENUM_VALUE; state ? flags |= ENUM_VALUE : flags &= ~ENUM_VALUE;
// If no other debug drawing state is active, turn active off. Otherwise, we maintain
// the active state.
flags == ACTIVE_FLAG ? flags = 0 : flags |= ACTIVE_FLAG;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -97,7 +106,7 @@ namespace SHADE
if (EVENT_DATA->debugDrawState) if (EVENT_DATA->debugDrawState)
{ {
if (collidersToDraw.empty()) if (collidersToDraw.empty())
SetFlagState(DebugDrawFlags::ACTIVE, true); flags |= ACTIVE_FLAG;
collidersToDraw.emplace(EVENT_DATA->entityID); collidersToDraw.emplace(EVENT_DATA->entityID);
} }
@ -105,8 +114,9 @@ namespace SHADE
{ {
collidersToDraw.erase(EVENT_DATA->entityID); collidersToDraw.erase(EVENT_DATA->entityID);
if (collidersToDraw.empty()) // if no colliders queued for drawing and no other debug drawing is enabled, disable debug drawing
SetFlagState(DebugDrawFlags::ACTIVE, false); if (collidersToDraw.empty() && flags == ACTIVE_FLAG)
flags = 0;
} }
return onColliderDrawEvent.get()->handle; return onColliderDrawEvent.get()->handle;

View File

@ -35,11 +35,10 @@ namespace SHADE
enum class DebugDrawFlags : uint8_t enum class DebugDrawFlags : uint8_t
{ {
ACTIVE = 0x0001 COLLIDERS = 0x02
, COLLIDERS = 0x0002 , CONTACTS = 0x04
, CONTACTS = 0x0004 , RAYCASTS = 0x08
, RAYCASTS = 0x0008 , BROADPHASE = 0x10
, BROADPHASE = 0x0010
}; };
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -53,7 +52,8 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] bool GetFlagState (DebugDrawFlags flag) const noexcept; [[nodiscard]] bool IsDebugDrawActive () const noexcept;
[[nodiscard]] bool GetFlagState (DebugDrawFlags flag) const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Setter Functions */ /* Setter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -64,10 +64,10 @@ namespace SHADE
/* Member Functions */ /* Member Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void Init () override; void Init () override;
void Exit () override; void Exit () override;
void AddRaycast (const SHRay& ray, const SHPhysicsRaycastResult& result) noexcept; void AddRaycast (const SHRay& ray, const SHPhysicsRaycastResult& result) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* System Routines */ /* System Routines */
@ -123,6 +123,8 @@ namespace SHADE
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
static constexpr uint8_t ACTIVE_FLAG = 0x01;
static const SHColour DEBUG_DRAW_COLOURS[static_cast<int>(Colours::COUNT)]; static const SHColour DEBUG_DRAW_COLOURS[static_cast<int>(Colours::COUNT)];
// 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive // 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive

View File

@ -52,8 +52,11 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] double GetFixedUpdateRate() const noexcept; [[nodiscard]] double GetFixedUpdateRate() const noexcept;
[[nodiscard]] double GetFixedDT () const noexcept; [[nodiscard]] double GetFixedDT () const noexcept;
[[nodiscard]] const std::vector<SHTriggerEvent>& GetTriggerInfo () const noexcept;
[[nodiscard]] const std::vector<SHCollisionEvent>& GetCollisionInfo () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Setter Functions */ /* Setter Functions */
@ -62,9 +65,6 @@ namespace SHADE
void SetFixedUpdateRate(double fixedUpdateRate) noexcept; void SetFixedUpdateRate(double fixedUpdateRate) noexcept;
void SetFixedDT(double fixedDt) noexcept; void SetFixedDT(double fixedDt) noexcept;
const std::vector<SHTriggerEvent>& GetTriggerInfo () const noexcept;
const std::vector<SHCollisionEvent>& GetCollisionInfo () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Member Functions */ /* Member Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/