From 65df50e722b8d2032196f670cc22c6e92f6e834c Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Tue, 7 Mar 2023 21:40:33 -0500 Subject: [PATCH] Added panel for box collider and created first rigidbody --- Editor/src/ComponentsPanel/BoxColliderPanel.h | 39 +++++++++++++++++++ Editor/src/ComponentsPanel/QuakeMapPanel.h | 2 +- Editor/src/Windows/EditorInterface.cpp | 5 +++ Editor/src/Windows/EditorSelectionPanel.cpp | 2 + Editor/src/Windows/EditorSelectionPanel.h | 8 ++-- Nuake/JoltImplementation.h | 10 +++++ Nuake/src/Core/Physics/DynamicWorld.cpp | 28 ++++++------- Nuake/src/Core/Physics/DynamicWorld.h | 5 ++- Nuake/src/Core/Physics/KinematicBody.h | 2 - Nuake/src/Core/Physics/Rigibody.h | 11 +++--- Nuake/src/Core/Physics/Rigidbody.cpp | 7 +++- Nuake/src/Scene/EditorCamera.cpp | 7 +++- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 4 -- 13 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 Editor/src/ComponentsPanel/BoxColliderPanel.h create mode 100644 Nuake/JoltImplementation.h diff --git a/Editor/src/ComponentsPanel/BoxColliderPanel.h b/Editor/src/ComponentsPanel/BoxColliderPanel.h new file mode 100644 index 00000000..0aeaf3da --- /dev/null +++ b/Editor/src/ComponentsPanel/BoxColliderPanel.h @@ -0,0 +1,39 @@ +#pragma once +#include "ComponentPanel.h" +#include +#include +#include + +class BoxColliderPanel : ComponentPanel { +public: + BoxColliderPanel() {} + + void Draw(Nuake::Entity entity) override + { + if (!entity.HasComponent()) + return; + + auto& component = entity.GetComponent(); + BeginComponentTable(BOX COLLIDER, Nuake::BoxColliderComponent); + { + { + ImGui::Text("Size"); + ImGui::TableNextColumn(); + + ImGuiHelper::DrawVec3("BoxSize", &component.Size); + ImGui::TableNextColumn(); + ComponentTableReset(component.Size, glm::vec3(1, 1, 1)); + } + ImGui::TableNextColumn(); + { + ImGui::Text("Is Trigger"); + ImGui::TableNextColumn(); + + ImGui::Checkbox("##isTrigger", &component.IsTrigger); + ImGui::TableNextColumn(); + ComponentTableReset(component.IsTrigger, false); + } + } + EndComponentTable(); + } +}; \ No newline at end of file diff --git a/Editor/src/ComponentsPanel/QuakeMapPanel.h b/Editor/src/ComponentsPanel/QuakeMapPanel.h index bf9c235d..6c805def 100644 --- a/Editor/src/ComponentsPanel/QuakeMapPanel.h +++ b/Editor/src/ComponentsPanel/QuakeMapPanel.h @@ -45,7 +45,7 @@ public: ImGui::Text("Collision"); ImGui::TableNextColumn(); - ImGui::Checkbox("#Collison", &component.HasCollisions); + ImGui::Checkbox("##Collison", &component.HasCollisions); ImGui::TableNextColumn(); ComponentTableReset(component.HasCollisions, true); } diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index a453a708..cd827f3b 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1399,6 +1399,11 @@ namespace Nuake { void EditorInterface::Update(float ts) { + if (!Engine::GetCurrentScene()) + { + return; + } + auto& editorCam = Engine::GetCurrentScene()->m_EditorCamera; editorCam->Update(ts, m_IsHoveringViewport); diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index a41a40b6..3c77548c 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -15,6 +15,7 @@ EditorSelectionPanel::EditorSelectionPanel() mQuakeMapPanel = QuakeMapPanel(); mCameraPanel = CameraPanel(); mRigidbodyPanel = RigidbodyPanel(); + mBoxColliderPanel = BoxColliderPanel(); } void EditorSelectionPanel::ResolveFile(Ref file) @@ -98,6 +99,7 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) mQuakeMapPanel.Draw(entity); mCameraPanel.Draw(entity); mRigidbodyPanel.Draw(entity); + mBoxColliderPanel.Draw(entity); /* if (Selection.Entity.HasComponent()) diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index b52a8d66..4f525755 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -1,7 +1,9 @@ #pragma once #include "../Actions/EditorSelection.h" #include "src/Scene/Entities/Entity.h" + #include "src/Core/FileSystem.h" +#include #include "../ComponentsPanel/TransformPanel.h" #include "../ComponentsPanel/LightPanel.h" @@ -10,9 +12,7 @@ #include "../ComponentsPanel/QuakeMapPanel.h" #include "../ComponentsPanel/CameraPanel.h" #include "../ComponentsPanel/RigidbodyPanel.h" - -#include "../Actions/EditorSelection.h" -#include +#include "../ComponentsPanel/BoxColliderPanel.h" class EditorSelectionPanel { private: @@ -23,9 +23,11 @@ private: QuakeMapPanel mQuakeMapPanel; CameraPanel mCameraPanel; RigidbodyPanel mRigidbodyPanel; + BoxColliderPanel mBoxColliderPanel; Ref currentFile; Ref selectedResource; + public: EditorSelectionPanel(); diff --git a/Nuake/JoltImplementation.h b/Nuake/JoltImplementation.h new file mode 100644 index 00000000..68edb7a6 --- /dev/null +++ b/Nuake/JoltImplementation.h @@ -0,0 +1,10 @@ +#pragma once + + +namespace Nuake +{ + class JoltImplementation + { + + }; +} \ No newline at end of file diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 09eeddd6..2b28f487 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -185,6 +185,7 @@ namespace Nuake } }; JPH::BodyID sphere_id; + BPLayerInterfaceImpl JoltBroadphaseLayerInterface = BPLayerInterfaceImpl(); namespace Physics { DynamicWorld::DynamicWorld() : _stepCount(0) @@ -194,10 +195,10 @@ namespace Nuake const uint32_t MaxBodyPairs = 1024; const uint32_t MaxContactConstraints = 1024; - BPLayerInterfaceImpl broad_phase_layer_interface; + _JoltPhysicsSystem = CreateRef(); - _JoltPhysicsSystem->Init(MaxBodies, NumBodyMutexes, MaxBodyPairs, MaxContactConstraints, broad_phase_layer_interface, MyBroadPhaseCanCollide, MyObjectCanCollide); + _JoltPhysicsSystem->Init(MaxBodies, NumBodyMutexes, MaxBodyPairs, MaxContactConstraints, JoltBroadphaseLayerInterface, MyBroadPhaseCanCollide, MyObjectCanCollide); // A body activation listener gets notified when bodies activate and go to sleep // Note that this is called from a job so whatever you do here needs to be thread safe. @@ -213,7 +214,7 @@ namespace Nuake // The main way to interact with the bodies in the physics system is through the body interface. There is a locking and a non-locking // variant of this. We're going to use the locking version (even though we're not planning to access bodies from multiple threads) - JPH::BodyInterface& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); + _JoltBodyInterface = &_JoltPhysicsSystem->GetBodyInterface(); // Next we can create a rigid body to serve as the floor, we make a large box // Create the settings for the collision volume (the shape). @@ -228,16 +229,16 @@ namespace Nuake JPH::BodyCreationSettings floor_settings(floor_shape, JPH::Vec3(0.0f, -1.0f, 0.0f), JPH::Quat::sIdentity(), JPH::EMotionType::Static, Layers::NON_MOVING); // Create the actual rigid body - JPH::Body* floor = bodyInterface.CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr + JPH::Body* floor = _JoltBodyInterface->CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr - bodyInterface.AddBody(floor->GetID(), JPH::EActivation::DontActivate); + _JoltBodyInterface->AddBody(floor->GetID(), JPH::EActivation::DontActivate); JPH::BodyCreationSettings sphere_settings(new JPH::SphereShape(0.5f), JPH::Vec3(0.0, 2.0, 0.0), JPH::Quat::sIdentity(), JPH::EMotionType::Dynamic, Layers::MOVING); - sphere_id = bodyInterface.CreateAndAddBody(sphere_settings, JPH::EActivation::Activate); + sphere_id = _JoltBodyInterface->CreateAndAddBody(sphere_settings, JPH::EActivation::Activate); // Now you can interact with the dynamic body, in this case we're going to give it a velocity. // (note that if we had used CreateBody then we could have set the velocity straight on the body before adding it to the physics system) - bodyInterface.SetLinearVelocity(sphere_id, JPH::Vec3(0.0f, -5.0f, 0.0f)); + _JoltBodyInterface->SetLinearVelocity(sphere_id, JPH::Vec3(0.0f, -5.0f, 0.0f)); // We simulate the physics world in discrete time steps. 60 Hz is a good rate to update the physics system. const float cDeltaTime = 1.0f / 60.0f; @@ -245,7 +246,7 @@ namespace Nuake // Optional step: Before starting the physics simulation you can optimize the broad phase. This improves collision detection performance (it's pointless here because we only have 2 bodies). // You should definitely not call this every frame or when e.g. streaming in a new level section as it is an expensive operation. // Instead insert all new objects in batches instead of 1 at a time to keep the broad phase efficient. - _JoltPhysicsSystem->OptimizeBroadPhase(); + //_JoltPhysicsSystem->OptimizeBroadPhase(); _JoltJobSystem = new JPH::JobSystemThreadPool(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsBarriers, std::thread::hardware_concurrency() - 1); } @@ -271,16 +272,15 @@ namespace Nuake JPH::BoxShapeSettings boxShapeSettings(JPH::Vec3(boxSize.x, boxSize.y, boxSize.z)); // Create the shape - JPH::ShapeSettings::ShapeResult floor_shape_result = boxShapeSettings.Create(); - JPH::ShapeRefC floor_shape = floor_shape_result.Get(); // We don't expect an error here, but you can check floor_shape_result for HasError() / GetError() + JPH::ShapeSettings::ShapeResult boxShapeResult = boxShapeSettings.Create(); + JPH::ShapeRefC boxShape = boxShapeResult.Get(); // We don't expect an error here, but you can check floor_shape_result for HasError() / GetError() // Create the settings for the body itself. Note that here you can also set other properties like the restitution / friction. - JPH::BodyCreationSettings boxSettings(floor_shape, JPH::Vec3(0.0f, -1.0f, 0.0f), JPH::Quat::sIdentity(), JPH::EMotionType::Static, Layers::NON_MOVING); + const auto& startPos = rb->GetPosition(); + JPH::BodyCreationSettings bodySettings(boxShape, JPH::Vec3(startPos.x, startPos.y, startPos.z), JPH::Quat::sIdentity(), JPH::EMotionType::Static, Layers::NON_MOVING); // Create the actual rigid body - JPH::Body* floor = bodyInterface.CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr - - bodyInterface.AddBody(floor->GetID(), JPH::EActivation::DontActivate); + JPH::BodyID floor = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::DontActivate); // Note that if we run out of bodies this can return nullptr } break; case RigidbodyShapes::SPHERE: diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index 3ed02b22..4370ac63 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -15,10 +15,12 @@ namespace JPH class JobSystemThreadPool; class ContactListener; class BodyActivationListener; + class BodyInterface; } namespace Nuake { + class BPLayerInterfaceImpl; class MyContactListener; class MyBodyActivationListener; @@ -30,7 +32,8 @@ namespace Nuake JPH::JobSystemThreadPool* _JoltJobSystem; Scope _contactListener; Scope _bodyActivationListener; - + JPH::BodyInterface* _JoltBodyInterface; + BPLayerInterfaceImpl* _JoltBroadphaseLayerInterface; public: DynamicWorld(); diff --git a/Nuake/src/Core/Physics/KinematicBody.h b/Nuake/src/Core/Physics/KinematicBody.h index d57c6a23..d09efa19 100644 --- a/Nuake/src/Core/Physics/KinematicBody.h +++ b/Nuake/src/Core/Physics/KinematicBody.h @@ -9,8 +9,6 @@ public: KinematicBody(); ~KinematicBody(); - - void MoveAndCollide(); void MoveAndSlide(); }; \ No newline at end of file diff --git a/Nuake/src/Core/Physics/Rigibody.h b/Nuake/src/Core/Physics/Rigibody.h index a4f8c093..6b7906a5 100644 --- a/Nuake/src/Core/Physics/Rigibody.h +++ b/Nuake/src/Core/Physics/Rigibody.h @@ -9,9 +9,10 @@ namespace Nuake namespace Physics { class RigidBody { private: - Ref m_CollisionShape; + Ref _collisionShape; + Vector3 _position; public: - float m_Mass; + float _mass; RigidBody(); RigidBody(glm::vec3 position, Entity handle); @@ -20,10 +21,10 @@ namespace Nuake void UpdateTransform(); void SetEntityID(Entity ent); - - bool HasShape() { return m_CollisionShape != nullptr; } + Vector3 GetPosition() const { return _position; } + bool HasShape() { return _collisionShape != nullptr; } void SetShape(Ref shape); - Ref GetShape() const { return m_CollisionShape; } + Ref GetShape() const { return _collisionShape; } }; } } diff --git a/Nuake/src/Core/Physics/Rigidbody.cpp b/Nuake/src/Core/Physics/Rigidbody.cpp index d2da5b69..3415a674 100644 --- a/Nuake/src/Core/Physics/Rigidbody.cpp +++ b/Nuake/src/Core/Physics/Rigidbody.cpp @@ -13,12 +13,15 @@ namespace Nuake } - RigidBody::RigidBody(glm::vec3 position, Entity handle) + RigidBody::RigidBody(glm::vec3 position, Entity handle) : _position(position) { } - RigidBody::RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel) + RigidBody::RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel) : + _position(position), + _collisionShape(shape), + _mass(mass) { } diff --git a/Nuake/src/Scene/EditorCamera.cpp b/Nuake/src/Scene/EditorCamera.cpp index dec23bc3..d9feaee5 100644 --- a/Nuake/src/Scene/EditorCamera.cpp +++ b/Nuake/src/Scene/EditorCamera.cpp @@ -33,6 +33,7 @@ namespace Nuake if (!previous && controlled) { + firstMouse = true; mouseLastX = x; mouseLastY = y; } @@ -42,6 +43,7 @@ namespace Nuake if (controlled && !isPressingMouse) { controlled = false; + firstMouse = true; } } @@ -145,6 +147,7 @@ namespace Nuake if (Pitch > 89.0f) Pitch = 89.0f; + if (Pitch < -89.0f) Pitch = -89.0f; @@ -172,7 +175,9 @@ namespace Nuake Translation += Vector3(Direction) * Input::YScroll; Input::YScroll = 0.0f; } - + else + { + } mouseLastX = x; mouseLastY = y; } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index d6efaab7..9095750e 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -35,10 +35,6 @@ namespace Nuake Ref btRigidbody = CreateRef(mass, transform.GetGlobalPosition(), boxShape); - rigidbody.m_Rigidbody = btRigidbody; - - //btRigidbody->SetKinematic(rigidbody.IsKinematic); - PhysicsManager::Get()->RegisterBody(btRigidbody); } }