Physics stuff

This commit is contained in:
Confideo-IOM
2026-08-25 22:10:54 +01:00
parent 1253571c17
commit 30538ae7ab
59 changed files with 1403 additions and 518 deletions
+3 -4
View File
@@ -25,11 +25,10 @@ class CLASS_DEFINE HydraActor : public HydraObject
std::string Name;
virtual ~HydraActor(){};
virtual void InitialiseComponents() = 0;
virtual void DeferredInitialiseComponents() {};
HydraID GetID() {return m_id;}
virtual void FixedTick(float delta_t) {};
virtual void DeferredInitialiseComponents() {};
virtual void FixedTick(float delta_t) {};
virtual void _Cleanup(){};
HydraID GetID() {return m_id;}
};
+17 -14
View File
@@ -95,22 +95,15 @@ void HydraActorManager::Initialise(size_t max_actors)
}
void HydraActorManager::TickActors(float delta_t)
void HydraActorManager::FixedTickActor(float actor_id, float delta_t)
{
// std::lock_guard<std::mutex> lock(m_mutex);
HydraActor *actor = GetActor(actor_id);
if (actor != nullptr)
{
actor->FixedTick(delta_t);
}
// // HydraID barrierID = GetEngine()->TaskScheduler()->CreateBarrier();
// for (int i = 0; i < m_actors_to_tick_fixed.size(); i++)
// {
// HydraID tick_actors_task_id = GetEngine()->TaskScheduler()->CreateTask<HydraFixedTickActorTask>(HydraThreadAffinity::General);
// if (tick_actors_task_id != INVALID_HYDRA_ID)
// {
// ((HydraFixedTickActorTask *)(GetEngine()->TaskScheduler()->GetTask(tick_actors_task_id)))->Initialise(m_actors_to_tick_fixed[i], delta_t);
// GetEngine()->TaskScheduler()->StartTask(tick_actors_task_id);
// }
// }
// GetEngine()->TaskScheduler()->WaitForBarrier(barrierID);
}
void HydraActorManager::InitialiseActor(HydraID actorID)
{
@@ -188,6 +181,16 @@ void HydraActorManager::SetOrientation(HydraID actorID, float x, float y, float
}
}
void HydraActorManager::SetLinearVelocity(HydraID actor_id, glm::vec3 velocity)
{
HydraECS* ecs = GetEngine()->ECS();
if (ecs->HasComponent<HydraPhysicsComponent>(actor_id))
{
std::shared_ptr<HydraPhysicsSystem> phys_sys = ecs->GetSystem<HydraPhysicsSystem>();
phys_sys->SetLinearVelocity(actor_id, velocity);
}
}
void HydraActorManager::SetPositionAndOrientation(HydraID actorID, glm::dvec3 position, glm::vec3 euler_angles, bool ignore_physics)
{
HydraECS *ecs = GetEngine()->ECS();
+6 -5
View File
@@ -33,13 +33,15 @@ class CLASS_DEFINE HydraActorManager : public HydraObject
void Initialise(size_t max_actors);
template<class T> HydraID CreateActor();
HydraID LoadActor(std::string filename);
HydraID LoadActor(std::string filename);
void DeleteActor(HydraID actorID);
HydraActor* const GetActor(HydraID actorID);
std::vector<HydraID>& GetChildren(HydraID actor_id);
void TickActors(float delta_t);
void FixedTickActor(float actor_id, float delta_t);
void InitialiseActor(HydraID actorID);
void Shutdown();
@@ -48,15 +50,14 @@ class CLASS_DEFINE HydraActorManager : public HydraObject
void SetPosition(HydraID actorID, glm::dvec3 position);
void SetOrientation(HydraID actorID, float x, float y, float z);
void SetLinearVelocity(HydraID actor_id, glm::vec3 velocity);
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);
void SetRelationship(HydraID parent_id, HydraID child_id);
// void AddComponentType(HydraID actorID, uint32_t component_type);
// bool HasComponentType(HydraID actorID, uint32_t component_type);
// void RemoveComponentType(HydraID actorID, uint32_t component_type);
HydraID GetParent(HydraID actor_id);
HydraID GetRoot(HydraID actor_id);
+2 -2
View File
@@ -19,12 +19,12 @@ void HydraPlaneActor::InitialiseComponents()
// HydraECS* ecs = GetEngine()->ECS();
// HydraID entity_id = ecs->CreateEntity();
_CreatePosition();
// _CreatePhysics();
_CreatePhysics();
_CreateMaterial();
_CreateMesh();
_CreateRenderableComponent();
_CreatePhysics();
}
void HydraPlaneActor::DeferredInitialiseComponents()
@@ -0,0 +1,79 @@
#include "HydraLightSpawnerActor.h"
#include "../../ecs/components/HydraPositionComponent.h"
#include "../../ecs/components/HydraTickComponent.h"
#include "../../ecs/HydraECS.h"
#include "../../engine/HydraEngine.h"
#include "../../actor/HydraActorManager.h"
#include "HydraSpawnedLightActor.h"
void HydraLightSpawnerActor::FixedTick(float delta_t)
{
if(m_enabled)
{
std::chrono::time_point<std::chrono::steady_clock> current_tick = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(current_tick - m_last_tick);
long elapsed_milli_seconds = elapsed.count();
if(elapsed_milli_seconds >= (m_interval * 1000.0f))
{
HydraEngine* engine = GetEngine();
HydraPositionComponent pos_comp =engine->ECS()->GetComponent<HydraPositionComponent>(GetID());
HydraActorManager* actor_manager = engine->ActorManager();
HydraID actor_id = actor_manager->CreateActor<HydraSpawnedLightActor>();
actor_manager->InitialiseActor(actor_id);
actor_manager->SetPosition(actor_id, pos_comp.position);
actor_manager->SetLinearVelocity(actor_id, m_spawn_direction * m_spawn_velocity);
m_last_tick = current_tick;
}
}
}
void HydraLightSpawnerActor::_CreatePosition()
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent position_component;
ecs->AddComponent<HydraPositionComponent>(GetID(), position_component);
}
void HydraLightSpawnerActor::_CreateTick()
{
HydraECS *ecs = GetEngine()->ECS();
HydraTickComponent tick_component;
tick_component.entity_id = GetID();
tick_component.fixed = true;
ecs->AddComponent<HydraTickComponent>(GetID(), tick_component);
}
void HydraLightSpawnerActor::InitialiseComponents()
{
_CreatePosition();
_CreateTick();
}
void HydraLightSpawnerActor::SetEnabled(bool enabled)
{
m_enabled = enabled;
if(enabled == true)
{
m_last_tick = std::chrono::steady_clock::now();
}
}
void HydraLightSpawnerActor::SpawnSettings(float spawns_per_second,
glm::vec3 spawn_direction,
glm::vec3 dir_variation,
float spawn_velocity,
float velocity_variation,
float spawn_duration)
{
m_spawns_per_second = spawns_per_second;
m_spawn_direction = spawn_direction;
m_dir_variation = dir_variation;
m_spawn_velocity = spawn_velocity;
m_velocity_variation = velocity_variation;
m_spawn_duration = spawn_duration;
m_interval = 1.0f / spawns_per_second;
}
@@ -0,0 +1,37 @@
#ifndef HYDRALIGHTSPAWNERACTOR
#define HYDRALIGHTSPAWNERACTOR
#include "../HydraActor.h"
#include <chrono>
#include <queue>
#include <GLMIncludes.h>
class CLASS_DEFINE HydraLightSpawnerActor : public HydraActor
{
public:
void InitialiseComponents() override;
void SetEnabled(bool enabled);
void FixedTick(float delta_t);
void SpawnSettings(float spawns_per_second,
glm::vec3 spawn_direction,
glm::vec3 dir_variation,
float spawn_velocity,
float velocity_variation,
float spawn_duration);
private:
void _CreatePosition();
void _CreateTick();
float m_spawns_per_second =0;
glm::vec3 m_spawn_direction = {0.0f, 1.0f, 0.0f};
glm::vec3 m_dir_variation = {0.0f, 0.0f, 0.0f};
float m_spawn_velocity = 0.0f;
float m_velocity_variation = 0.0f;
float m_spawn_duration;
float m_interval = 0.0f;
bool m_enabled = false;
std::chrono::time_point<std::chrono::steady_clock> m_last_tick;
std::queue<HydraID> m_spawned_actors;
};
#endif /* HYDRALIGHTSPAWNERACTOR */
@@ -0,0 +1,203 @@
#include "HydraSpawnedLightActor.h"
#include "../../engine/HydraEngine.h"
#include "../../actor/HydraActorManager.h"
#include "../../task/HydraTask.h"
#include "../../ecs/HydraECS.h"
#include "../../ecs/components/HydraPositionComponent.h"
#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"
#include "../../mesh/HydraMeshManager.h"
#include "../../mesh/HydraMesh.h"
void HydraSpawnedLightActor::InitialiseComponents()
{
_CreatePosition();
_CreatePhysics();
_CreateMaterial();
_CreateMesh();
_CreateRenderableComponent();
}
void HydraSpawnedLightActor::DeferredInitialiseComponents()
{
}
void HydraSpawnedLightActor::_CreatePosition()
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent position_component;
ecs->AddComponent<HydraPositionComponent>(GetID(), position_component);
}
void HydraSpawnedLightActor::_CreateMesh()
{
HydraMeshManager *mesh_manager = GetEngine()->MeshManager();
HydraID mesh_id = mesh_manager->CreateMesh();
HydraMesh *mesh = mesh_manager->GetMesh(mesh_id);
HydraMaterialComponent mat_comp = GetEngine()->ECS()->GetComponent<HydraMaterialComponent>(GetID());
mesh->vertexes.resize(24);
mesh->indexes.resize(36);
float scale_x = 0.3f;
float scale_y = 0.3f;
float scale_z = 0.3f;
float min_x = -0.5f * scale_x;
float min_y = -0.5f * scale_y;
float min_z = -0.5f * scale_z;
float max_x = 0.5f * scale_x;
float max_y = 0.5f * scale_y;
float max_z = 0.5f * scale_z;
float min_u = 1;
float min_v = 1;
float max_u = 0;
float max_v = 0;
// 1---2 5---6
//| / | | / |
// 0---3 4---7
HydraID actor_id = GetID();
mesh->vertexes[0] = {min_x, min_y, min_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[1] = {min_x, max_y, min_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[2] = {max_x, max_y, min_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[3] = {max_x, min_y, min_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
// Face 0
mesh->indexes[0] = 0;
mesh->indexes[1] = 1;
mesh->indexes[2] = 2;
mesh->indexes[3] = 0;
mesh->indexes[4] = 2;
mesh->indexes[5] = 3;
mesh->vertexes[4] = {max_x, min_y, min_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[5] = {max_x, max_y, min_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[6] = {max_x, max_y, max_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[7] = {max_x, min_y, max_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->indexes[6] = 4;
mesh->indexes[7] = 5;
mesh->indexes[8] = 6;
mesh->indexes[9] = 4;
mesh->indexes[10] = 6;
mesh->indexes[11] = 7;
// Back Face
mesh->vertexes[8] = {max_x, min_y, max_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[9] = {max_x, max_y, max_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[10] = {min_x, max_y, max_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[11] = {min_x, min_y, max_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->indexes[12] = 8;
mesh->indexes[13] = 9;
mesh->indexes[14] = 10;
mesh->indexes[15] = 8;
mesh->indexes[16] = 10;
mesh->indexes[17] = 11;
mesh->vertexes[12] = {min_x, min_y, max_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[13] = {min_x, max_y, max_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[14] = {min_x, max_y, min_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[15] = {min_x, min_y, min_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->indexes[18] = 12;
mesh->indexes[19] = 13;
mesh->indexes[20] = 14;
mesh->indexes[21] = 12;
mesh->indexes[22] = 14;
mesh->indexes[23] = 15;
mesh->vertexes[16] = {min_x, max_y, min_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[17] = {min_x, max_y, max_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[18] = {max_x, max_y, max_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[19] = {max_x, max_y, min_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->indexes[24] = 16;
mesh->indexes[25] = 17;
mesh->indexes[26] = 18;
mesh->indexes[27] = 16;
mesh->indexes[28] = 18;
mesh->indexes[29] = 19;
mesh->vertexes[20] = {min_x, min_y, min_z, min_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[21] = {max_x, min_y, min_z, min_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[22] = {max_x, min_y, max_z, max_u, max_v, actor_id, mat_comp.material_id, 0};
mesh->vertexes[23] = {min_x, min_y, max_z, max_u, min_v, actor_id, mat_comp.material_id, 0};
mesh->indexes[30] = 20;
mesh->indexes[31] = 21;
mesh->indexes[32] = 22;
mesh->indexes[33] = 20;
mesh->indexes[34] = 22;
mesh->indexes[35] = 23;
mesh_manager->CalculateNormals(mesh->mesh_id);
HydraECS *ecs = GetEngine()->ECS();
HydraMeshComponent mesh_comp;
mesh_comp.mesh_id = mesh_id;
mesh_comp.mesh_state = HYDRA_STATE::Waiting;
ecs->AddComponent<HydraMeshComponent>(GetID(), mesh_comp);
}
void HydraSpawnedLightActor::_CreateMaterial()
{
HydraEngine *engine = GetEngine();
std::string filename = std::string(texture_base) + std::string("/DesignArea/DesignArea_Albedo.png");
HydraID tex_id = engine->TextureBufferManager()->LoadTexture(filename);
HydraMaterialBuffer material = {};
material.diffuse_texture_id = tex_id;
material.diffuse_colour = glm::vec4(1, 1, 1, 1);
material.metalic_rough = glm::vec4(0, 0.5, 0.3, 1);
HydraID mat_id = engine->MaterialBufferManager()->CreateMaterial(material);
HydraMaterialComponent mat_comp = {};
mat_comp.material_id = mat_id;
engine->ECS()->AddComponent<HydraMaterialComponent>(GetID(), mat_comp);
}
void HydraSpawnedLightActor::_CreatePhysics()
{
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;
phys_comp.depth = 0.3f;
phys_comp.height = 0.3f;
phys_comp.width = 0.3f;
engine->ECS()->AddComponent<HydraPhysicsComponent>(GetID(), phys_comp);
}
void HydraSpawnedLightActor::_CreateRenderableComponent()
{
HydraRenderableComponent render_comp;
HydraECS *ecs = GetEngine()->ECS();
render_comp.renderable_types.set((uint32_t)HYDRA_RENDERABLE_TYPE::HYDRA_COLOUR, true);
ecs->AddComponent<HydraRenderableComponent>(GetID(), render_comp);
}
@@ -0,0 +1,26 @@
#ifndef HYDRASPAWNEDLIGHTACTOR
#define HYDRASPAWNEDLIGHTACTOR
#include "../HydraActor.h"
class CLASS_DEFINE HydraSpawnedLightActor : public HydraActor
{
private:
double m_scale = 1.0;
double m_widthscale = 1.0f;
HydraID m_material_component = INVALID_HYDRA_ID;
HydraID m_position_component = INVALID_HYDRA_ID;
HydraID m_mesh_component = INVALID_HYDRA_ID;
protected:
void _CreatePosition();
void _CreateMesh();
void _CreateMaterial();
void _CreatePhysics();
void _CreateRenderableComponent();
public:
void InitialiseComponents() override;
void DeferredInitialiseComponents() override;
};
#endif /* HYDRASPAWNEDLIGHTACTOR */