From f45091349e9a2a83bd80a3587bcf51d563570fc5 Mon Sep 17 00:00:00 2001 From: Erris Date: Wed, 4 Mar 2026 11:18:16 +0100 Subject: [PATCH 1/5] added rotation --- imgui.ini | 26 ++++---- .../include/open_engine/scene/components.hpp | 6 +- open_engine/src/open_engine/physics.cpp | 10 +-- open_engine/src/open_engine/scene/scene.cpp | 61 +++++++++++-------- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/imgui.ini b/imgui.ini index 1e264a3..a2c5d73 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][WindowOverViewport_11111111] Pos=0,24 -Size=2552,1363 +Size=2560,1371 Collapsed=0 [Window][Debug##Default] @@ -10,31 +10,31 @@ Collapsed=0 [Window][Statistics] Pos=0,24 -Size=224,437 +Size=224,439 Collapsed=0 DockId=0x00000003,0 [Window][Properties] -Pos=2102,24 -Size=450,805 +Pos=2110,24 +Size=450,810 Collapsed=0 DockId=0x00000007,0 [Window][Viewport] Pos=226,61 -Size=1874,956 +Size=1882,964 Collapsed=0 DockId=0x00000012,0 [Window][Dear ImGui Demo] -Pos=2102,831 -Size=450,556 +Pos=2110,836 +Size=450,559 Collapsed=0 DockId=0x00000008,0 [Window][Scene] -Pos=0,463 -Size=224,924 +Pos=0,465 +Size=224,930 Collapsed=0 DockId=0x00000004,0 @@ -143,8 +143,8 @@ Collapsed=0 DockId=0x00000012,1 [Window][Assets] -Pos=226,1019 -Size=1874,368 +Pos=226,1027 +Size=1882,368 Collapsed=0 DockId=0x0000000C,0 @@ -156,12 +156,12 @@ DockId=0x0000000F,0 [Window][##play_state_bar] Pos=226,24 -Size=1874,35 +Size=1882,35 Collapsed=0 DockId=0x00000011,0 [Docking][Data] -DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2552,1363 Split=X +DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 Split=X DockNode ID=0x00000005 Parent=0x08BD597D SizeRef=820,1386 Split=X DockNode ID=0x00000001 Parent=0x00000005 SizeRef=224,1386 Split=Y Selected=0xE601B12F DockNode ID=0x00000003 Parent=0x00000001 SizeRef=255,417 Selected=0x553E127E diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index a896b12..c99dbe9 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -119,17 +119,15 @@ namespace OpenEngine { struct PhysicsBodyComponent { - enum class BodyType { Static = 0, Kinematic, Dynamic }; - BodyID body; - BodyType type = BodyType::Dynamic; - float linear_damping = 0.05f; float angular_damping = 0.05f; float gravity_factor = 1.0f; + EMotionType type = EMotionType::Dynamic; EActivation initial_activation_state = EActivation::Activate; + ObjectLayer layer = Layers::MOVING; PhysicsBodyComponent() = default; PhysicsBodyComponent(const PhysicsBodyComponent&) = default; diff --git a/open_engine/src/open_engine/physics.cpp b/open_engine/src/open_engine/physics.cpp index 7fde043..c3de441 100644 --- a/open_engine/src/open_engine/physics.cpp +++ b/open_engine/src/open_engine/physics.cpp @@ -38,7 +38,9 @@ namespace OpenEngine { tmp_allocator = CreateRef(10 * 1024 * 1024); - job_system = CreateRef(cMaxPhysicsJobs, cMaxPhysicsBarriers, thread::hardware_concurrency() - 1); + job_system = CreateRef(cMaxPhysicsJobs, + cMaxPhysicsBarriers, + thread::hardware_concurrency() - 1); physics_system.Init( 1024, @@ -49,10 +51,10 @@ namespace OpenEngine { object_vs_broadphase_layer_filter, object_vs_object_layer_filter); - physics_system.SetBodyActivationListener(&body_activation_listener); - physics_system.SetContactListener(&contact_listener); + //physics_system.SetBodyActivationListener(&body_activation_listener); + //physics_system.SetContactListener(&contact_listener); - // TODO: Move out of here and check the comment on Jolt's example + // TODO: Check the comment on Jolt's example physics_system.OptimizeBroadPhase(); } diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index 626b18d..07d26d0 100755 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -36,34 +37,40 @@ namespace OpenEngine { // Add it to the world body_interface->AddBody(floor->GetID(), EActivation::DontActivate); - auto view = registry.view(); + // TODO: Cleanup components aquisition + auto view = registry.view(); for (auto e : view) { Entity entity = { e, this }; - TransformComponent* tc; - if (entity.HasComponent()) - tc = &entity.GetComponents(); auto& pbc = entity.GetComponents(); + auto& tc = entity.GetComponents(); - PhysicsShapeComponent* psc; + PhysicsShapeComponent* psc = nullptr; if (entity.HasComponent()) psc = &entity.GetComponents(); + + glm::vec3& pos = tc.translation; + glm::vec3& scale = tc.scale; + glm::vec3& rot = tc.rotation; - glm::vec3& pos = tc->translation; + Quat quat = Quat::sEulerAngles(Vec3(rot.x, rot.y, rot.z)); BodyCreationSettings settings( - new BoxShape(Vec3(0.5f, 0.5f, 0.5f)), + new BoxShape(Vec3(scale.x * 0.5, scale.y * 0.5f, scale.z * 0.5f)), Vec3(pos.x, pos.y, pos.z), - Quat::sIdentity(), - EMotionType::Dynamic, - Layers::MOVING); + quat, + pbc.type, + pbc.layer); + settings.mLinearDamping = pbc.linear_damping; settings.mAngularDamping = pbc.angular_damping; settings.mGravityFactor = pbc.gravity_factor; - settings.mRestitution = psc->restitution; - settings.mFriction = psc->friction; + if (psc != nullptr) { + settings.mRestitution = psc->restitution; + settings.mFriction = psc->friction; + } - pbc.body = body_interface->CreateAndAddBody(settings, EActivation::Activate); + pbc.body = body_interface->CreateAndAddBody(settings, pbc.initial_activation_state); } } @@ -75,8 +82,8 @@ namespace OpenEngine { auto& pbc = entity.GetComponents(); - physics_engine.GetBodyInterface().RemoveBody(pbc.body); - physics_engine.GetBodyInterface().DestroyBody(pbc.body); + body_interface->RemoveBody(pbc.body); + body_interface->DestroyBody(pbc.body); } } @@ -156,7 +163,6 @@ namespace OpenEngine { body_interface->SetRestitution(body.body, shape->restitution); body_interface->SetFriction(body.body, shape->friction); - body_interface->SetGravityFactor(body.body, body.gravity_factor); } } @@ -169,10 +175,17 @@ namespace OpenEngine { auto& transform = entity.GetComponents(); auto& body = entity.GetComponents(); - auto position = physics_engine.GetBodyInterface().GetPosition(body.body); + auto position = body_interface->GetPosition(body.body); + auto rotation = body_interface->GetRotation(body.body).GetEulerAngles(); transform.translation.x = position.GetX(); transform.translation.y = position.GetY(); transform.translation.z = position.GetZ(); + + transform.rotation.x = rotation.GetX(); + transform.rotation.y = rotation.GetY(); + transform.rotation.z = rotation.GetZ(); + + body_interface->SetGravityFactor(body.body, body.gravity_factor); } SceneCamera* main_camera = nullptr; @@ -194,16 +207,16 @@ namespace OpenEngine { auto view = registry.view(); - for (const auto& entity : view) { - auto [transform, mesh] = view.get(entity); + for (const auto& e : view) { + auto [transform, mesh] = view.get(e); - Material material; + Material* material; - Entity _entity(entity, this); - if (_entity.HasComponent()) - material = _entity.GetComponents().material; + Entity entity(e, this); + if (entity.HasComponent()) + material = &entity.GetComponents().material; - Renderer3D::DrawMesh(mesh.mesh, material, GetTransformFromComp(transform)); + Renderer3D::DrawMesh(mesh.mesh, *material, GetTransformFromComp(transform)); /* if (sprite.texture) Renderer2D::DrawQuad(GetTransformFromComp(transform), From ecf27eae7367c532e25c1e08e7fcbb654a34178d Mon Sep 17 00:00:00 2001 From: Erris Date: Thu, 5 Mar 2026 01:22:41 +0100 Subject: [PATCH 2/5] various bug fix, serialization, adding shapes --- editor/include/editor_component.hpp | 3 +- editor/src/editor_component.cpp | 86 +++++++++++----- editor/src/panels/scene_hierarchy.cpp | 14 ++- open_engine/include/open_engine/physics.hpp | 3 + .../include/open_engine/scene/components.hpp | 20 ++-- .../include/open_engine/scene/scene.hpp | 3 +- open_engine/src/open_engine/scene/scene.cpp | 99 ++++++++++--------- .../open_engine/scene/scene_serializer.cpp | 79 +++++++++++++-- 8 files changed, 215 insertions(+), 92 deletions(-) diff --git a/editor/include/editor_component.hpp b/editor/include/editor_component.hpp index 5630dfc..7a3ecc7 100644 --- a/editor/include/editor_component.hpp +++ b/editor/include/editor_component.hpp @@ -19,7 +19,8 @@ namespace OpenEngine { void MeshOnImGuiRender(entt::registry®istry, entt::entity entity); void MaterialOnImGuiRender(entt::registry& registry, entt::entity entity); void BodyOnImGuiRender(entt::registry& registry, entt::entity entity); - void ShapeOnImGuiRender(entt::registry& registry, entt::entity entity); + void SphereShapeOnImGuiRender(entt::registry& registry, entt::entity entity); + void BoxShapeOnImGuiRender(entt::registry& registry, entt::entity entity); } #endif // EDITOR_COMPONENT_HPP diff --git a/editor/src/editor_component.cpp b/editor/src/editor_component.cpp index 6b1d445..855ee08 100644 --- a/editor/src/editor_component.cpp +++ b/editor/src/editor_component.cpp @@ -1,6 +1,7 @@ #include "imgui.h" #include "open_engine/renderer/renderer3d.hpp" #include "open_engine/scene/components.hpp" +#include #include #include #include @@ -181,12 +182,11 @@ namespace OpenEngine { { auto& mesh_component = registry.get(entity); - const char* items[] = { "Quad", "Cube" }; - static int item_selected_idx = 0; + const char* items[] = { "None", "Quad", "Cube" }; + int item_selected_idx = (int)mesh_component.primitive_type; if (ImGui::BeginCombo("Mesh", items[item_selected_idx])) { - for (int n = 0; n < 2; n++) - { + for (int n = 0; n < 3; n++) { const bool is_selected = (item_selected_idx == n); if (ImGui::Selectable(items[n], is_selected)) item_selected_idx = n; @@ -196,25 +196,26 @@ namespace OpenEngine { ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); + } - if ((int)mesh_component.primitive_type == (item_selected_idx + 1)) - return; + if ((int)mesh_component.primitive_type == (item_selected_idx)) + return; - mesh_component.primitive_type = (PrimitiveType)(item_selected_idx + 1); - switch (mesh_component.primitive_type) { - case OpenEngine::PrimitiveType::Quad: - { - mesh_component.mesh = CreateQuad((uint32_t)entity); - break; - } - case OpenEngine::PrimitiveType::Cube: - { - mesh_component.mesh = CreateCube((uint32_t)entity); - break; - } - default: + mesh_component.primitive_type = (PrimitiveType)(item_selected_idx); + switch (mesh_component.primitive_type) { + case OpenEngine::PrimitiveType::Quad: + { + mesh_component.mesh = CreateQuad((uint32_t)entity); break; - } + } + case OpenEngine::PrimitiveType::Cube: + { + mesh_component.mesh = CreateCube((uint32_t)entity); + break; + } + default: + mesh_component.mesh = nullptr; + break; } } @@ -233,16 +234,53 @@ namespace OpenEngine { { auto& body_comp = registry.get(entity); + const char* items[] = { "Static", "Kinematic", "Dynamic" }; + int item_selected_idx = body_comp.type; + + if (ImGui::BeginCombo("Body type", items[item_selected_idx])) { + for (int n = 0; n < 3; n++) + { + const bool is_selected = (item_selected_idx == n); + if (ImGui::Selectable(items[n], is_selected)) + item_selected_idx = n; + + // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + ImGui::EndCombo(); + } + + body_comp.type = item_selected_idx; + ImGui::SliderFloat("Linear damping", &body_comp.linear_damping, 0, 1); ImGui::SliderFloat("Angular damping", &body_comp.angular_damping, 0, 1); ImGui::SliderFloat("Gravity factor", &body_comp.gravity_factor, 0, 1); + ImGui::SliderFloat("Bounciness", &body_comp.restitution, 0, 1); + ImGui::SliderFloat("Friction", &body_comp.friction, 0, 1); } - void ShapeOnImGuiRender(entt::registry ®istry, entt::entity entity) + void SphereShapeOnImGuiRender(entt::registry ®istry, entt::entity entity) { - auto& shape_comp = registry.get(entity); + auto& sphere_comp = registry.get(entity); + + ImGui::DragFloat("Radius", + &sphere_comp.radius, + 0.1f, + 0.11f, FLT_MAX, + "%.2f", + ImGuiSliderFlags_AlwaysClamp); + } - ImGui::SliderFloat("Bounciness", &shape_comp.restitution, 0, 1); - ImGui::SliderFloat("Friction", &shape_comp.friction, 0, 1); + void BoxShapeOnImGuiRender(entt::registry ®istry, entt::entity entity) + { + auto& box_comp = registry.get(entity); + + ImGui::DragFloat3("Size", + glm::value_ptr(box_comp.size), + 0.1f, + 0.11f, FLT_MAX, + "%.2f", + ImGuiSliderFlags_AlwaysClamp); } } diff --git a/editor/src/panels/scene_hierarchy.cpp b/editor/src/panels/scene_hierarchy.cpp index 8b7e41c..bdcfdc6 100644 --- a/editor/src/panels/scene_hierarchy.cpp +++ b/editor/src/panels/scene_hierarchy.cpp @@ -25,7 +25,8 @@ namespace OpenEngine { RegisterDrawer("Mesh", &MeshOnImGuiRender); RegisterDrawer("Material", &MaterialOnImGuiRender); RegisterDrawer("Physics Body", &BodyOnImGuiRender); - RegisterDrawer("Physics Shape", &ShapeOnImGuiRender); + RegisterDrawer("Sphere Shape", &SphereShapeOnImGuiRender); + RegisterDrawer("Box Shape", &BoxShapeOnImGuiRender); scene = context; selected_context = {}; @@ -64,9 +65,14 @@ namespace OpenEngine { selected_context.AddComponent(); ImGui::CloseCurrentPopup(); } - if (!selected_context.HasComponent()) - if (ImGui::MenuItem("Physics shape")) { - selected_context.AddComponent(); + if (!selected_context.HasComponent()) + if (ImGui::MenuItem("Sphere shape")) { + selected_context.AddComponent(); + ImGui::CloseCurrentPopup(); + } + if (!selected_context.HasComponent()) + if (ImGui::MenuItem("Box shape")) { + selected_context.AddComponent(); ImGui::CloseCurrentPopup(); } } diff --git a/open_engine/include/open_engine/physics.hpp b/open_engine/include/open_engine/physics.hpp index 317d918..673a5d4 100644 --- a/open_engine/include/open_engine/physics.hpp +++ b/open_engine/include/open_engine/physics.hpp @@ -17,6 +17,7 @@ #include #include +#include using namespace JPH; using namespace JPH::literals; @@ -73,6 +74,8 @@ namespace BroadPhaseLayers static constexpr uint NUM_LAYERS(2); }; +inline JPH::Vec3 ToJolt(const glm::vec3& v) { return JPH::Vec3(v.x, v.y, v.z); }; + class BPLayerInterfaceImpl final : public BroadPhaseLayerInterface { public: diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index c99dbe9..9375e32 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -124,23 +124,27 @@ namespace OpenEngine { float linear_damping = 0.05f; float angular_damping = 0.05f; float gravity_factor = 1.0f; + float restitution = 0.8f; + float friction = 0.5f; - EMotionType type = EMotionType::Dynamic; - EActivation initial_activation_state = EActivation::Activate; - ObjectLayer layer = Layers::MOVING; + int type = (int)EMotionType::Static; + int initial_activation_state = (int)EActivation::Activate; + int layer = (int)Layers::MOVING; PhysicsBodyComponent() = default; PhysicsBodyComponent(const PhysicsBodyComponent&) = default; }; - struct PhysicsShapeComponent + // TODO: Let's add more shapes + struct BoxShapeComponent { - ShapeRefC shape; - - float restitution = 0.8f; - float friction = 0.5f; + glm::vec3 size = { 1.0f, 1.0f, 1.0f }; }; + struct SphereShapeComponent + { + float radius = 1.0f; + }; } #endif // COMPONENTS_HPP diff --git a/open_engine/include/open_engine/scene/scene.hpp b/open_engine/include/open_engine/scene/scene.hpp index 91dd480..c475590 100644 --- a/open_engine/include/open_engine/scene/scene.hpp +++ b/open_engine/include/open_engine/scene/scene.hpp @@ -18,6 +18,7 @@ namespace OpenEngine { public: Scene() = default; ~Scene() = default; + Scene(Scene& other) {}; void OnRuntimeStart(); void OnRuntimeStop(); @@ -52,8 +53,6 @@ namespace OpenEngine { std::vector pending_deletion; - //BodyID sphere_id; - friend class SceneSerializer; friend class Entity; }; diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index 07d26d0..dfc26fb 100755 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -1,4 +1,9 @@ #include "logging.hpp" +#include "physics.hpp" +#include "ref_scope.hpp" +#include +#include +#include #include #include @@ -8,6 +13,7 @@ #include #include +#include #include #include #include @@ -17,37 +23,42 @@ namespace OpenEngine { + static Ref CreateShape(entt::registry& reg, entt::entity e) + { + if (reg.any_of(e)) { + auto& c = reg.get(e); + return CreateRef(ToJolt(c.size * 0.5f)); + } + if (reg.any_of(e)) { + auto& c = reg.get(e); + return CreateRef(c.radius); + } + + OE_CORE_ERROR("Entity has no shape component!"); + return nullptr; + }; + void Scene::OnRuntimeStart() { body_interface = &physics_engine.GetBodyInterface(); - - BoxShapeSettings floor_shape_settings(Vec3(100.0f, 1.0f, 100.0f)); - floor_shape_settings.SetEmbedded(); // A ref counted object on the stack (base class RefTarget) should be marked as such to prevent it from being freed when its reference count goes to 0. - - // Create the shape - ShapeSettings::ShapeResult floor_shape_result = floor_shape_settings.Create(); - ShapeRefC floor_shape = floor_shape_result.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. - BodyCreationSettings floor_settings(floor_shape, RVec3(0.0_r, -1.0_r, 0.0_r), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING); - - // Create the actual rigid body - Body *floor = body_interface->CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr - - // Add it to the world - body_interface->AddBody(floor->GetID(), EActivation::DontActivate); - // TODO: Cleanup components aquisition auto view = registry.view(); for (auto e : view) { Entity entity = { e, this }; + + auto shape_settings = CreateShape(registry, e); + if (!shape_settings) + shape_settings = CreateRef(Vec3(0.5f, 0.5f, 0.5f)); + + auto shape_result = shape_settings->Create(); + if (shape_result.HasError()) { + OE_CORE_ERROR("Shape creation failed: {}", shape_result.GetError().c_str()); + continue; + } + ShapeRefC shape = shape_result.Get(); auto& pbc = entity.GetComponents(); auto& tc = entity.GetComponents(); - - PhysicsShapeComponent* psc = nullptr; - if (entity.HasComponent()) - psc = &entity.GetComponents(); glm::vec3& pos = tc.translation; glm::vec3& scale = tc.scale; @@ -55,22 +66,21 @@ namespace OpenEngine { Quat quat = Quat::sEulerAngles(Vec3(rot.x, rot.y, rot.z)); BodyCreationSettings settings( - new BoxShape(Vec3(scale.x * 0.5, scale.y * 0.5f, scale.z * 0.5f)), + shape, + //new BoxShape(Vec3(scale.x * 0.5, scale.y * 0.5f, scale.z * 0.5f)), Vec3(pos.x, pos.y, pos.z), quat, - pbc.type, + (EMotionType)pbc.type, pbc.layer); settings.mLinearDamping = pbc.linear_damping; settings.mAngularDamping = pbc.angular_damping; settings.mGravityFactor = pbc.gravity_factor; + settings.mRestitution = pbc.restitution; + settings.mFriction = pbc.friction; + settings.mObjectLayer = (ObjectLayer)pbc.layer; - if (psc != nullptr) { - settings.mRestitution = psc->restitution; - settings.mFriction = psc->friction; - } - - pbc.body = body_interface->CreateAndAddBody(settings, pbc.initial_activation_state); + pbc.body = body_interface->CreateAndAddBody(settings, (EActivation)pbc.initial_activation_state); } } @@ -157,14 +167,8 @@ namespace OpenEngine { body_interface->SetPosition(body.body, { pos.x, pos.y, pos.z }, EActivation::Activate); - PhysicsShapeComponent* shape; - if (entity.HasComponent()) { - shape = &entity.GetComponents(); - - body_interface->SetRestitution(body.body, shape->restitution); - body_interface->SetFriction(body.body, shape->friction); - } - + body_interface->SetRestitution(body.body, body.restitution); + body_interface->SetFriction(body.body, body.friction); } OnUpdatePhysics(); @@ -209,14 +213,13 @@ namespace OpenEngine { for (const auto& e : view) { auto [transform, mesh] = view.get(e); - - Material* material; - Entity entity(e, this); - if (entity.HasComponent()) - material = &entity.GetComponents().material; - Renderer3D::DrawMesh(mesh.mesh, *material, GetTransformFromComp(transform)); + Material material; + if (entity.HasComponent()) + material = entity.GetComponents().material; + + Renderer3D::DrawMesh(mesh.mesh, material, GetTransformFromComp(transform)); /* if (sprite.texture) Renderer2D::DrawQuad(GetTransformFromComp(transform), @@ -247,7 +250,8 @@ namespace OpenEngine { if (_entity.HasComponent()) material = _entity.GetComponents().material; - Renderer3D::DrawMesh(mesh.mesh, material, GetTransformFromComp(transform)); + if (mesh.mesh) + Renderer3D::DrawMesh(mesh.mesh, material, GetTransformFromComp(transform)); } Renderer3D::EndScene(); @@ -328,7 +332,12 @@ namespace OpenEngine { } template<> - void Scene::OnComponentAdded(Entity entity, PhysicsShapeComponent& component) + void Scene::OnComponentAdded(Entity entity, SphereShapeComponent& component) + { + } + + template<> + void Scene::OnComponentAdded(Entity entity, BoxShapeComponent& component) { } } diff --git a/open_engine/src/open_engine/scene/scene_serializer.cpp b/open_engine/src/open_engine/scene/scene_serializer.cpp index c217544..818e3d1 100644 --- a/open_engine/src/open_engine/scene/scene_serializer.cpp +++ b/open_engine/src/open_engine/scene/scene_serializer.cpp @@ -115,8 +115,7 @@ namespace OpenEngine { out << YAML::EndMap; // TransformComponent } - if (entity.HasComponent()) - { + if (entity.HasComponent()) { out << YAML::Key << "CameraComponent"; out << YAML::BeginMap; // CameraComponent @@ -151,8 +150,7 @@ namespace OpenEngine { out << YAML::EndMap; // SpriteRendererComponent } - if (entity.HasComponent()) - { + if (entity.HasComponent()) { out << YAML::Key << "MeshComponent"; out << YAML::BeginMap; // MeshComponent @@ -162,8 +160,7 @@ namespace OpenEngine { out << YAML::EndMap; // MeshComponent } - if (entity.HasComponent()) - { + if (entity.HasComponent()) { out << YAML::Key << "MaterialComponent"; out << YAML::BeginMap; // MaterialComponent @@ -177,6 +174,44 @@ namespace OpenEngine { out << YAML::EndMap; // MaterialComponent } + if (entity.HasComponent()) { + out << YAML::Key << "PhysicsBodyComponent"; + out << YAML::BeginMap; // PhysicsBodyComponent + + auto& pbc = entity.GetComponents(); + out << YAML::Key << "LinearDamping" << YAML::Value << pbc.linear_damping; + out << YAML::Key << "AngularDamping" << YAML::Value << pbc.angular_damping; + out << YAML::Key << "GravityFactor" << YAML::Value << pbc.gravity_factor; + out << YAML::Key << "Restitution" << YAML::Value << pbc.restitution; + out << YAML::Key << "Friction" << YAML::Value << pbc.friction; + + out << YAML::Key << "Type" << YAML::Value << pbc.type; + out << YAML::Key << "ActivationState" << YAML::Value << pbc.initial_activation_state; + out << YAML::Key << "Layer" << YAML::Value << pbc.layer; + + out << YAML::EndMap; // PhysicsBodyComponent + } + + if (entity.HasComponent()) { + out << YAML::Key << "BoxShapeComponent"; + out << YAML::BeginMap; //BoxShapeComponent + + auto& bsc = entity.GetComponents(); + out << YAML::Key << "Size" << YAML::Value << bsc.size; + + out << YAML::EndMap; //BoxShapeComponent + } + + if (entity.HasComponent()) { + out << YAML::Key << "SphereShapeComponent"; + out << YAML::BeginMap; //SphereShapeComponent + + auto& ssc = entity.GetComponents(); + out << YAML::Key << "Radius" << YAML::Value << ssc.radius; + + out << YAML::EndMap; //SphereShapeComponent + } + out << YAML::EndMap; } @@ -292,8 +327,7 @@ namespace OpenEngine { } auto material_component = entity["MaterialComponent"]; - if (material_component) - { + if (material_component) { auto& material = deserializedEntity.AddComponent(); material.material.albedo = material_component["Albedo"].as(); material.material.roughness = material_component["Roughness"].as(); @@ -301,6 +335,35 @@ namespace OpenEngine { material.material.ambient_strength = material_component["AmbiantStrength"].as(); material.material.specular_strength = material_component["SpecularStrength"].as(); } + + auto physics_body_component = entity["PhysicsBodyComponent"]; + if (physics_body_component) { + auto& pbc = deserializedEntity.AddComponent(); + + pbc.linear_damping = physics_body_component["LinearDamping"].as(); + pbc.angular_damping = physics_body_component["AngularDamping"].as(); + pbc.gravity_factor = physics_body_component["GravityFactor"].as(); + pbc.restitution = physics_body_component["Restitution"].as(); + pbc.friction = physics_body_component["Friction"].as(); + + pbc.type = physics_body_component["Type"].as(); + pbc.initial_activation_state = physics_body_component["ActivationState"].as(); + pbc.layer = physics_body_component["Layer"].as(); + } + + auto box_shape_component = entity["BoxShapeComponent"]; + if (box_shape_component) { + auto& bsc = deserializedEntity.AddComponent(); + + bsc.size = box_shape_component["Size"].as(); + } + + auto sphere_shape_component = entity["SphereShapeComponent"]; + if (sphere_shape_component) { + auto& ssc = deserializedEntity.AddComponent(); + + ssc.radius = sphere_shape_component["Radius"].as(); + } } } From a187bd2894f9af5fa96b051b135b96b632cf5dcb Mon Sep 17 00:00:00 2001 From: Erris Date: Thu, 5 Mar 2026 01:23:26 +0100 Subject: [PATCH 3/5] needed for jolt --- open_engine/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/open_engine/CMakeLists.txt b/open_engine/CMakeLists.txt index 808c01d..736726e 100644 --- a/open_engine/CMakeLists.txt +++ b/open_engine/CMakeLists.txt @@ -74,3 +74,5 @@ add_subdirectory(vendor/glad ) add_subdirectory("vendor/nativefiledialog-extended") + +target_compile_options(open_engine PRIVATE -msse4.1 -msse4.2 -mavx -mavx2) From 5be3c6185959a5677a8264b520f7fe6c78063ab5 Mon Sep 17 00:00:00 2001 From: Erris Date: Thu, 5 Mar 2026 22:30:48 +0100 Subject: [PATCH 4/5] fixing segv when closing the program --- open_engine/include/open_engine/renderer/uniform_buffer.hpp | 2 +- open_engine/src/open_engine/renderer/renderer3d.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/open_engine/include/open_engine/renderer/uniform_buffer.hpp b/open_engine/include/open_engine/renderer/uniform_buffer.hpp index c6ccf6f..d38afe8 100644 --- a/open_engine/include/open_engine/renderer/uniform_buffer.hpp +++ b/open_engine/include/open_engine/renderer/uniform_buffer.hpp @@ -8,7 +8,7 @@ namespace OpenEngine { class UniformBuffer { public: - virtual ~UniformBuffer() {} + virtual ~UniformBuffer() = default; virtual void SetData(const void* data, uint32_t size, uint32_t offset = 0) = 0; diff --git a/open_engine/src/open_engine/renderer/renderer3d.cpp b/open_engine/src/open_engine/renderer/renderer3d.cpp index 58120ba..291bfb7 100644 --- a/open_engine/src/open_engine/renderer/renderer3d.cpp +++ b/open_engine/src/open_engine/renderer/renderer3d.cpp @@ -205,8 +205,10 @@ namespace OpenEngine { void Renderer3D::Shutdown() { - renderer_data.camera_uniform_buffer.reset(); renderer_data.color_shader_3d.reset(); + renderer_data.camera_uniform_buffer.reset(); + renderer_data.transform_uniform_buffer.reset(); + renderer_data.material_uniform_buffer.reset(); } void Renderer3D::BeginScene(const SceneCamera& camera, const glm::mat4& transform) From 2d3fe57d5a80791e0c05cad31538230637f71b69 Mon Sep 17 00:00:00 2001 From: Erris Date: Fri, 6 Mar 2026 18:07:51 +0100 Subject: [PATCH 5/5] added UUID --- editor/src/panels/scene_hierarchy.cpp | 1 - imgui.ini | 30 ++++++++-------- open_engine/include/open_engine/core/uuid.hpp | 36 +++++++++++++++++++ .../include/open_engine/scene/components.hpp | 16 ++++++--- .../include/open_engine/scene/entity.hpp | 4 +++ .../scene/native_scriptable_entity.hpp | 1 + .../include/open_engine/scene/scene.hpp | 2 ++ open_engine/src/open_engine/core/uuid.cpp | 24 +++++++++++++ open_engine/src/open_engine/scene/scene.cpp | 12 +++++++ .../open_engine/scene/scene_serializer.cpp | 8 +++-- 10 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 open_engine/include/open_engine/core/uuid.hpp create mode 100644 open_engine/src/open_engine/core/uuid.cpp diff --git a/editor/src/panels/scene_hierarchy.cpp b/editor/src/panels/scene_hierarchy.cpp index bdcfdc6..652febf 100644 --- a/editor/src/panels/scene_hierarchy.cpp +++ b/editor/src/panels/scene_hierarchy.cpp @@ -1,4 +1,3 @@ -#include "open_engine/scene/components.hpp" #include "open_engine/scene/entity.hpp" #include "editor_component.hpp" diff --git a/imgui.ini b/imgui.ini index a2c5d73..9bfba42 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][WindowOverViewport_11111111] Pos=0,24 -Size=2560,1371 +Size=1272,1363 Collapsed=0 [Window][Debug##Default] @@ -10,31 +10,31 @@ Collapsed=0 [Window][Statistics] Pos=0,24 -Size=224,439 +Size=224,437 Collapsed=0 DockId=0x00000003,0 [Window][Properties] -Pos=2110,24 -Size=450,810 +Pos=822,24 +Size=450,805 Collapsed=0 DockId=0x00000007,0 [Window][Viewport] Pos=226,61 -Size=1882,964 +Size=594,956 Collapsed=0 DockId=0x00000012,0 [Window][Dear ImGui Demo] -Pos=2110,836 -Size=450,559 +Pos=822,831 +Size=450,556 Collapsed=0 DockId=0x00000008,0 [Window][Scene] -Pos=0,465 -Size=224,930 +Pos=0,463 +Size=224,924 Collapsed=0 DockId=0x00000004,0 @@ -143,8 +143,8 @@ Collapsed=0 DockId=0x00000012,1 [Window][Assets] -Pos=226,1027 -Size=1882,368 +Pos=226,1019 +Size=594,368 Collapsed=0 DockId=0x0000000C,0 @@ -156,12 +156,12 @@ DockId=0x0000000F,0 [Window][##play_state_bar] Pos=226,24 -Size=1882,35 +Size=594,35 Collapsed=0 DockId=0x00000011,0 [Docking][Data] -DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 Split=X +DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=1272,1363 Split=X DockNode ID=0x00000005 Parent=0x08BD597D SizeRef=820,1386 Split=X DockNode ID=0x00000001 Parent=0x00000005 SizeRef=224,1386 Split=Y Selected=0xE601B12F DockNode ID=0x00000003 Parent=0x00000001 SizeRef=255,417 Selected=0x553E127E @@ -178,6 +178,6 @@ DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 DockNode ID=0x0000000C Parent=0x00000009 SizeRef=409,368 Selected=0x42C24103 DockNode ID=0x0000000A Parent=0x00000002 SizeRef=483,600 Selected=0x1BCA3180 DockNode ID=0x00000006 Parent=0x08BD597D SizeRef=450,1386 Split=Y Selected=0x8C72BEA8 - DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,810 Selected=0x8C72BEA8 - DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,559 Selected=0x5E5F7166 + DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,805 Selected=0x8C72BEA8 + DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,556 Selected=0x5E5F7166 diff --git a/open_engine/include/open_engine/core/uuid.hpp b/open_engine/include/open_engine/core/uuid.hpp new file mode 100644 index 0000000..3ab6d36 --- /dev/null +++ b/open_engine/include/open_engine/core/uuid.hpp @@ -0,0 +1,36 @@ +#ifndef UUID_HPP +#define UUID_HPP + +#include + +namespace OpenEngine { + + class UUID + { + public: + UUID(); + UUID(uint64_t uuid); + UUID(const UUID&) = default; + + operator uint64_t() const { return uuid; }; + + private: + uint64_t uuid; + }; + +} + +namespace std { + + template<> + struct hash + { + std::size_t operator()(const OpenEngine::UUID& uuid) const + { + return hash()((uint64_t)uuid); + } + }; + +} + +#endif // UUID_HPP diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index 9375e32..d5fe5cb 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -1,12 +1,12 @@ #ifndef COMPONENTS_HPP #define COMPONENTS_HPP -#include "open_engine/scene/native_scriptable_entity.hpp" #include "open_engine/renderer/renderer3d.hpp" #include "open_engine/scene/scene_camera.hpp" #include "open_engine/renderer/texture.hpp" #include "open_engine/renderer/renderer3d.hpp" #include "open_engine/physics.hpp" +#include "open_engine/core/uuid.hpp" #include #include @@ -28,6 +28,15 @@ namespace OpenEngine { + struct IDComponent + { + UUID id; + + IDComponent() = default; + IDComponent(const IDComponent&) = default; + IDComponent(const UUID& uuid) : id(uuid) {}; + }; + struct TagComponent { std::string tag; @@ -74,6 +83,8 @@ namespace OpenEngine { CameraComponent(const CameraComponent&) = default; }; + class NativeScriptableEntity; + struct NativeScriptComponent { NativeScriptableEntity* instance = nullptr; @@ -81,9 +92,6 @@ namespace OpenEngine { NativeScriptableEntity* (*InstanciateScript)(); void (*DestroyInstanceScript)(NativeScriptComponent*); - void OnImGuiRender(Entity& entity) - {}; - template void Bind() { diff --git a/open_engine/include/open_engine/scene/entity.hpp b/open_engine/include/open_engine/scene/entity.hpp index bfe1e30..50d375a 100644 --- a/open_engine/include/open_engine/scene/entity.hpp +++ b/open_engine/include/open_engine/scene/entity.hpp @@ -3,6 +3,8 @@ #include "open_engine/core.hpp" +#include "open_engine/core/uuid.hpp" +#include "open_engine/scene/components.hpp" #include "open_engine/scene/scene.hpp" #include @@ -48,6 +50,8 @@ namespace OpenEngine { return scene->registry.all_of(handle); }; + UUID GetUUID() { return GetComponents().id; }; + operator bool() const { return handle != entt::null; }; operator entt::entity() const { return handle; }; operator uint32_t() const { return (uint32_t)handle; }; diff --git a/open_engine/include/open_engine/scene/native_scriptable_entity.hpp b/open_engine/include/open_engine/scene/native_scriptable_entity.hpp index dceb611..65ae6c2 100644 --- a/open_engine/include/open_engine/scene/native_scriptable_entity.hpp +++ b/open_engine/include/open_engine/scene/native_scriptable_entity.hpp @@ -1,6 +1,7 @@ #ifndef NATIVE_SCRIPTABLE_ENTITY_HPP #define NATIVE_SCRIPTABLE_ENTITY_HPP +#include "open_engine/scene/native_scriptable_entity.hpp" #include "open_engine/scene/entity.hpp" namespace OpenEngine { diff --git a/open_engine/include/open_engine/scene/scene.hpp b/open_engine/include/open_engine/scene/scene.hpp index c475590..24d405a 100644 --- a/open_engine/include/open_engine/scene/scene.hpp +++ b/open_engine/include/open_engine/scene/scene.hpp @@ -1,6 +1,7 @@ #ifndef SCENE_HPP #define SCENE_HPP +#include "open_engine/core/uuid.hpp" #include "open_engine/renderer/editor_camera.hpp" #include "open_engine/physics.hpp" @@ -24,6 +25,7 @@ namespace OpenEngine { void OnRuntimeStop(); Entity CreateEntity(const std::string& name = std::string()); + Entity CreateEntityWithUUID(UUID uuid, const std::string& name = std::string()); void DeleteEntity(entt::entity entity); void MarkEntityForDeletion(Entity entity); diff --git a/open_engine/src/open_engine/core/uuid.cpp b/open_engine/src/open_engine/core/uuid.cpp new file mode 100644 index 0000000..5e08f75 --- /dev/null +++ b/open_engine/src/open_engine/core/uuid.cpp @@ -0,0 +1,24 @@ +#include +#include + +#include + +#include + +namespace OpenEngine { + + static std::random_device s_random_device; + static std::mt19937_64 s_engine(s_random_device()); + static std::uniform_int_distribution s_uniform_distribution; + + UUID::UUID() + : uuid(s_uniform_distribution(s_engine)) + { + } + + UUID::UUID(uint64_t uuid) + : uuid(uuid) + { + } + +} diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index dfc26fb..75297ad 100755 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -98,9 +99,15 @@ namespace OpenEngine { } Entity Scene::CreateEntity(const std::string& name) + { + return CreateEntityWithUUID(UUID(), name); + } + + Entity Scene::CreateEntityWithUUID(UUID uuid, const std::string& name) { Entity entity = { registry.create(), this }; + entity.AddComponent(uuid); auto& tag = entity.AddComponent(); tag.tag = name.empty() ? "Entity" : name; @@ -340,4 +347,9 @@ namespace OpenEngine { void Scene::OnComponentAdded(Entity entity, BoxShapeComponent& component) { } + + template<> + void Scene::OnComponentAdded(Entity entity, IDComponent& component) + { + } } diff --git a/open_engine/src/open_engine/scene/scene_serializer.cpp b/open_engine/src/open_engine/scene/scene_serializer.cpp index 818e3d1..9733ea6 100644 --- a/open_engine/src/open_engine/scene/scene_serializer.cpp +++ b/open_engine/src/open_engine/scene/scene_serializer.cpp @@ -88,8 +88,10 @@ namespace OpenEngine { static void SerializeEntity(YAML::Emitter& out, Entity entity) { + OE_CORE_ASSERT(entity.HasComponent(), "Entity is missing UUID."); + out << YAML::BeginMap; - out << YAML::Key << "Entity" << YAML::Value << "412741205"; // Needs random + out << YAML::Key << "Entity" << YAML::Value << entity.GetUUID(); // Needs random if (entity.HasComponent()) { @@ -258,7 +260,7 @@ namespace OpenEngine { { for (auto entity : entities) { - uint64_t uuid = entity["Entity"].as(); // TODO + uint64_t uuid = entity["Entity"].as(); std::string name; auto tagComponent = entity["TagComponent"]; @@ -267,7 +269,7 @@ namespace OpenEngine { OE_CORE_TRACE("Deserialized entity with ID = {0}, name = {1}", uuid, name); - Entity deserializedEntity = context->CreateEntity(name); + Entity deserializedEntity = context->CreateEntityWithUUID(uuid, name); auto transformComponent = entity["TransformComponent"]; if (transformComponent)