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
@@ -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 */