Techneck/code/source-c/world/world_physics.c

62 lines
2.4 KiB
C
Raw Normal View History

2023-10-17 14:37:19 +00:00
#include <world/world.h>
2023-10-15 12:41:53 +00:00
#include <stddef.h> // For NULL
#include <stdio.h>
2023-10-15 12:41:53 +00:00
void tc_initialize_chunk_physics(tc_entity_s *chunk_entity, void *userdata)
2023-10-15 12:41:53 +00:00
{
2023-10-17 14:37:19 +00:00
tc_chunk_s *chunk = chunk_entity->specific;
2023-10-17 11:25:02 +00:00
tc_world_s *world = chunk->location.world;
tc_physics_simulation_s *simulation = world->physics;
2023-10-15 12:41:53 +00:00
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);
2023-10-17 14:37:19 +00:00
}
void tc_free_chunk_physics(tc_entity_s *chunk_entity, void *userdata)
{
tc_physics_entity_s *body =
tc_get_pointer_from_entity(chunk_entity, "physics_body");
tc_remove_physics_entity(body);
2023-10-15 12:41:53 +00:00
}
void tc_create_world_physics(tc_world_s *world, void *userdata)
{
world->physics = tc_new_physics_simulation();
2023-10-17 11:25:02 +00:00
tc_add_to_hooklist(&world->on_chunk_create, (tc_fn_hook) tc_initialize_chunk_physics, NULL);
2023-10-17 14:37:19 +00:00
// tc_add_to_hooklist(&world->on_chunk_update, (tc_fn_hook) tc_initialize_chunk_physics, NULL);
tc_add_to_hooklist(&world->on_chunk_delete, (tc_fn_hook) tc_free_chunk_physics, NULL);
2023-10-15 12:41:53 +00:00
}