Implement broad-phase collision system

Changelog:
- Implement grid system
- Update screen bounce scene test
- Add grid movement update system
- Add component to track occupied grid tiles
scene_man
En Yi 2022-12-15 16:24:13 +08:00
parent 905f0d70a6
commit 03b602de40
5 changed files with 170 additions and 9 deletions

View File

@ -3,11 +3,12 @@
#include "raylib.h"
// TODO: Look at sc to use macros to auto generate functions
#define N_COMPONENTS 2
#define N_COMPONENTS 3
enum ComponentEnum
{
CBBOX_COMP_T,
CTRANSFORM_COMP_T,
CTILECOORD_COMP_T,
};
typedef enum ComponentEnum ComponentEnum_t;
@ -18,8 +19,19 @@ typedef struct _CBBox_t
typedef struct _CTransform_t
{
Vector2 prev_position;
Vector2 position;
Vector2 velocity;
Vector2 accel;
}CTransform_t;
// This is to store the occupying tiles
// Limits to store 4 tiles at a tile,
// Thus the size of the entity cannot be larger than the tile
typedef struct _CTileCoord_t
{
unsigned int tiles[4];
unsigned int n_tiles;
}CTileCoord_t;
#endif // __COMPONENTS_H

View File

@ -6,6 +6,7 @@
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE];
// Use hashmap as a Set
// Use list will be used to check if an object exist
@ -27,6 +28,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
{
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), {0}, {0}},
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), {0}, {0}},
{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), {0}, {0}},
};
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), {0}, {0}};

View File

