Physics working

This commit is contained in:
Confideo-IOM
2026-08-24 20:59:24 +01:00
parent 20c0794d10
commit 1253571c17
146 changed files with 4469 additions and 1273 deletions
+13
View File
@@ -49,6 +49,19 @@ const uint32_t DIRECTIONAL_SHADOW_MAP_SIZE = 4096;
const uint32_t SPOT_SHADOW_MAP_SIZE = 1024;
const uint32_t POINT_SHADOW_MAP_SIZE = 1024;
enum class HYDRA_PHYSICS_TYPE
{
HYDRA_RIGID_BODY =0,
HYDRA_STATIC_BODY = 1,
HYDRA_INVALID_PHYSICS_TYPE
};
enum class HYDRA_PHYSICS_SHAPE
{
HYDRA_PHYSICS_BOX = 0,
HYDRA_PHYSICS_CAPSULE = 1,
HYDRA_INVALID_PHYSICS_TYPE
};
enum class HYDRA_RENDERABLE_TYPE
{
HYDRA_COLOUR = 1,
+19 -16
View File
@@ -6,7 +6,9 @@
#include "../ecs/HydraEntityManager.h"
#include "../ecs/HydraECS.h"
#include "../ecs/components/HydraPositionComponent.h"
#include "../ecs/components/HydraPhysicsComponent.h"
#include "../ecs/systems/HydraTransformSystem.h"
#include "../ecs/systems/HydraPhysicsSystem.h"
#include "../fileaccess/HydraGLTFLoader.h"
#include "../task/actor/HydraLoadGLTFFileTask.hpp"
#include "../scheduler/HydraTaskScheduler.h"
@@ -167,18 +169,11 @@ void HydraActorManager::SetPosition(HydraID actorID, glm::dvec3 position)
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actorID);
pos_comp.position = position;
// if (ecs->HasComponent<HydraECSPhysicsComponent>(actorID))
// {
// HydraECSPhysicsComponent &phys_comp = ecs->GetComponent<HydraECSPhysicsComponent>(actorID);
// // const float* matrix = glm::value_ptr<float>(GetActorPose(actorID));
// phys_comp.physics_definition.pose = GetActorPose(actorID);
// }
if(!pos_comp.needs_update)
if(!pos_comp.needs_update)
{
ecs->GetSystem<HydraTransformSystem>()->UpdateTransform(actorID);
ecs->GetSystem<HydraTransformSystem>()->UpdateTransform(actorID);
}
}
@@ -189,16 +184,24 @@ void HydraActorManager::SetOrientation(HydraID actorID, float x, float y, float
pos_comp.orientation = glm::vec3(x, y, z);
if(!pos_comp.needs_update)
{
// if (ecs->HasComponent<HydraECSPhysicsComponent>(actorID))
// {
// HydraECSPhysicsComponent &phys_comp = ecs->GetComponent<HydraECSPhysicsComponent>(actorID);
// phys_comp.physics_definition.pose = GetActorPose(actorID);
// }
ecs->GetSystem<HydraTransformSystem>()->UpdateTransform(actorID);
}
}
void HydraActorManager::SetPositionAndOrientation(HydraID actorID, glm::dvec3 position, glm::vec3 euler_angles, bool ignore_physics)
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actorID);
pos_comp.position = position;
pos_comp.orientation = glm::vec3(euler_angles.x,euler_angles.y,euler_angles.z);
if(!pos_comp.needs_update)
{
ecs->GetSystem<HydraTransformSystem>()->UpdateTransform(actorID, ignore_physics);
}
}
void HydraActorManager::SetScale(HydraID actor_id, glm::dvec3 scaling)
{
HydraECS *ecs = GetEngine()->ECS();
@@ -48,6 +48,7 @@ class CLASS_DEFINE HydraActorManager : public HydraObject
void SetPosition(HydraID actorID, glm::dvec3 position);
void SetOrientation(HydraID actorID, float x, float y, float z);
void SetPositionAndOrientation(HydraID actorID, glm::dvec3 position, glm::vec3 euler_angles, bool ignore_physics = false);
void SetScale(HydraID actor_id, glm::dvec3 scaling);
void TranslateActor(HydraID actorID, glm::dvec3 translation);
+12 -13
View File
@@ -7,6 +7,7 @@
#include "../ecs/components/HydraMeshComponent.h"
#include "../ecs/components/HydraRenderableComponent.h"
#include "../ecs/components/HydraMaterialComponent.h"
#include "../ecs/components/HydraPhysicsComponent.h"
#include "../buffer/HydraMaterialBufferManager.h"
#include "../buffer/HydraTextureBufferManager.h"
#include "../buffer/HydraMaterialBuffer.h"
@@ -23,6 +24,7 @@ void HydraPlaneActor::InitialiseComponents()
_CreateMaterial();
_CreateMesh();
_CreateRenderableComponent();
_CreatePhysics();
}
void HydraPlaneActor::DeferredInitialiseComponents()
@@ -183,21 +185,18 @@ void HydraPlaneActor::_CreateMaterial()
void HydraPlaneActor::_CreatePhysics()
{
// HydraECSPhysicsComponent physics_component;
// physics_component.physics_state = Hydra_State::Waiting;
// physics_component.physics_definition.actor_id = GetID();
// physics_component.physics_definition.physics_type = HydraPhyicsType::Static;
// physics_component.physics_definition.physics_shape = HydraPhysicsShape::HydraBoxShape;
// physics_component.physics_definition.mass = 100000.0f;
// physics_component.physics_definition.extent_x = 1000.0f;
// physics_component.physics_definition.extent_y = 0.5f;
// physics_component.physics_definition.extent_z = 1000.0f;
// physics_component.physics_definition.dynamic_friction = 0.5f;
// physics_component.physics_definition.restitution = 0.5f;
HydraEngine *engine = GetEngine();
HydraPhysicsComponent phys_comp = {};
phys_comp.mass = 0;
phys_comp.phyics_type = HYDRA_PHYSICS_TYPE::HYDRA_STATIC_BODY;
phys_comp.physics_shape = HYDRA_PHYSICS_SHAPE::HYDRA_PHYSICS_BOX;
// HydraECS* ecs = GetEngine()->ECS();
phys_comp.depth = 1000.0f;
phys_comp.height = 1.0f;
phys_comp.width = 1000.0f;
// ecs->AddComponent<HydraECSPhysicsComponent>(GetID(), physics_component);
engine->ECS()->AddComponent<HydraPhysicsComponent>(GetID(), phys_comp);
}
void HydraPlaneActor::_CreateRenderableComponent()
{
+11 -14
View File
@@ -7,6 +7,7 @@
#include "../ecs/components/HydraMeshComponent.h"
#include "../ecs/components/HydraRenderableComponent.h"
#include "../ecs/components/HydraMaterialComponent.h"
#include "../ecs/components/HydraPhysicsComponent.h"
#include "../buffer/HydraMaterialBufferManager.h"
#include "../buffer/HydraTextureBufferManager.h"
#include "../buffer/HydraMaterialBuffer.h"
@@ -18,7 +19,7 @@ void HydraTestCubeActor::InitialiseComponents()
// HydraECS* ecs = GetEngine()->ECS();
// HydraID entity_id = ecs->CreateEntity();
_CreatePosition();
// _CreatePhysics();
_CreatePhysics();
_CreateMaterial();
_CreateMesh();
@@ -183,21 +184,17 @@ void HydraTestCubeActor::_CreateMaterial()
void HydraTestCubeActor::_CreatePhysics()
{
// HydraECSPhysicsComponent physics_component;
// physics_component.physics_state = Hydra_State::Waiting;
// physics_component.physics_definition.actor_id = GetID();
// physics_component.physics_definition.physics_type = HydraPhyicsType::Static;
// physics_component.physics_definition.physics_shape = HydraPhysicsShape::HydraBoxShape;
// physics_component.physics_definition.mass = 100000.0f;
// physics_component.physics_definition.extent_x = 1000.0f;
// physics_component.physics_definition.extent_y = 0.5f;
// physics_component.physics_definition.extent_z = 1000.0f;
// physics_component.physics_definition.dynamic_friction = 0.5f;
// physics_component.physics_definition.restitution = 0.5f;
HydraEngine *engine = GetEngine();
HydraPhysicsComponent phys_comp = {};
phys_comp.mass = 0;
phys_comp.phyics_type = HYDRA_PHYSICS_TYPE::HYDRA_RIGID_BODY;
phys_comp.physics_shape = HYDRA_PHYSICS_SHAPE::HYDRA_PHYSICS_BOX;
// HydraECS* ecs = GetEngine()->ECS();
phys_comp.depth = 1.0f;
phys_comp.height = 1.0f;
phys_comp.width = 1.0f;
// ecs->AddComponent<HydraECSPhysicsComponent>(GetID(), physics_component);
engine->ECS()->AddComponent<HydraPhysicsComponent>(GetID(), phys_comp);
}
void HydraTestCubeActor::_CreateRenderableComponent()
{
@@ -1,15 +1,20 @@
#ifndef HYDRAECSPHYSICSCOMPONENT
#define HYDRAECSPHYSICSCOMPONENT
#include "../engine/HydraEngine.h"
#include "../../engine/HydraEngine.h"
//#include "../physics/HydraPhysicsDefinition.h"
#include <box3d/box3d.h>
struct HydraPhysicsComponent
{
//
HYDRA_STATE physics_state = HYDRA_STATE::Empty;
// HydraPhysicsDefinitionStruct physics_definition;
HYDRA_PHYSICS_TYPE phyics_type = HYDRA_PHYSICS_TYPE::HYDRA_INVALID_PHYSICS_TYPE;
HYDRA_PHYSICS_SHAPE physics_shape = HYDRA_PHYSICS_SHAPE::HYDRA_INVALID_PHYSICS_TYPE;
float mass = 0;
float height = 0;
float width = 0;
float depth = 0;
b3BodyId body_id = {};
bool initialised = false;
bool needs_update = false;
};
@@ -0,0 +1,98 @@
#include "HydraPhysicsSystem.h"
#include "../../engine/HydraEngine.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/physics/HydraInitialisePhysicsWorldTask.hpp"
#include "../../task/physics/HydraDestroyPhysicsWorldTask.hpp"
#include "../../task/physics/HydraCreatePhysicsObjectTask.hpp"
#include "../../task/physics/HydraSetPhysicsTransform.hpp"
#include "../../task/physics/HydraGetPhysicsUpdatesTask.hpp"
#include "../../task/physics/HydraStepPhysicsTask.h"
#include "../../ecs/HydraECS.h"
#include "../../ecs/components/HydraPhysicsComponent.h"
#include "../../ecs/components/HydraPositionComponent.h"
void HydraPhysicsSystem::Initialise()
{
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID task_id = task_scheduler->CreateTask<HydraInitialisePhysicsWorldTask>(HydraThreadAffinity::Physics);
HydraInitialisePhysicsWorldTask *task = static_cast<HydraInitialisePhysicsWorldTask *>(task_scheduler->GetTask(task_id));
task->Initialise(&m_world_id);
task_scheduler->WaitForTask(task_id);
}
void HydraPhysicsSystem::Shutdown()
{
//TODO destroy all objects here then
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID task_id = task_scheduler->CreateTask<HydraDestroyPhysicsWorldTask>(HydraThreadAffinity::Physics);
HydraDestroyPhysicsWorldTask *task = static_cast<HydraDestroyPhysicsWorldTask *>(task_scheduler->GetTask(task_id));
task->Initialise(m_world_id);
task_scheduler->WaitForTask(task_id);
}
void HydraPhysicsSystem::SetTransform(HydraID entity_id)
{
HydraPhysicsComponent &phys_comp = GetEngine()->ECS()->GetComponent<HydraPhysicsComponent>(entity_id);
if(!phys_comp.needs_update)
{
m_bodies_to_set.push(entity_id);
phys_comp.needs_update = true;
}
}
void HydraPhysicsSystem::InitialiseComponent(HydraID entity_id)
{
b3BodyId body_id = {};
HydraPhysicsComponent& phys_comp = GetEngine()->ECS()->GetComponent<HydraPhysicsComponent>(entity_id);
HydraPositionComponent& pos_comp = GetEngine()->ECS()->GetComponent<HydraPositionComponent>(entity_id);
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID task_id = task_scheduler->CreateTask<HydraCreatePhysicsObjectTask>(HydraThreadAffinity::Physics);
HydraCreatePhysicsObjectTask *task = static_cast<HydraCreatePhysicsObjectTask *>(task_scheduler->GetTask(task_id));
task->Initialise(m_world_id, phys_comp, &body_id, entity_id);
task_scheduler->WaitForTask(task_id);
phys_comp.body_id = body_id;
task_id = task_scheduler->CreateTask<HydraSetPhysicsTransform>(HydraThreadAffinity::Physics);
HydraSetPhysicsTransform *set_task = static_cast<HydraSetPhysicsTransform *>(task_scheduler->GetTask(task_id));
set_task->Initialise(body_id, pos_comp.position, glm::quat(pos_comp.orientation));
task_scheduler->WaitForTask(task_id);
phys_comp.initialised = true;
}
void HydraPhysicsSystem::DestroyComponent(HydraID entity_id)
{
}
void HydraPhysicsSystem::UpdateSystem(float delta_t_seconds)
{
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
while(m_bodies_to_set.size() >0)
{
HydraID entity_id = m_bodies_to_set.front();
m_bodies_to_set.pop();
HydraPhysicsComponent& phys_comp = GetEngine()->ECS()->GetComponent<HydraPhysicsComponent>(entity_id);
HydraPositionComponent& pos_comp = GetEngine()->ECS()->GetComponent<HydraPositionComponent>(entity_id);
HydraID task_id = task_scheduler->CreateTask<HydraSetPhysicsTransform>(HydraThreadAffinity::Physics);
HydraSetPhysicsTransform *set_task = static_cast<HydraSetPhysicsTransform *>(task_scheduler->GetTask(task_id));
set_task->Initialise(phys_comp.body_id, pos_comp.position, glm::quat(pos_comp.orientation));
task_scheduler->WaitForTask(task_id);
}
}
void HydraPhysicsSystem::StepPhysics(float delta_t)
{
HydraID physics_step_task_id = GetEngine()->TaskScheduler()->CreateTask<HydraStepPhysicsTask>(HydraThreadAffinity::Physics);
HydraStepPhysicsTask* physics_task = static_cast<HydraStepPhysicsTask*>(GetEngine()->TaskScheduler()->GetTask(physics_step_task_id));
physics_task->Initialise(m_world_id, delta_t);
GetEngine()->TaskScheduler()->WaitForTask(physics_step_task_id);
}
@@ -0,0 +1,29 @@
#ifndef HYDRAPHYSICSSYSTEM
#define HYDRAPHYSICSSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include <GLMIncludes.h>
#include <queue>
#include <box3d/box3d.h>
class HydraPhysicsSystem : public HydraSystem
{
public:
void Initialise() override;
void Shutdown() override;
void SetTransform(HydraID entity_id);
void StepPhysics(float delta_t);
protected:
void InitialiseComponent(HydraID entity_id) override;
void DestroyComponent(HydraID entity_id) override;
virtual void UpdateSystem(float delta_t_seconds);
private:
b3WorldId m_world_id;
std::queue<HydraID> m_bodies_to_set;
};
#endif /* HYDRAPHYSICSSYSTEM */
@@ -1,20 +1,31 @@
#include "HydraTransformSystem.h"
#include "../../engine/HydraEngine.h"
#include "../HydraECS.h"
#include "HydraPhysicsSystem.h"
#include "../components/HydraPositionComponent.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraDeleteGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp"
#include "../../task/physics/HydraSetPhysicsTransform.hpp"
#include "../../actor/HydraActorManager.h"
#include "../components/HydraPhysicsComponent.h"
void HydraTransformSystem::UpdateTransform(HydraID actor_id)
void HydraTransformSystem::UpdateTransform(HydraID actor_id, bool ignore_physics)
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
pos_comp.needs_update = true;
m_entities_to_update.push(actor_id);
pos_comp.needs_update = true;
m_entities_to_update.push(actor_id);
if (!ignore_physics)
{
if (ecs->HasComponent<HydraPhysicsComponent>(actor_id))
{
std::shared_ptr<HydraPhysicsSystem> phys_sys = GetEngine()->ECS()->GetSystem<HydraPhysicsSystem>();
phys_sys->SetTransform(actor_id);
}
}
}
void HydraTransformSystem::Initialise()
@@ -23,7 +34,7 @@ void HydraTransformSystem::Initialise()
HydraID create_gpu_buffer_task_id = task_scheduler->CreateTask<HydraCreateGPUBufferTask>(HydraThreadAffinity::Render);
HydraCreateGPUBufferTask *task = static_cast<HydraCreateGPUBufferTask *>(task_scheduler->GetTask(create_gpu_buffer_task_id));
task->Intialise("PositionsBuffer", 1, MAX_ACTORS * sizeof(glm::mat4),
&m_transform_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
&m_transform_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
m_model_matrix.resize(MAX_ACTORS);
}
@@ -35,22 +46,22 @@ void HydraTransformSystem::FreeSystemResources()
HydraID delete_gpu_buffer_task_id = task_scheduler->CreateTask<HydraDeleteGPUBufferTask>(HydraThreadAffinity::Render);
HydraDeleteGPUBufferTask *task = static_cast<HydraDeleteGPUBufferTask *>(task_scheduler->GetTask(delete_gpu_buffer_task_id));
task->Intialise(m_transform_buffer_id);
task_scheduler->WaitForTask(delete_gpu_buffer_task_id);
task_scheduler->WaitForTask(delete_gpu_buffer_task_id);
}
void HydraTransformSystem::UpdateSystem(float delta_t_seconds)
{
HydraECS *ecs = GetEngine()->ECS();
while(m_entities_to_update.size() > 0)
while (m_entities_to_update.size() > 0)
{
HydraID actor_id = m_entities_to_update.front();
m_entities_to_update.pop();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
if(pos_comp.needs_update)
if (pos_comp.needs_update)
{
HydraID parent_id = GetEngine()->ActorManager()->GetRoot(actor_id);
glm::mat4 identity = glm::mat4(1);
glm::mat4 identity = glm::mat4(1);
_UpdateChild(INVALID_HYDRA_ID, parent_id, identity);
}
}
@@ -83,10 +94,26 @@ void HydraTransformSystem::_UpdateChild(HydraID parent_id, HydraID actor_id, glm
glm::mat4 scale_matrix = glm::scale(pos_comp.scale);
glm::mat4 model_matrix = trans_matrix * rot_matrix * scale_matrix;
pos_comp.transform = trans * model_matrix;
pos_comp.transform = trans * model_matrix;
pos_comp.needs_update = false;
m_model_matrix[actor_id] = pos_comp.transform;
trans = pos_comp.transform;
if (ecs->HasComponent<HydraPhysicsComponent>(actor_id))
{
HydraPhysicsComponent phys_comp = ecs->GetComponent<HydraPhysicsComponent>(actor_id);
if (phys_comp.initialised)
{
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID task_id = task_scheduler->CreateTask<HydraSetPhysicsTransform>(HydraThreadAffinity::Render);
HydraSetPhysicsTransform *task = static_cast<HydraSetPhysicsTransform *>(task_scheduler->GetTask(task_id));
task->Initialise(phys_comp.body_id,
glm::vec3(pos_comp.position.x, pos_comp.position.y, pos_comp.position.z),
glm::quat(pos_comp.orientation));
task_scheduler->WaitForTask(task_id);
}
}
trans = pos_comp.transform;
}
for (uint32_t child_idx = 0; child_idx < child_ids.size(); child_idx++)
{
@@ -8,7 +8,7 @@ class HydraTransformSystem : public HydraSystem
{
public:
void UpdateTransform(HydraID actor_id);
void UpdateTransform(HydraID actor_id, bool ignore_physics = false);
void Initialise() override;
protected:
+40 -4
View File
@@ -24,6 +24,7 @@
#include "../ecs/systems/HydraDirectionalShadowSourceSystem.h"
#include "../ecs/systems/HydraSpotShadowSourceSystem.h"
#include "../ecs/systems/HydraPointShadowSourceSystem.h"
#include "../ecs/systems/HydraPhysicsSystem.h"
#include "../ecs/components/HydraPositionComponent.h"
#include "../ecs/components/HydraMeshComponent.h"
@@ -34,6 +35,7 @@
#include "../ecs/components/HydraDirectionalShadowSourceComponent.h"
#include "../ecs/components/HydraSpotShadowSourceComponent.h"
#include "../ecs/components/HydraPointShadowSourceComponent.h"
#include "../ecs/components/HydraPhysicsComponent.h"
#include "../camera/HydraCamera.h"
@@ -59,7 +61,7 @@
#include "../task/buffer/HydraInitialiseTextureManagerTask.hpp"
#include "../task/buffer/HydraInitialiseShadowBufferTask.hpp"
#include "../task/actor/HydraUpdatePlayerTask.h"
#include "../task/physics/HydraStepPhysicsTask.h"
#include "../task/renderer/HydraEndRenderTask.hpp"
#include "../mesh/HydraMeshManager.h"
@@ -197,6 +199,7 @@ void HydraEngine::_CreateManagers()
m_material_buffer_manager = new HydraMaterialBufferManager();
m_light_buffer_manager = new HydraLightBufferManager();
m_shadow_buffer_manager = new HydraShadowBufferManager();
}
void HydraEngine::_InitialiseManagers()
@@ -234,6 +237,7 @@ void HydraEngine::_InitialiseSystems()
m_ecs->RegisterComponentType<HydraDirectionalShadowSourceComponent>();
m_ecs->RegisterComponentType<HydraSpotShadowSourceComponent>();
m_ecs->RegisterComponentType<HydraPointShadowSourceComponent>();
m_ecs->RegisterComponentType<HydraPhysicsComponent>();
m_mesh_system = m_ecs->RegisterSystem<HydraMeshSystem>();
m_transform_system = m_ecs->RegisterSystem<HydraTransformSystem>();
@@ -243,7 +247,7 @@ void HydraEngine::_InitialiseSystems()
m_directional_shadow_system = m_ecs->RegisterSystem<HydraDirectionalShadowSourceSystem>();
m_spot_shadow_system = m_ecs->RegisterSystem<HydraSpotShadowSourceSystem>();
m_point_shadow_system = m_ecs->RegisterSystem<HydraPointShadowSourceSystem>();
m_physics_system = m_ecs->RegisterSystem<HydraPhysicsSystem>();
HydraSignature mesh_sig;
HydraSignature trans_sig;
@@ -253,6 +257,7 @@ void HydraEngine::_InitialiseSystems()
HydraSignature directional_shadow_sig;
HydraSignature spot_shadow_sig;
HydraSignature point_shadow_sig;
HydraSignature physics_sig;
mesh_sig.set(m_ecs->GetComponentType<HydraMeshComponent>(),true);
trans_sig.set(m_ecs->GetComponentType<HydraPositionComponent>(),true);
@@ -274,6 +279,9 @@ void HydraEngine::_InitialiseSystems()
point_shadow_sig.set(m_ecs->GetComponentType<HydraPositionComponent>(), true);
point_shadow_sig.set(m_ecs->GetComponentType<HydraPointShadowSourceComponent>(), true);
physics_sig.set(m_ecs->GetComponentType<HydraPhysicsComponent>(), true);
physics_sig.set(m_ecs->GetComponentType<HydraPositionComponent>(), true);
m_ecs->SetSystemSignature<HydraMeshSystem>(mesh_sig);
m_ecs->SetSystemSignature<HydraTransformSystem>(trans_sig);
m_ecs->SetSystemSignature<HydraRenderSystem>(render_sig);
@@ -282,11 +290,16 @@ void HydraEngine::_InitialiseSystems()
m_ecs->SetSystemSignature<HydraDirectionalShadowSourceSystem>(directional_shadow_sig);
m_ecs->SetSystemSignature<HydraSpotShadowSourceSystem>(spot_shadow_sig);
m_ecs->SetSystemSignature<HydraPointShadowSourceSystem>(point_shadow_sig);
m_ecs->SetSystemSignature<HydraPhysicsSystem>(physics_sig);
}
void HydraEngine::_UpdateSystems()
{
//Update physics system with any changes from the transform system
m_physics_system->Update(m_delta_t);
HydraID posUpdateBarrierID = m_task_scheduler->CreateBarrier();
HydraID update_trans_id = m_task_scheduler->CreateTask<HydraUpdateSystemTask>(HydraThreadAffinity::General, posUpdateBarrierID);
@@ -377,15 +390,16 @@ void HydraEngine::_GameLoop()
m_delta_t = diff.count();
float fixed_delta_t = fixed_diff.count();
if (fixed_delta_t > 0.016666666666f)
{
m_fixed_delta_t = fixed_diff.count();
last_fixed_tick = this_tick;
fixed_tick = true;
HydraID processInputID = m_task_scheduler->CreateTask<HydraProcessInputTask>(HydraThreadAffinity::Render, barrierID);
m_task_scheduler->StartTask(processInputID);
//Physics here
m_physics_system->StepPhysics(fixed_delta_t);
}
if (fpsFrameCount >= 15)
{
@@ -399,12 +413,24 @@ void HydraEngine::_GameLoop()
last_tick = this_tick;
fpsFrameCount++;
HydraID processInputID = m_task_scheduler->CreateTask<HydraProcessInputTask>(HydraThreadAffinity::Render, barrierID);
HydraProcessInputTask* m_input_task = static_cast<HydraProcessInputTask*>(m_task_scheduler->GetTask(processInputID));
m_input_task->Initialise(fixed_tick);
m_task_scheduler->StartTask(processInputID);
HydraID initialise_components_id = m_task_scheduler->CreateTask<HydraInitialiseComponentsTask>(HydraThreadAffinity::General, barrierID);
HydraID update_player_task_id = m_task_scheduler->CreateTask<HydraUpdatePlayerTask>(HydraThreadAffinity::General, barrierID);
m_task_scheduler->StartTask(initialise_components_id);
m_task_scheduler->StartTask(update_player_task_id);
m_task_scheduler->WaitForBarrier(barrierID);
if(fixed_tick)
{
m_game->FixedUpdate(m_delta_t);
}
m_game->Update(m_delta_t);
_UpdateSystems();
@@ -466,6 +492,16 @@ bool HydraEngine::KeyPressed(HYDRA_KEY_CODES key)
return m_input_manager->KeyPressed(key);
}
bool HydraEngine::KeyUp(HYDRA_KEY_CODES key)
{
return m_input_manager->KeyUp(key);
}
bool HydraEngine::KeyDown(HYDRA_KEY_CODES key)
{
return m_input_manager->KeyDown(key);
}
glm::vec2 HydraEngine::MouseChange()
{
return m_input_manager->MouseChange();
+5
View File
@@ -29,6 +29,7 @@ class HydraDirectionalShadowSourceSystem;
class HydraSpotShadowSourceSystem;
class HydraShadowBufferManager;
class HydraPointShadowSourceSystem;
class HydraPhysicsSystem;
class CLASS_DEFINE HydraEngine : public HydraObject
{
@@ -43,6 +44,8 @@ public:
void Start();
bool KeyPressed(HYDRA_KEY_CODES key);
bool KeyUp(HYDRA_KEY_CODES key);
bool KeyDown(HYDRA_KEY_CODES key);
glm::vec2 MouseChange();
void Exit();
@@ -92,6 +95,7 @@ private:
HydraMaterialBufferManager* m_material_buffer_manager = nullptr;
HydraLightBufferManager* m_light_buffer_manager = nullptr;
HydraShadowBufferManager* m_shadow_buffer_manager = nullptr;
std::shared_ptr<HydraMeshSystem> m_mesh_system;
std::shared_ptr<HydraTransformSystem> m_transform_system;
std::shared_ptr<HydraRenderSystem> m_render_system;
@@ -100,6 +104,7 @@ private:
std::shared_ptr<HydraDirectionalShadowSourceSystem> m_directional_shadow_system;
std::shared_ptr<HydraSpotShadowSourceSystem> m_spot_shadow_system;
std::shared_ptr<HydraPointShadowSourceSystem> m_point_shadow_system;
std::shared_ptr<HydraPhysicsSystem> m_physics_system;
bool m_running = true;
float m_delta_t = 0.0f;
@@ -1,12 +1,16 @@
#include "HydraInputManager.h"
#include "../windowmanager/HydraWindowManager.h"
void HydraInputManager::ProcessInput()
void HydraInputManager::ProcessInput(bool reset)
{
bool quit = false;
m_mouse_change = glm::vec2();
m_key_down.clear();
m_key_up.clear();
if(reset)
{
std::fill(m_key_down, m_key_down+512, 0);
std::fill(m_key_up, m_key_up+512, 0);
}
while (!quit)
{
HydraKeyPressStuct key_press;
@@ -18,13 +22,20 @@ void HydraInputManager::ProcessInput()
{
if (key_press.key_action == KEY_PRESS_ACTIONS::HYDRA_KEY_PRESSED)
{
m_keys[key_press.key_code] = 1;
m_key_up.push_back(key_press.key_code);
if(m_keys[key_press.key_code] == 0)
{
m_key_down[key_press.key_code] = 1;
}
m_keys[key_press.key_code] = 1;
}
else
{
{
if(m_keys[key_press.key_code] == 1)
{
m_key_up[key_press.key_code] = 1;
}
m_keys[key_press.key_code] = 0;
m_key_down.push_back(key_press.key_code);
}
}
if (m_key_presses.size() > 10000)
@@ -78,5 +89,15 @@ void HydraInputManager::ProcessInput()
bool HydraInputManager::KeyPressed(HYDRA_KEY_CODES key)
{
return m_keys[(unsigned int)key] == 1;
}
return m_keys[static_cast<uint32_t>(key)] == 1;
}
bool HydraInputManager::KeyDown(HYDRA_KEY_CODES key)
{
return m_key_down[static_cast<uint32_t>(key)] == 1;
}
bool HydraInputManager::KeyUp(HYDRA_KEY_CODES key)
{
return m_key_up[static_cast<uint32_t>(key)] == 1;
}
@@ -9,12 +9,13 @@ class HydraInputManager : public HydraObject
{
private:
int m_keys[512] = { };
std::vector<int> m_key_down;
std::vector<int> m_key_up;
int m_key_down[512] = {};
int m_key_up[512] = {};
glm::vec2 m_last_mouse = glm::vec2(0, 0);
glm::vec2 m_mouse_change = glm::vec2(0, 0);
glm::vec2 m_mouse_pos = glm::vec2(0, 0);
std::vector<HydraKeyPressStuct> m_key_presses;
std::queue<glm::vec2> m_mouse_moves;
bool m_mouse_reset = true;
@@ -22,8 +23,10 @@ class HydraInputManager : public HydraObject
friend class HydraEngine;
public:
void ProcessInput();
void ProcessInput(bool reset);
bool KeyPressed(HYDRA_KEY_CODES key);
bool KeyDown(HYDRA_KEY_CODES key);
bool KeyUp(HYDRA_KEY_CODES key);
glm::vec2 MouseChange() {return m_mouse_change;}
void Shutdown() {}
};
+1 -1
View File
@@ -9,7 +9,7 @@ class CLASS_DEFINE HydraGameProxy : public HydraObject
public:
virtual bool Initialise() = 0;
virtual void Update(float delta_t_seconds) {};
virtual void FixedUpdate(float delta_t_seconds) {};
};
@@ -31,7 +31,7 @@ void HydraDeferredPipeline::Initialise()
_CreateFrameBuffer();
_CreateColourRenderStage();
_CreateOutputRenderStage();
_CreateTextureDebugOutputStage();
//_CreateTextureDebugOutputStage();
}
void HydraDeferredPipeline::Shutdown()
@@ -0,0 +1,74 @@
#ifndef HYDRACREATEPHYSICSOBJECTTASK
#define HYDRACREATEPHYSICSOBJECTTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../ecs/components/HydraPhysicsComponent.h"
#include <box3d/box3d.h>
class HydraCreatePhysicsObjectTask : public HydraTask
{
private:
b3WorldId m_world_id = {};
b3BodyId *m_body_id = {};
HydraID m_entity_id = INVALID_HYDRA_ID;
HydraPhysicsComponent m_physics_comp;
protected:
public:
void Process() override
{
b3BodyDef bodyDef = b3DefaultBodyDef();
switch(m_physics_comp.phyics_type)
{
case HYDRA_PHYSICS_TYPE::HYDRA_RIGID_BODY:
{
bodyDef.type = b3_dynamicBody;
break;
}
case HYDRA_PHYSICS_TYPE::HYDRA_STATIC_BODY:
{
bodyDef.type = b3_staticBody;
break;
}
default:
{
bodyDef.type = b3_dynamicBody;
break;
}
}
HydraID* user_entity_id = new HydraID();
*user_entity_id = m_entity_id;
bodyDef.isEnabled = true;
bodyDef.name = std::to_string(m_entity_id).c_str();
bodyDef.userData = user_entity_id;
*m_body_id = b3CreateBody( m_world_id, &bodyDef );
b3BoxHull box = b3MakeBoxHull(m_physics_comp.width/2.0f, m_physics_comp.height/2.0f, m_physics_comp.depth/2.0f);
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3Pos pos = {};
shapeDef.density = 1.0f;
shapeDef.baseMaterial.restitution = 0.4f;
b3CreateHullShape(*m_body_id, &shapeDef, &box.base );
}
void Initialise(b3WorldId world_id, HydraPhysicsComponent physics_comp, b3BodyId* body_id, HydraID entity_id)
{
m_body_id = body_id;
m_world_id = world_id;
m_entity_id = entity_id;
m_physics_comp = physics_comp;
}
};
#endif /* HYDRACREATEPHYSICSOBJECTTASK */
@@ -0,0 +1,31 @@
#ifndef HYDRADELETEPHYSICSOBJECTTASK
#define HYDRADELETEPHYSICSOBJECTTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../ecs/components/HydraPhysicsComponent.h"
#include <box3d/box3d.h>
class HydraDeletePhysicsObjectTask : public HydraTask
{
private:
b3BodyId m_body_id = {};
protected:
public:
void Process() override
{
b3DestroyBody(m_body_id);
}
void Initialise(b3BodyId body_id)
{
m_body_id = body_id;
}
};
#endif /* HYDRADELETEPHYSICSOBJECTTASK */
@@ -0,0 +1,31 @@
#ifndef HYDRADESTROYPHYSICSWORLDTASK
#define HYDRADESTROYPHYSICSWORLDTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include <box3d/box3d.h>
class HydraDestroyPhysicsWorldTask : public HydraTask
{
private:
b3WorldId m_world_id = {};
protected:
public:
void Process() override
{
b3DestroyWorld(m_world_id);
}
void Initialise(b3WorldId world_id)
{
m_world_id = world_id;
}
};
#endif /* HYDRADESTROYPHYSICSWORLDTASK */
@@ -0,0 +1,52 @@
#ifndef HYDRAGETPHYSICSUPDATESTASK
#define HYDRAGETPHYSICSUPDATESTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../scheduler/HydraTaskBarrier.h"
#include "HydraSetWorldTransformTask.hpp"
#include <GLMIncludes.h>
#include <box3d/box3d.h>
class HydraGetPhysicsUpdatesTask : public HydraTask
{
private:
b3WorldId m_world_id = {};
protected:
public:
void Process() override
{
b3BodyEvents events = b3World_GetBodyEvents(m_world_id);
HydraTaskScheduler* task_scheduler = GetEngine()->TaskScheduler();
HydraID barrier_id = task_scheduler->CreateBarrier();
for(uint32_t i = 0; i < events.moveCount; i ++)
{
HydraID task_id = task_scheduler->CreateTask<HydraSetWorldTransformTask>(HydraThreadAffinity::General, barrier_id);
HydraSetWorldTransformTask *set_task = static_cast<HydraSetWorldTransformTask *>(task_scheduler->GetTask(task_id));
glm::dvec3 position = {events.moveEvents[i].transform.p.x, events.moveEvents[i].transform.p.y, events.moveEvents[i].transform.p.z};
glm::quat temp = {events.moveEvents[i].transform.q.v.x, events.moveEvents[i].transform.q.v.y, events.moveEvents[i].transform.q.v.z, events.moveEvents[i].transform.q.s};
glm::vec3 rotation = glm::eulerAngles(temp);
HydraID* entity_id = reinterpret_cast<HydraID*>(events.moveEvents[i].userData);
set_task->Initialise(*entity_id, position, rotation);
task_scheduler->StartTask(task_id);
}
task_scheduler->WaitForBarrier(barrier_id);
}
void Initialise(b3WorldId world_id)
{
m_world_id = world_id;
}
};
#endif /* HYDRAGETPHYSICSUPDATESTASK */
@@ -0,0 +1,41 @@
#ifndef HYDRAINITIALISEPHYSICSWORLDTASK
#define HYDRAINITIALISEPHYSICSWORLDTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include <box3d/box3d.h>
class HydraInitialisePhysicsWorldTask : public HydraTask
{
private:
b3WorldId* m_world_id = nullptr;
protected:
public:
void Process() override
{
b3WorldDef world_def = b3DefaultWorldDef();
world_def.workerCount = 1;
world_def.enableSleep = true;
*m_world_id = b3CreateWorld( &world_def );
b3World_SetContactRecycleDistance(*m_world_id, 0.05f);
}
void Initialise(b3WorldId *world_id)
{
m_world_id = world_id;
}
};
#endif /* HYDRAINITIALISEPHYSICSWORLDTASK */
@@ -0,0 +1,38 @@
#ifndef HYDRASETPHYSICSTRANSFORM
#define HYDRASETPHYSICSTRANSFORM
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include <box3d/box3d.h>
class HydraSetPhysicsTransform : public HydraTask
{
private:
b3BodyId m_body_id = {};
glm::vec3 m_position = {};
glm::quat m_rotation = {};
protected:
public:
void Process() override
{
b3Vec3 position = {m_position.x, m_position.y, m_position.z};
b3Quat rotation = {m_rotation.x, m_rotation.y, m_rotation.z, m_rotation.w};
b3Body_SetTransform(m_body_id, position, rotation);
}
void Initialise(b3BodyId body_id, glm::vec3 position, glm::quat rotation)
{
m_body_id = body_id;
m_position = position;
m_rotation = rotation;
}
};
#endif /* HYDRASETPHYSICSTRANSFORM */
@@ -0,0 +1,35 @@
#ifndef HYDRASETWORLDTRANSFORMTASK
#define HYDRASETWORLDTRANSFORMTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../actor/HydraActorManager.h"
#include <box3d/box3d.h>
class HydraSetWorldTransformTask : public HydraTask
{
private:
HydraID m_entity_id = INVALID_HYDRA_ID;
glm::dvec3 m_position = {};
glm::vec3 m_rotation = {};
protected:
public:
void Process() override
{
GetEngine()->ActorManager()->SetPositionAndOrientation(m_entity_id, m_position, m_rotation, true);
}
void Initialise(HydraID entity_id, glm::dvec3 position, glm::vec3 rotation)
{
m_entity_id = entity_id;
m_position = position;
m_rotation = rotation;
}
};
#endif /* HYDRASETWORLDTRANSFORMTASK */
@@ -0,0 +1,50 @@
#ifndef HYDRASTEPPHYSICSTASK
#define HYDRASTEPPHYSICSTASK
#include "../HydraTask.h"
#include "../../engine/HydraEngine.h"
#include "../../renderer/HydraRenderer.h"
#include <GLMIncludes.h>
#include "../../GlewIncludes.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../actor/HydraActorManager.h"
#include "HydraSetWorldTransformTask.hpp"
#include <box3d/box3d.h>
class HydraStepPhysicsTask : public HydraTask
{
private:
b3WorldId m_world_id;
float m_delta_t_seconds= 0;
protected:
public:
void Process() override
{
b3World_Step(m_world_id, m_delta_t_seconds, 1);
b3BodyEvents events = b3World_GetBodyEvents(m_world_id);
HydraTaskScheduler* task_scheduler = GetEngine()->TaskScheduler();
HydraID barrier_id = task_scheduler->CreateBarrier();
for(uint32_t i = 0; i < events.moveCount; i ++)
{
HydraID task_id = task_scheduler->CreateTask<HydraSetWorldTransformTask>(HydraThreadAffinity::General, barrier_id);
HydraSetWorldTransformTask *set_task = static_cast<HydraSetWorldTransformTask *>(task_scheduler->GetTask(task_id));
glm::dvec3 position = {events.moveEvents[i].transform.p.x, events.moveEvents[i].transform.p.y, events.moveEvents[i].transform.p.z};
glm::quat temp = glm::quat(events.moveEvents[i].transform.q.s, glm::vec3(events.moveEvents[i].transform.q.v.x, events.moveEvents[i].transform.q.v.y, events.moveEvents[i].transform.q.v.z));
glm::vec3 rotation = glm::eulerAngles(temp);
HydraID* entity_id = reinterpret_cast<HydraID*>(events.moveEvents[i].userData);
set_task->Initialise(*entity_id, position, rotation);
task_scheduler->StartTask(task_id);
}
task_scheduler->WaitForBarrier(barrier_id);
}
void Initialise(b3WorldId world_id, float delta_t_seconds)
{
m_world_id= world_id;
m_delta_t_seconds = delta_t_seconds;
}
};
#endif /* HYDRASTEPPHYSICSTASK */
@@ -8,12 +8,17 @@
class HydraProcessInputTask : public HydraTask
{
private:
bool m_reset;
protected:
public:
virtual void Process() override
{
GetEngine()->WindowManager()->HandleMessages();
GetEngine()->InputManager()->ProcessInput();
GetEngine()->InputManager()->ProcessInput(m_reset);
}
void Initialise(bool reset)
{
m_reset = reset;
}
};
#endif /* HYDRAPROCESSINPUTTASK */