From f96a688c55e1857e5c52e5f1fe562a73eaaf62b6 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Sun, 15 Oct 2023 17:20:51 +0200 Subject: [PATCH] Now creating physics objects of all chunks --- code/source-c/entity.c | 4 +- code/source-c/hook.c | 2 +- code/source-c/physics/aabb.c | 14 +++- code/source-c/physics/physics.c | 103 +++++++++++++++++++++++++---- code/source-c/physics/primitives.h | 18 ++++- code/source-c/physics/simulation.h | 24 ++++--- code/source-c/world.c | 5 +- code/source-c/world.h | 2 +- code/source-c/world_physics.c | 37 ++++++++++- 9 files changed, 177 insertions(+), 32 deletions(-) diff --git a/code/source-c/entity.c b/code/source-c/entity.c index c021b49..2d9016c 100644 --- a/code/source-c/entity.c +++ b/code/source-c/entity.c @@ -137,15 +137,15 @@ void tc_delete_entity(tc_entity_s *entity) tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name) { u32_t attribute_index = 0; - while(attribute_index < entity->attributes_capacity) + while(attribute_index < entity->num_attributes) { tc_entity_attribute_s attribute = entity->attributes[attribute_index]; if(attribute.name == NULL) { + puts("NAAJAA"); ++attribute_index; continue; } - if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index]; ++attribute_index; } diff --git a/code/source-c/hook.c b/code/source-c/hook.c index c4e690d..932a19f 100644 --- a/code/source-c/hook.c +++ b/code/source-c/hook.c @@ -8,7 +8,7 @@ tc_hooklist_s tc_new_hooklist(u32_t capacity) tc_hooklist_s hooklist; hooklist.capacity = capacity; hooklist.num_hooks = 0; - hooklist.hooks = malloc(sizeof(tc_fn_hook)); + hooklist.hooks = malloc(sizeof(tc_hook_s)); return hooklist; } diff --git a/code/source-c/physics/aabb.c b/code/source-c/physics/aabb.c index 37d6584..c442c5a 100644 --- a/code/source-c/physics/aabb.c +++ b/code/source-c/physics/aabb.c @@ -1,4 +1,16 @@ #include "primitives.h" - +tc_physics_mesh_s tc_wrap_aabb(tc_vec3f_s start, tc_vec3f_s size) +{ + tc_aabb_s aabb; + aabb.start = start; + aabb.size = size; + + tc_physics_mesh_s mesh; + mesh.num_primitives = 1; + mesh.value.simple.primitive.type = TC_PRIMITIVE_AABB; + mesh.value.simple.primitive.data.aabb = aabb; + + return mesh; +} diff --git a/code/source-c/physics/physics.c b/code/source-c/physics/physics.c index 764e21c..958857b 100644 --- a/code/source-c/physics/physics.c +++ b/code/source-c/physics/physics.c @@ -1,42 +1,117 @@ #include "simulation.h" -tc_physics_simulation_s tc_new_physics_simulation() +#include +#include +#include + +tc_physics_simulation_s * tc_new_physics_simulation() { - tc_physics_simulation_s simulation; + tc_physics_simulation_s *simulation = malloc(sizeof(tc_physics_simulation_s)); + simulation->root = tc_new_physics_group(simulation); return simulation; } -void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity_s object) +void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity_s *object) { - - - + tc_add_physics_object_to(simulation->root, object); } -void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_s object) +void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_s *object) { - - - + if((container->value.group.num_entities+1) + >= + container->value.group.entities_capacity + ) { + container->value.group.entities_capacity *= 2; + container->value.group.entities = + realloc(container->value.group.entities, sizeof(tc_physics_entity_s *) * container->value.group.entities_capacity); + } + container->value.group.entities[container->value.group.num_entities] = object; + ++container->value.group.num_entities; } -tc_physics_entity_s tc_new_physics_entity() +tc_physics_entity_s * tc_new_physics_entity(tc_physics_simulation_s *simulation) { + tc_physics_entity_s *entity = malloc(sizeof(tc_physics_entity_s));; + entity->super = NULL; + entity->simulation = simulation; + entity->relative_position.x = 0.0f; + entity->relative_position.y = 0.0f; + entity->relative_position.z = 0.0f; + entity->type = TC_PHYSICS_ENTITY; + entity->value.entity.mesh.num_primitives = 0; + entity->value.entity.name = NULL; - + return entity; } +tc_physics_entity_s * tc_new_physics_group(tc_physics_simulation_s *simulation) +{ + tc_physics_entity_s *entity = malloc(sizeof(tc_physics_entity_s));; + entity->super = NULL; + entity->simulation = simulation; + entity->relative_position.x = 0.0f; + entity->relative_position.y = 0.0f; + entity->relative_position.z = 0.0f; + entity->type = TC_PHYSICS_ENTITY_GROUP; + + entity->value.group.entities_capacity = 8; + entity->value.group.num_entities = 0; + entity->value.group.entities = + calloc(sizeof(tc_physics_entity_s), entity->value.group.entities_capacity); + + return entity; +} + + + +i32_t tc_physics_find_entity_in_group(tc_physics_entity_s *group, tc_physics_entity_s *entity) +{ + i32_t index = 0; + while(index < group->value.group.num_entities) + { + if(entity->super->value.group.entities[group->value.group.num_entities] + == + entity + ) return index; + ++index; + } + return -1; +} + +void tc_free_physics_entity(tc_physics_entity_s *entity) +{ + if(entity->type == TC_PHYSICS_ENTITY_GROUP) + { + for(u32_t index = 0; index < entity->value.group.num_entities; ++index) + tc_free_physics_entity(entity->value.group.entities[index]); + } + free(entity); +} + +void tc_remove_physics_entity(tc_physics_entity_s *entity) +{ + u32_t num_entities = entity->simulation->root->value.group.num_entities; + if(entity->super == NULL) return; + + i32_t index = 0; + if((index = tc_physics_find_entity_in_group(entity->super, entity)) == -1) return; + + entity->super->value.group.entities[index] = + entity->super->value.group.entities[entity->super->value.group.num_entities-1]; +} + + + void tc_tick_physics(tc_physics_simulation_s *simulation, f32_t delta) { - - } diff --git a/code/source-c/physics/primitives.h b/code/source-c/physics/primitives.h index 62713b5..edc7d7f 100644 --- a/code/source-c/physics/primitives.h +++ b/code/source-c/physics/primitives.h @@ -87,10 +87,24 @@ typedef struct tc_primitive_container struct tc_physics_mesh { - u32_t primitives_capacity; u32_t num_primitives; - tc_primitive_s *primitives; + union + { + struct complex + { + u32_t primitives_capacity; + tc_primitive_s *primitives; + } complex; + struct simple + { + tc_primitive_s primitive; + + + } simple; + } value; }; +tc_physics_mesh_s tc_wrap_aabb (tc_vec3f_s start, tc_vec3f_s size); + #endif // TC_PHYSICS_PRIMITIVES_H diff --git a/code/source-c/physics/simulation.h b/code/source-c/physics/simulation.h index 2453140..76bd4c3 100644 --- a/code/source-c/physics/simulation.h +++ b/code/source-c/physics/simulation.h @@ -10,6 +10,7 @@ typedef struct tc_physics_entity tc_physics_entity_s; typedef struct tc_entity_physics_attributes tc_entity_physics_attributes_s; +typedef struct tc_physics_simulation tc_physics_simulation_s; struct tc_entity_physics_attributes { @@ -31,6 +32,7 @@ typedef enum struct tc_physics_entity { + tc_physics_simulation_s *simulation; tc_physics_entity_s *super; // relative_position: The position of this physics entity relative to its super-entity @@ -42,31 +44,35 @@ struct tc_physics_entity struct entity { char *name; - tc_entity_s *entity; + tc_physics_mesh_s mesh; } entity; struct subentity_group { + u32_t entities_capacity; u32_t num_entities; - tc_physics_entity_s *entities; + tc_physics_entity_s **entities; } group; } value; }; -typedef struct tc_physics_simulation +struct tc_physics_simulation { - tc_physics_entity_s world_root; + tc_physics_entity_s *root; -} tc_physics_simulation_s; +}; -tc_physics_simulation_s tc_new_physics_simulation (); -void tc_add_physics_object (tc_physics_simulation_s *simulation, tc_physics_entity_s object); -void tc_add_physics_object_to (tc_physics_entity_s *container, tc_physics_entity_s object); +tc_physics_simulation_s * tc_new_physics_simulation (); +void tc_add_physics_object (tc_physics_simulation_s *simulation, tc_physics_entity_s *object); +void tc_add_physics_object_to (tc_physics_entity_s *container, tc_physics_entity_s *object); -tc_physics_entity_s tc_new_physics_entity (); +tc_physics_entity_s * tc_new_physics_entity (tc_physics_simulation_s *simulation); +tc_physics_entity_s * tc_new_physics_group (tc_physics_simulation_s *simulation); + +void tc_remove_physics_entity (tc_physics_entity_s *entity); void tc_tick_physics (tc_physics_simulation_s *simulation, f32_t delta); diff --git a/code/source-c/world.c b/code/source-c/world.c index c9990da..3d3d59f 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -93,8 +93,9 @@ void tc_create_chunk_entity(tc_entity_s *entity, void *userdata) tc_set_integer_for_entity(entity, "grid_y", 0); tc_set_integer_for_entity(entity, "grid_z", 0); entity->specific = chunk; + chunk->world = world; - tc_run_hooklist(&world->on_chunk_create, chunk); + tc_run_hooklist(&world->on_chunk_create, entity); } void tc_spawn_chunk_entity(tc_entity_s *entity, tc_location_s location) @@ -115,6 +116,8 @@ void tc_teleport_chunk_entity(tc_entity_s *entity, tc_location_s location) void tc_delete_chunk_entity(tc_entity_s *entity) { + tc_physics_entity_s *physics_body = tc_get_pointer_from_entity(entity, "physics_body"); + tc_remove_physics_entity(physics_body); tc_free_chunk(entity->specific); } diff --git a/code/source-c/world.h b/code/source-c/world.h index 89ce97d..be82174 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -96,7 +96,7 @@ struct tc_world tc_chunkloader_s *loaders; tc_chunkloader_s *spawn_loader; - tc_physics_simulation_s physics; + tc_physics_simulation_s *physics; tc_hooklist_s on_chunk_create; tc_hooklist_s on_chunk_generate; diff --git a/code/source-c/world_physics.c b/code/source-c/world_physics.c index 2650e06..60a5574 100644 --- a/code/source-c/world_physics.c +++ b/code/source-c/world_physics.c @@ -1,9 +1,44 @@ #include #include // For NULL +#include -void tc_initialize_chunk_physics(tc_chunk_s *chunk, void *userdata) +void tc_initialize_chunk_physics(tc_entity_s *chunk_entity, void *userdata) { + tc_chunk_s *chunk = chunk_entity->specific; + tc_world_s *world = chunk->world; + tc_physics_simulation_s *simulation = world->physics; + tc_physics_entity_s *chunk_body = tc_new_physics_group(simulation); + + tc_vec3f_s block_start; + block_start.x = -0.5f; + block_start.y = -0.5f; + block_start.z = -0.5f; + + tc_vec3f_s block_size; + block_size.x = 1.0f; + block_size.y = 1.0f; + block_size.z = 1.0f; + + tc_physics_mesh_s block_mesh = tc_wrap_aabb(block_start, block_size); + + for(u32_t x = 0; x < 32; ++x) + for(u32_t y = 0; y < 32; ++y) + for(u32_t z = 0; z < 32; ++z) + { + tc_physics_entity_s *block_entity = tc_new_physics_entity(simulation); + block_entity->relative_position.x = x; + block_entity->relative_position.y = y; + block_entity->relative_position.z = z; + block_entity->type = TC_PHYSICS_ENTITY; + block_entity->value.entity.mesh = block_mesh; + + tc_add_physics_object_to(chunk_body, block_entity); + } + + + tc_add_physics_object(world->physics, chunk_body); + tc_set_pointer_for_entity(chunk_entity, "physics_body", chunk_body); } void tc_create_world_physics(tc_world_s *world, void *userdata)