diff --git a/enginecustom/src/inc/system/application_class.h b/enginecustom/src/inc/system/application_class.h index 07d44fb..4410c07 100644 --- a/enginecustom/src/inc/system/application_class.h +++ b/enginecustom/src/inc/system/application_class.h @@ -175,7 +175,7 @@ public: private: bool render(float, float, float, float, float); - bool render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time); + bool render_physics(float delta_time); bool update_mouse_strings(int, int, bool); bool update_fps(); bool update_render_count_string(int); diff --git a/enginecustom/src/inc/system/ecs/entity_manager.h b/enginecustom/src/inc/system/ecs/entity_manager.h index 1ff1686..6e0b535 100644 --- a/enginecustom/src/inc/system/ecs/entity_manager.h +++ b/enginecustom/src/inc/system/ecs/entity_manager.h @@ -54,6 +54,18 @@ public: } } + // Obtenir toutes les entités + std::vector> GetAllEntities() { + std::vector> result; + result.reserve(m_Entities.size()); + + for (const auto& [id, entity] : m_Entities) { + result.push_back(entity); + } + + return result; + } + // Obtenir toutes les entités qui ont un composant spécifique template std::vector> GetEntitiesWithComponent() { diff --git a/enginecustom/src/src/system/application_class.cpp b/enginecustom/src/src/system/application_class.cpp index 0a3e87a..289f982 100644 --- a/enginecustom/src/src/system/application_class.cpp +++ b/enginecustom/src/src/system/application_class.cpp @@ -1791,36 +1791,34 @@ void application_class::culling_thread_function() } // Traitement des files d'objets normaux (sans la skybox) - std::vector>> queues = { - std::ref(object_), std::ref(cubes_), std::ref(terrain_chunk_) - }; + auto all_entity = entity_manager_->GetAllEntities(); - for (auto& queueRef : queues) - { - std::vector& queue = queueRef.get(); + for (auto& entity : all_entity) + { + std::lock_guard lock(objects_mutex_); + auto renderComponent = entity->GetComponent(); + if (renderComponent && renderComponent->GetModel()) + { + // Extraction des données de position via le composant TransformComponent + auto transformComponent = entity->GetComponent(); + if (transformComponent) + { + XMVECTOR transformPosition = transformComponent->GetPosition(); + float x = XMVectorGetX(transformPosition); + float y = XMVectorGetY(transformPosition); + float z = XMVectorGetZ(transformPosition); - std::lock_guard lock(objects_mutex_); + // Calcul du rayon approximatif + XMVECTOR scale = transformComponent->GetScale(); + float radius = max(max(XMVectorGetX(scale), XMVectorGetY(scale)), XMVectorGetZ(scale)); - for (auto* object : queue) - { - if (!object) continue; - - // Extraction des données de position - XMVECTOR position = object->GetPosition(); - float x = XMVectorGetX(position); - float y = XMVectorGetY(position); - float z = XMVectorGetZ(position); - - // Calcul du rayon approximatif - XMVECTOR scale = object->GetScale(); - float radius = max(max(XMVectorGetX(scale), XMVectorGetY(scale)), XMVectorGetZ(scale)); - - // Vérification du frustum - bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance()); - object->SetVisible(visible); - - } - } + // verification du frustum + bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance()); + renderComponent->SetVisible(visible); + } + + } + } // Pause pour éviter de surcharger le CPU std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 Hz @@ -1962,83 +1960,16 @@ void application_class::update_skybox_position() } -bool application_class::render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time) { - const float maxSpeed = 50.0f; // Limite de vitesse maximale +bool application_class::render_physics(float delta_time) { - for (auto& object : object_) { - if (object == nullptr) { - Logger::Get().Log("object is null", __FILE__, __LINE__, Logger::LogLevel::Error); - return false; + // update the physical entity if they have the physics component + auto entities_with_physics = entity_manager_->GetEntitiesWithComponent(); + for (auto& entity : entities_with_physics) { + auto physicsComponent = entity->GetComponent(); + if (physicsComponent) { + // Update the physics component with the input keys and delta time + physicsComponent->Update(delta_time); } - - if (!object->IsPhysicsEnabled()) { - continue; - } - - // Reset acceleration for the new frame - object->SetAcceleration(XMVectorZero()); - object->SetGrounded(false); - - for (auto& chunk : terrain_chunk_) { - if (!physics_->IsColliding(object, chunk)) { - continue; - } - - object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f)); - object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f)); - object->SetGrounded(true); - } - - for (auto& object2 : object_) { - if (object->GetId() == object2->GetId()) { - continue; - } - - if (!physics_->IsColliding(object, object2)) { - continue; - } - - object->SetVelocity(XMVectorZero()); - object->SetAcceleration(XMVectorZero()); - } - - float forceX = 0, forceY = 0, forceZ = 0, forceW = 0; - - if (key_left) { - forceX = -40.0f; - } - if (key_right) { - forceX = 40.0f; - } - if (key_up) { - forceY = 40.0f; - } - if (key_down && !object->IsGrounded()) { - forceY = -40.0f; - } - - XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW); - physics_->AddForce(object, force); - - object->AddVelocity(delta_time); - - XMVECTOR velocity = object->GetVelocity(); - float speed = XMVectorGetX(XMVector3Length(velocity)); - if (speed > maxSpeed) { - velocity = XMVectorScale(velocity, maxSpeed / speed); - object->SetVelocity(velocity); - } - - if (object->m_gravityEnabled) { - physics_->ApplyGravity(object, delta_time); - } - - if (XMVectorGetY(object->GetPosition()) < -30.0f) { - XMVECTOR currentPosition = object->GetPosition(); - object->SetPosition(XMVectorSetY(currentPosition, 50.0f)); - } - - object->m_previousPosition = object->GetPosition(); } return true; @@ -2057,7 +1988,7 @@ void application_class::physics_thread_function() lastTime = now; float deltaTime = 1.0f / static_cast(physics_tick_rate_); - bool result = render_physics(inputs_.key_left, inputs_.key_right, inputs_.key_up, inputs_.key_down, deltaTime); + bool result = render_physics(deltaTime); if (!result) { Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);