Techneck/code/source-c/physics/physics.c

126 lines
3.6 KiB
C
Raw Normal View History

2023-10-15 08:21:36 +00:00
#include "simulation.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
tc_physics_simulation_s * tc_new_physics_simulation()
2023-10-15 08:21:36 +00:00
{
tc_physics_simulation_s *simulation = malloc(sizeof(tc_physics_simulation_s));
simulation->root = tc_new_physics_group(simulation);
2023-10-15 08:21:36 +00:00
2023-10-15 12:41:53 +00:00
return simulation;
2023-10-15 08:21:36 +00:00
}
void tc_add_physics_object(tc_physics_simulation_s *simulation, tc_physics_entity_s *object)
2023-10-15 08:21:36 +00:00
{
tc_add_physics_object_to(simulation->root, object);
}
void tc_add_physics_object_to(tc_physics_entity_s *container, tc_physics_entity_s *object)
{
2023-10-17 14:37:19 +00:00
if(container != NULL) object->super = container;
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_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;
2023-10-15 08:21:36 +00:00
entity->value.entity.mesh.num_primitives = 0;
entity->value.entity.name = NULL;
2023-10-15 08:21:36 +00:00
return entity;
2023-10-15 08:21:36 +00:00
}
tc_physics_entity_s * tc_new_physics_group(tc_physics_simulation_s *simulation)
2023-10-15 08:21:36 +00:00
{
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;
2023-10-15 08:21:36 +00:00
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);
2023-10-15 08:21:36 +00:00
return entity;
2023-10-15 08:21:36 +00:00
}
i32_t tc_physics_find_entity_in_group(tc_physics_entity_s *group, tc_physics_entity_s *entity)
2023-10-15 08:21:36 +00:00
{
i32_t index = 0;
while(index < group->value.group.num_entities)
{
2023-10-17 14:37:19 +00:00
if(group->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)
{
if(entity->super == NULL) return;
2023-10-15 08:21:36 +00:00
2023-10-17 11:25:02 +00:00
// Fill up the gap created inside the entities-array.
2023-10-17 14:37:19 +00:00
tc_physics_entity_s *super = entity->super;
i32_t index = 0;
2023-10-17 14:37:19 +00:00
if((index = tc_physics_find_entity_in_group(super, entity)) == -1) return;
super->value.group.entities[index] =
super->value.group.entities[super->value.group.num_entities-1];
--super->value.group.num_entities;
2023-10-15 08:21:36 +00:00
2023-10-17 14:37:19 +00:00
tc_free_physics_entity(entity);
2023-10-15 08:21:36 +00:00
}
2023-10-15 12:41:53 +00:00
void tc_tick_physics(tc_physics_simulation_s *simulation, f32_t delta)
2023-10-15 08:21:36 +00:00
{
}