From 0548f7762ce66d5ca86795536cdc96a4c13462d5 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 01:58:23 -0400 Subject: [PATCH 01/10] Renamed Wren API Method name --- Editor/resources/Scripts/Scene.wren | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren index 1464c3c3..a5f0eb65 100644 --- a/Editor/resources/Scripts/Scene.wren +++ b/Editor/resources/Scripts/Scene.wren @@ -4,7 +4,7 @@ import "Nuake:Math" for Vector3 class Scene { foreign static GetEntityID(name) - foreign static AddEntity(name) + foreign static CreateEntity(name) foreign static AddPrefab(prefabPath) static GetEntity(name) { From 4150610b95c200de1bf65e7ad83ab4c3d638f720 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 01:58:44 -0400 Subject: [PATCH 02/10] Silenced jolt callbacks debug prints + comments --- Nuake/src/Core/Physics/DynamicWorld.cpp | 4 ++-- Nuake/src/Resource/Prefab.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 1d674c8a..d35b670f 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -187,12 +187,12 @@ namespace Nuake public: virtual void OnBodyActivated(const JPH::BodyID& inBodyID, JPH::uint64 inBodyUserData) override { - std::cout << "A body got activated" << std::endl; + //std::cout << "A body got activated" << std::endl; } virtual void OnBodyDeactivated(const JPH::BodyID& inBodyID, JPH::uint64 inBodyUserData) override { - std::cout << "A body went to sleep" << std::endl; + //std::cout << "A body went to sleep" << std::endl; } }; diff --git a/Nuake/src/Resource/Prefab.h b/Nuake/src/Resource/Prefab.h index 593e3282..1d476a3b 100644 --- a/Nuake/src/Resource/Prefab.h +++ b/Nuake/src/Resource/Prefab.h @@ -76,6 +76,7 @@ namespace Nuake { this->AddEntity(entity); } + // Set reference to the parent entity to children for (auto& e : Entities) { auto parentC = e.GetComponent(); From e2790244ebe968340310f4f56d9870f4e8c7e78f Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 01:58:59 -0400 Subject: [PATCH 03/10] Fixed bug with name incrementation not working --- Nuake/src/Scene/Scene.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 575d8781..3605fe01 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -206,7 +206,7 @@ namespace Nuake { } std::string entityName; - if (GetEntity(name) != Entity()) + if (GetEntity(name) == Entity()) { entityName = name; } @@ -217,7 +217,7 @@ namespace Nuake { { const std::string& entityEnumName = name + std::to_string(i); const auto& entityId = GetEntity(entityEnumName).GetHandle(); - if (entityId != -1) + if (entityId == -1) { entityName = entityEnumName; break; From 11e3024a982ec67a0285881d22b705ab1bbc93df Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 02:44:38 -0400 Subject: [PATCH 04/10] Make sure prefabs gets unique entity IDs --- Nuake/src/Resource/Prefab.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Nuake/src/Resource/Prefab.h b/Nuake/src/Resource/Prefab.h index 1d476a3b..6e0241f2 100644 --- a/Nuake/src/Resource/Prefab.h +++ b/Nuake/src/Resource/Prefab.h @@ -71,8 +71,13 @@ namespace Nuake { { for (json e : j["Entities"]) { - Entity entity = Entity { Engine::GetCurrentScene()->m_Registry.create(), Engine::GetCurrentScene().get() }; + Entity entity = Engine::GetCurrentScene()->CreateEntity("-"); + auto& nameComponent = entity.GetComponent(); + + int entityId = nameComponent.ID; entity.Deserialize(e.dump()); + nameComponent.ID = entityId; + this->AddEntity(entity); } From 9b1de4a13f123b2fcb6c1ac093a646d60410c9a9 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 02:45:04 -0400 Subject: [PATCH 05/10] AddComponent<> now replaces instead of crashing if it already exists --- Nuake/src/Scene/Entities/Entity.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nuake/src/Scene/Entities/Entity.h b/Nuake/src/Scene/Entities/Entity.h index bbf073d3..2f197050 100644 --- a/Nuake/src/Scene/Entities/Entity.h +++ b/Nuake/src/Scene/Entities/Entity.h @@ -31,7 +31,7 @@ namespace Nuake template T& AddComponent() { - T& component = m_Scene->m_Registry.emplace(m_EntityHandle); + T& component = m_Scene->m_Registry.emplace_or_replace (m_EntityHandle); return component; } From 830c5fddf4c340ec588af60726e8b5184430f4c1 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 02:46:19 -0400 Subject: [PATCH 06/10] Can now add rigidbodies at runtime --- Nuake/src/Scene/Components/RigidbodyComponent.cpp | 6 +++--- Nuake/src/Scene/Components/RigidbodyComponent.h | 2 +- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 10 ++++++++++ Nuake/src/Scene/Systems/PhysicsSystem.h | 1 - 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Nuake/src/Scene/Components/RigidbodyComponent.cpp b/Nuake/src/Scene/Components/RigidbodyComponent.cpp index 4dd4c3e7..39862b53 100644 --- a/Nuake/src/Scene/Components/RigidbodyComponent.cpp +++ b/Nuake/src/Scene/Components/RigidbodyComponent.cpp @@ -13,18 +13,18 @@ namespace Nuake { Ref RigidBodyComponent::GetRigidBody() const { - return m_Rigidbody; + return Rigidbody; } void RigidBodyComponent::SyncTransformComponent(TransformComponent* tc) { - if (!m_Rigidbody) + if (!GetRigidBody()) return; } void RigidBodyComponent::SyncWithTransform(TransformComponent* tc) { - if (!m_Rigidbody) + if (!GetRigidBody()) return; } diff --git a/Nuake/src/Scene/Components/RigidbodyComponent.h b/Nuake/src/Scene/Components/RigidbodyComponent.h index 7fae80fb..85a54a89 100644 --- a/Nuake/src/Scene/Components/RigidbodyComponent.h +++ b/Nuake/src/Scene/Components/RigidbodyComponent.h @@ -13,7 +13,7 @@ namespace Nuake { { public: float Mass; - Ref m_Rigidbody; + Ref Rigidbody; RigidBodyComponent(); Ref GetRigidBody() const; diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index 2f5e0ff7..6a5839bc 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -120,6 +120,8 @@ namespace Nuake if (!Engine::IsPlayMode()) return; + InitializeRigidbodies(); + PhysicsManager::Get().Step(ts); } @@ -227,6 +229,11 @@ namespace Nuake Entity ent = Entity({ e, m_Scene }); Ref rigidBody; + if (rigidBodyComponent.GetRigidBody()) + { + continue; + } + if (ent.HasComponent()) { float mass = rigidBodyComponent.Mass; @@ -276,6 +283,7 @@ namespace Nuake { Logger::Log("Cannot use mesh collider without model component", WARNING); } + const auto& modelComponent = ent.GetComponent(); const auto& component = ent.GetComponent(); @@ -293,6 +301,8 @@ namespace Nuake PhysicsManager::Get().RegisterBody(rigidBody); } } + + rigidBodyComponent.Rigidbody = rigidBody; } } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.h b/Nuake/src/Scene/Systems/PhysicsSystem.h index 5c14c303..7b639e65 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.h +++ b/Nuake/src/Scene/Systems/PhysicsSystem.h @@ -14,7 +14,6 @@ namespace Nuake void Exit() override; private: - void InitializeShapes(); void InitializeQuakeMap(); void InitializeRigidbodies(); From 6433518a25c6c4e56a0f13244698d3fbbb403a48 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 02:46:35 -0400 Subject: [PATCH 07/10] Reduced physics maxstep to 16 --- Nuake/src/Core/Physics/DynamicWorld.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index d35b670f..a3ac5c1d 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -420,7 +420,7 @@ namespace Nuake // Do 1 collision step per 1 / 60th of a second (round up). int collisionSteps = 1; constexpr float minStepDuration = 1.0f / 90.0f; - constexpr int maxStepCount = 32; + constexpr int maxStepCount = 16; if(ts > minStepDuration) { collisionSteps = static_cast(ts) / minStepDuration; From 520867d3fe3f9af29f85fb90b84987539c47ee27 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 02:46:44 -0400 Subject: [PATCH 08/10] AddForce test --- Nuake/src/Core/Physics/DynamicWorld.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index a3ac5c1d..44d04461 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -275,6 +275,8 @@ namespace Nuake bodySettings.mUserData = rb->GetEntity().GetID(); // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr + + _JoltBodyInterface->AddForce(body, JPH::Vec3(10, 1, 0)); _registeredBodies.push_back((uint32_t)body.GetIndexAndSequenceNumber()); } From 6c15fe9e8536a4b1a14fb4be4832bfabb5ed3175 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 17 Jul 2023 03:11:07 -0400 Subject: [PATCH 09/10] Namespace cleanup --- Nuake/src/Scripting/Modules/PhysicsModule.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Nuake/src/Scripting/Modules/PhysicsModule.h b/Nuake/src/Scripting/Modules/PhysicsModule.h index ae211dd6..621750bb 100644 --- a/Nuake/src/Scripting/Modules/PhysicsModule.h +++ b/Nuake/src/Scripting/Modules/PhysicsModule.h @@ -8,8 +8,10 @@ #include #include "../Core/Physics/PhysicsManager.h" -namespace Nuake { - namespace ScriptAPI { +namespace Nuake +{ + namespace ScriptAPI + { class PhysicsModule : public ScriptModule { std::string ModuleName = "Engine"; From 1fee5b8d41d48a9b54274c799b9d212c82aaaf6b Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Tue, 18 Jul 2023 00:16:26 -0400 Subject: [PATCH 10/10] Added add force API endpoint --- Editor/resources/Scripts/Scene.wren | 15 +++++++++++ Nuake/src/Core/Physics/DynamicWorld.cpp | 26 +++++++++++++++---- Nuake/src/Core/Physics/DynamicWorld.h | 4 +-- Nuake/src/Core/Physics/Rigibody.h | 1 + Nuake/src/Core/Physics/Rigidbody.cpp | 9 ++++++- .../src/Scene/Components/RigidbodyComponent.h | 1 + Nuake/src/Scripting/Modules/SceneModule.h | 15 +++++++++++ 7 files changed, 63 insertions(+), 8 deletions(-) diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren index a5f0eb65..64aa3c93 100644 --- a/Editor/resources/Scripts/Scene.wren +++ b/Editor/resources/Scripts/Scene.wren @@ -24,6 +24,8 @@ class Scene { return Light.new(id) } else if (component == "CharacterController") { return CharacterController.new(id) + } else if (component == "RigidBody") { + return RigidBody.new(id) } else if (component == "Camera") { return Camera.new(id) } else if (component == "Transform") { @@ -70,6 +72,9 @@ class Scene { foreign static IsCharacterControllerOnGround_(e) //foreign static IsOnGround_(e) + // RigidBody + foreign static AddForce_(e, x, y, z) + foreign static TriggerGetOverlappingBodyCount_(e) foreign static TriggerGetOverlappingBodies_(e) @@ -159,6 +164,16 @@ class CharacterController { } } +class RigidBody { + construct new(id) { + _entityId = id + } + + AddForce(force) { + Scene.AddForce_(_entityId, force.x, force,y, force.z) + } +} + class Camera { construct new(id) { _entityId = id diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 44d04461..927190d5 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -275,8 +275,6 @@ namespace Nuake bodySettings.mUserData = rb->GetEntity().GetID(); // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr - - _JoltBodyInterface->AddForce(body, JPH::Vec3(10, 1, 0)); _registeredBodies.push_back((uint32_t)body.GetIndexAndSequenceNumber()); } @@ -452,9 +450,10 @@ namespace Nuake if (!_registeredBodies.empty()) { - _JoltBodyInterface->RemoveBodies(reinterpret_cast(_registeredBodies.data()), _registeredBodies.size()); - _registeredBodies.clear(); + _JoltBodyInterface->DestroyBodies(reinterpret_cast(_registeredBodies.data()), _registeredBodies.size()); } + + _registeredBodies.clear(); if (!_registeredCharacters.empty()) { @@ -466,7 +465,7 @@ namespace Nuake } } - void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3 velocity) + void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity) { const uint32_t entityHandle = entity.GetHandle(); if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) @@ -476,6 +475,23 @@ namespace Nuake } } + void DynamicWorld::AddForceToRigidBody(const Entity& entity, const Vector3& force) + { + auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); + for (const auto& body : _registeredBodies) + { + auto bodyId = static_cast(body); + auto entityId = static_cast(bodyInterface.GetUserData(bodyId)); + if (entityId == entity.GetHandle()) + { + bodyInterface.AddForce(bodyId, JPH::Vec3(force.x, force.y, force.z)); + return; + } + } + + Logger::Log("[PhysicsSystem] - Failed to add force to rigidbody. Body not found with id: " + std::to_string(entity.GetHandle())); + } + JPH::Ref DynamicWorld::GetJoltShape(const Ref shape) { JPH::ShapeSettings::ShapeResult result; diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index cfa25a48..d568c957 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -59,8 +59,8 @@ namespace Nuake void AddCharacterController(Ref cc); bool IsCharacterGrounded(const Entity& entity); // This is going to be ugly. TODO: Find a better way that passing itself as a parameter - void MoveAndSlideCharacterController(const Entity& entity, const Vector3 velocity); - + void MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity); + void AddForceToRigidBody(const Entity& entity, const Vector3& force); RaycastResult Raycast(glm::vec3 from, glm::vec3 to); void StepSimulation(Timestep ts); diff --git a/Nuake/src/Core/Physics/Rigibody.h b/Nuake/src/Core/Physics/Rigibody.h index 14b99b76..4a9bc9e1 100644 --- a/Nuake/src/Core/Physics/Rigibody.h +++ b/Nuake/src/Core/Physics/Rigibody.h @@ -37,6 +37,7 @@ namespace Nuake void SetShape(Ref shape); Ref GetShape() const { return _collisionShape; } Entity GetEntity() const { return _entity; } + void AddForce(const Vector3& force); }; } } diff --git a/Nuake/src/Core/Physics/Rigidbody.cpp b/Nuake/src/Core/Physics/Rigidbody.cpp index aaa93938..a8cbd7fc 100644 --- a/Nuake/src/Core/Physics/Rigidbody.cpp +++ b/Nuake/src/Core/Physics/Rigidbody.cpp @@ -1,6 +1,8 @@ #include "PhysicsShapes.h" #include "Rigibody.h" -#include "../Core.h" +#include "src/Core/Core.h" +#include "src/Core/Physics/PhysicsManager.h" + #include #include @@ -42,5 +44,10 @@ namespace Nuake { _entity = ent; } + + void RigidBody::AddForce(const Vector3& force) + { + PhysicsManager::Get().GetWorld()->AddForceToRigidBody(_entity, force); + } } } diff --git a/Nuake/src/Scene/Components/RigidbodyComponent.h b/Nuake/src/Scene/Components/RigidbodyComponent.h index 85a54a89..508b6597 100644 --- a/Nuake/src/Scene/Components/RigidbodyComponent.h +++ b/Nuake/src/Scene/Components/RigidbodyComponent.h @@ -24,6 +24,7 @@ namespace Nuake { void DrawShape(TransformComponent* tc); void DrawEditor(); + json Serialize() { BEGIN_SERIALIZE(); diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index bd2c2e4e..c0297bbc 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -57,6 +57,8 @@ namespace Nuake { RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide); RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround); + RegisterMethod("AddForce_(_,_,_,_)", (void*)AddForce); + RegisterMethod("TriggerGetOverlappingBodyCount_(_)", (void*)TriggerGetOverlappingBodyCount); RegisterMethod("TriggerGetOverlappingBodies_(_)", (void*)TriggerGetOverlappingBodies); @@ -126,6 +128,7 @@ namespace Nuake { if (name == "Transform") result = ent.HasComponent(); if (name == "Light") result = ent.HasComponent(); if (name == "QuakeMap") result = ent.HasComponent(); + if (name == "RigidBody") result = ent.HasComponent(); if (name == "CharacterController") { result = ent.HasComponent(); @@ -259,6 +262,18 @@ namespace Nuake { characterController.CharacterController->MoveAndSlide(Vector3(x, y, z)); } + static void AddForce(WrenVM* vm) + { + double handle = wrenGetSlotDouble(vm, 1); + double x = wrenGetSlotDouble(vm, 2); + double y = wrenGetSlotDouble(vm, 3); + double z = wrenGetSlotDouble(vm, 4); + + Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); + auto& rigidBodyComponent = ent.GetComponent(); + rigidBodyComponent.Rigidbody->AddForce(Vector3(x, y, z)); + } + static void GetTranslation(WrenVM* vm) { double handle = wrenGetSlotDouble(vm, 1);