xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 9a89b434ece1e2bd1d8d81afe0b5465cd4423f9f
parent ddfe16ce8fbc008780002313177298bd8befcdd7
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 30 Dec 2022 09:58:00 +0900

Change handle collision order so that closer object to the player
is handled first and distant object last.

Diffstat:
Mex7/ex7.c | 41+++++++++++++++++++++++++++++++++++++++--
Mex7/ex7.h | 14++++++++++++++
Mex7/physics.c | 8+++++---
3 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/ex7/ex7.c b/ex7/ex7.c @@ -54,6 +54,8 @@ void game_over(void); /* events */ void receive_events(enum key_state[]); void handle_inputs(enum key_state[]); +/* sort */ +void sort_blocks(int colliding_blocks[], int distance[], int count); void start_menu(void) @@ -129,7 +131,7 @@ game_play(void) block[bi].v.y = 0; block[bi].body.rectangle.w = BLOCK_SIZE; block[bi].body.rectangle.h = BLOCK_SIZE; - block[bi].m = block[bi].body.rectangle.w * + block[bi].m = block[bi].body.rectangle.w * block[bi].body.rectangle.h; bi++; } else if (world_map[i] == 'p') { @@ -181,8 +183,21 @@ game_play(void) for (int i = 0; i < NUM_BLOCK; i++) next_tick(&block[i], 1e9 / FPS / SUB_TICK); + int colliding_blocks[9]; + int distance[9]; + int j = 0; for (int i = 0; i < NUM_BLOCK; i++) - handle_collision_mf(&player, &block[i]); + if (test_collision(&player, &block[i])) { + colliding_blocks[j] = i; + distance[j] = (player.p.x - block[i].p.x) * + (player.p.x - block[i].p.x) + + (player.p.y - block[i].p.y) * + (player.p.y - block[i].p.y); + j++; + } + sort_blocks(colliding_blocks, distance, j); + for (int i = 0; i < j; i++) + handle_collision_mf(&player, &block[colliding_blocks[i]]); } // fix fps @@ -339,6 +354,28 @@ handle_inputs(enum key_state key_state[]) player.v.y = -450; } +void +swap(int list[], int i, int j) +{ + int tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +void +sort_blocks(int colliding_blocks[], int distance[], int count) +{ + for (int i = 0; i < count - 1; i++) { + for (int j = i + 1; j < count; j++) { + if (distance[i] > distance[j]) { + swap(colliding_blocks, i, j); + swap(distance, i, j); + } + } + } +} + + int main(void) { diff --git a/ex7/ex7.h b/ex7/ex7.h @@ -41,6 +41,20 @@ struct Object { int m; }; +/* +struct ObjectCol { + struct ObjectCol up; + struct ObjectCol down; + struct Object obj; +}; + +struct ObjectRow { + struct ObjectRow left; + struct ObjectRow right; + struct ObjectCol; +}; +*/ + struct rect { struct Point pp; struct Point p; diff --git a/ex7/physics.c b/ex7/physics.c @@ -7,7 +7,7 @@ static void rect_next_tick(struct Object *, long); static void rect_handle_collision_mf(struct Object *, struct Object *); /* - *1 if collide, 0 if not + * 1 if collide, 0 if not */ int test_collision(struct Object *o1, struct Object *o2) @@ -27,8 +27,10 @@ test_collision_rr(struct Object *o1, struct Object *o2) fprintf(stderr, "test_collision_rr: invalid objects\n"); return -1; } - return o1->p.x < o2->p.x + o2->body.rectangle.w && o2->p.x < o1->p.x + o1->body.rectangle.w && - o2->p.y < o1->p.y + o1->body.rectangle.h && o1->p.y < o2->p.y + o2->body.rectangle.h; + return o1->p.x < o2->p.x + o2->body.rectangle.w && + o2->p.x < o1->p.x + o1->body.rectangle.w && + o2->p.y < o1->p.y + o1->body.rectangle.h && + o1->p.y < o2->p.y + o2->body.rectangle.h; } void