@ -3,21 +3,62 @@
#include "raymath.h"
#include <stdio.h>
#define TILE_SIZE 32
#define MAX_N_TILES 4096
static Tile_t all_tiles[MAX_N_TILES] = {0};
static void level_scene_render_func(Scene_t* scene)
{
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
TileGrid_t tilemap = data->tilemap;
Entity_t *p_ent;
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
CTileCoord_t* p_tilecoord = get_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
for (size_t i=0;i<p_tilecoord->n_tiles;++i)
{
// Use previously store tile position
// Clear from those positions
int x = (p_tilecoord->tiles[i] % tilemap.width) * TILE_SIZE;
int y = (p_tilecoord->tiles[i] / tilemap.width) * TILE_SIZE;
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, GREEN);
}
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, RED);
}
//Draw tile grid
for (size_t i=0; i<tilemap.width;++i)
{
int x = (i+1)*TILE_SIZE;
DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK);
}
for (size_t i=0; i<tilemap.height;++i)
{
int y = (i+1)*TILE_SIZE;
DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK);
}
for (size_t i=0; i<tilemap.n_tiles;++i)
{
char buffer[6] = {0};
int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
DrawText(buffer, x, y, 10, BLACK);
}
return;
}
static void movement_update_system(Scene_t* scene)
{
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
// Deal with player acceleration/velocity via inputs first
float mag = Vector2Length(data->player_dir);
mag = (mag == 0)? 1 : mag;
Entity_t * p_player;
@ -30,6 +71,7 @@ static void movement_update_system(Scene_t* scene)
data->player_dir.x = 0;
data->player_dir.y = 0;
// Update movement
float delta_time = GetFrameTime();
CTransform_t * p_ctransform;
sc_map_foreach_value(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], p_ctransform)
@ -38,15 +80,100 @@ static void movement_update_system(Scene_t* scene)
p_ctransform->velocity,
Vector2Scale(p_ctransform->accel, delta_time)
);
// Store previous position before update
p_ctransform->prev_position.x = p_ctransform->position.x;
p_ctransform->prev_position.y = p_ctransform->position.y;
p_ctransform->position = Vector2Add(
p_ctransform->position,
Vector2Scale(p_ctransform->velocity, delta_time)
);
}
}
static void update_tilemap_system(Scene_t *scene)
{
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
TileGrid_t tilemap = data->tilemap;
Entity_t *p_ent;
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{
CTileCoord_t * p_tilecoord = get_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
if (p_tilecoord == NULL) continue;
CTransform_t * p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
if (p_ctransform == NULL) continue;
CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
if (p_bbox == NULL) continue;
// Update tilemap position
for (size_t i=0;i<p_tilecoord->n_tiles;++i)
{
// Use previously store tile position
// Clear from those positions
unsigned int tile_idx = p_tilecoord->tiles[i];
sc_map_del_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id);
}
p_tilecoord->n_tiles = 0;
// Compute new occupied tile positions and add
unsigned int tile_x = (p_ctransform->position.x) / TILE_SIZE;
unsigned int tile_y = (p_ctransform->position.y) / TILE_SIZE;
p_tilecoord->tiles[0] = tile_y * tilemap.width + tile_x;
p_tilecoord->n_tiles++;
bool check_x = (tile_x + 1) < tilemap.width;
bool check_y = (tile_y + 1) < tilemap.height;
if (check_x)
{
p_tilecoord->tiles[p_tilecoord->n_tiles++] = p_tilecoord->tiles[0] + 1;
}
if (check_y)
{
p_tilecoord->tiles[p_tilecoord->n_tiles++] = p_tilecoord->tiles[0] + tilemap.width;
}
if (check_x && check_y)
{
p_tilecoord->tiles[p_tilecoord->n_tiles++] = p_tilecoord->tiles[0] + tilemap.width + 1;
}
for (size_t i=0;i<p_tilecoord->n_tiles;++i)
{
unsigned int tile_idx = p_tilecoord->tiles[i];
sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, 0);
}
}
}
static void player_collision_system(Scene_t *scene)
{
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
TileGrid_t tilemap = data->tilemap;
Entity_t *p_player;
size_t tiles_to_check[8] = {0};
size_t n_tiles = 0;
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
{
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T);
// Get the occupied tiles
// For each tile, loop through the entities and find overlaps
// exclude self
// find also previous overlap
// If there is collision, use previous overlap to determine direction
// Resolve collision via moving player by the overlap amount
}
}
static void screen_bounce_system(Scene_t *scene)
{
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
TileGrid_t tilemap = data->tilemap;
Entity_t* p_ent;
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{
@ -54,17 +181,19 @@ static void screen_bounce_system(Scene_t *scene)
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
if (p_bbox == NULL || p_ctransform == NULL) continue;
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > 320)
unsigned int level_width = tilemap.width * TILE_SIZE;
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width)
{
p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x;
p_ctransform->position.x = (p_ctransform->position.x + p_bbox->size.x > 320) ? 320 - p_bbox->size.x : p_ctransform->position.x;
p_ctransform->position.x = (p_ctransform->position.x + p_bbox->size.x > level_width) ? level_width - p_bbox->size.x : p_ctransform->position.x;
p_ctransform->velocity.x *= -1;
}
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > 240)
unsigned int level_height = tilemap.height * TILE_SIZE;
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height)
{
p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y;
p_ctransform->position.y = (p_ctransform->position.y + p_bbox->size.y > 240) ? 240 - p_bbox->size.y : p_ctransform->position.y;
p_ctransform->position.y = (p_ctransform->position.y + p_bbox->size.y > level_height) ? level_height - p_bbox->size.y : p_ctransform->position.y;
p_ctransform->velocity.y *= -1;
}
}
@ -104,16 +233,32 @@ void init_level_scene(LevelScene_t *scene)
// insert level scene systems
sc_array_add(&scene->scene.systems, &movement_update_system);
sc_array_add(&scene->scene.systems, &screen_bounce_system);
sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_map_put_64(&scene->scene.action_map, KEY_UP, ACTION_UP);
sc_map_put_64(&scene->scene.action_map, KEY_DOWN, ACTION_DOWN);
sc_map_put_64(&scene->scene.action_map, KEY_LEFT, ACTION_LEFT);
sc_map_put_64(&scene->scene.action_map, KEY_RIGHT, ACTION_RIGHT);
scene->data.tilemap.width = 16;
scene->data.tilemap.height = 16;
scene->data.tilemap.n_tiles = 16*16;
scene->data.tilemap.tiles = all_tiles;
for (size_t i=0; i<MAX_N_TILES;i++)
{
all_tiles[i].solid = 0;
sc_map_init_64(&all_tiles[i].entities_set, 8, 0);
}
}
void free_level_scene(LevelScene_t *scene)
{
free_scene(&scene->scene);
for (size_t i=0; i<MAX_N_TILES;i++)
{
all_tiles[i].solid = 0;
sc_map_term_64(&all_tiles[i].entities_set);
}
}
void reload_level_scene(LevelScene_t *scene)

View File

@ -8,14 +8,15 @@
typedef struct Tile
{
bool solid;
// TODO: add a map?
struct sc_map_64 entities_set;
}Tile_t;
typedef struct TileGrid
{
unsigned int width;
unsigned int height;
Tile_t * const tiles;
unsigned int n_tiles;
Tile_t * tiles;
}TileGrid_t;
typedef struct LevelSceneData

View File

@ -9,7 +9,7 @@ struct sc_queue_32 key_buffer;
int main(void)
{
sc_queue_init(&key_buffer);
InitWindow(320, 240, "raylib");
InitWindow(640, 640, "raylib");
SetTargetFPS(60);
init_memory_pools();
LevelScene_t scene;
@ -20,6 +20,7 @@ int main(void)
p_bbox->size.x = 30;
p_bbox->size.y = 30;
add_component(&scene.scene.ent_manager, p_ent, CTRANSFORM_COMP_T);
add_component(&scene.scene.ent_manager, p_ent, CTILECOORD_COMP_T);
update_entity_manager(&scene.scene.ent_manager);
//for (size_t step = 0; step < 6000; step++)
while(true)