Now creating physics objects of all chunks

This commit is contained in:
Eric-Paul Ickhorn 2023-10-15 17:20:51 +02:00
parent efc84f691c
commit f96a688c55
9 changed files with 177 additions and 32 deletions

View File

@ -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) tc_entity_attribute_s * tc_get_entity_attribute_with_name(tc_entity_s *entity, char *name)
{ {
u32_t attribute_index = 0; 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]; tc_entity_attribute_s attribute = entity->attributes[attribute_index];
if(attribute.name == NULL) if(attribute.name == NULL)
{ {
puts("NAAJAA");
++attribute_index; ++attribute_index;
continue; continue;
} }
if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index]; if(!strcmp(attribute.name, name)) return &entity->attributes[attribute_index];
++attribute_index; ++attribute_index;
} }

View File

@ -8,7 +8,7 @@ tc_hooklist_s tc_new_hooklist(u32_t capacity)
tc_hooklist_s hooklist; tc_hooklist_s hooklist;
hooklist.capacity = capacity; hooklist.capacity = capacity;
hooklist.num_hooks = 0; hooklist.num_hooks = 0;
hooklist.hooks = malloc(sizeof(tc_fn_hook)); hooklist.hooks = malloc(sizeof(tc_hook_s));
return hooklist; return hooklist;
} }

View File

@ -1,4 +1,16 @@
#include "primitives.h" #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;
}

View File

@ -1,42 +1,117 @@
#include "simulation.h" #include "simulation.h"
tc_physics_simulation_s tc_new_physics_simulation() #include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
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; 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) void tc_tick_physics(tc_physics_simulation_s *simulation, f32_t delta)
{ {
} }

View File

@ -87,10 +87,24 @@ typedef struct tc_primitive_container
struct tc_physics_mesh struct tc_physics_mesh
{ {
u32_t primitives_capacity;
u32_t num_primitives; 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 #endif // TC_PHYSICS_PRIMITIVES_H

View File

@ -10,6 +10,7 @@
typedef struct tc_physics_entity tc_physics_entity_s; typedef struct tc_physics_entity tc_physics_entity_s;
typedef struct tc_entity_physics_attributes tc_entity_physics_attributes_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 struct tc_entity_physics_attributes
{ {
@ -31,6 +32,7 @@ typedef enum
struct tc_physics_entity struct tc_physics_entity
{ {
tc_physics_simulation_s *simulation;
tc_physics_entity_s *super; tc_physics_entity_s *super;
// relative_position: The position of this physics entity relative to its super-entity // relative_position: The position of this physics entity relative to its super-entity
@ -42,31 +44,35 @@ struct tc_physics_entity
struct entity struct entity
{ {
char *name; char *name;
tc_entity_s *entity; tc_physics_mesh_s mesh;
} entity; } entity;
struct subentity_group struct subentity_group
{ {
u32_t entities_capacity;
u32_t num_entities; u32_t num_entities;
tc_physics_entity_s *entities; tc_physics_entity_s **entities;
} group; } group;
} value; } 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 (); 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 (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); 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); void tc_tick_physics (tc_physics_simulation_s *simulation, f32_t delta);

View File

@ -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_y", 0);
tc_set_integer_for_entity(entity, "grid_z", 0); tc_set_integer_for_entity(entity, "grid_z", 0);
entity->specific = chunk; 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) 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) 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); tc_free_chunk(entity->specific);
} }

View File

@ -96,7 +96,7 @@ struct tc_world
tc_chunkloader_s *loaders; tc_chunkloader_s *loaders;
tc_chunkloader_s *spawn_loader; 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_create;
tc_hooklist_s on_chunk_generate; tc_hooklist_s on_chunk_generate;

View File

@ -1,9 +1,44 @@
#include <world.h> #include <world.h>
#include <stddef.h> // For NULL #include <stddef.h> // For NULL
#include <stdio.h>
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) void tc_create_world_physics(tc_world_s *world, void *userdata)