summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-11-05 13:55:55 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2023-11-05 13:55:55 +0100
commit278bdb59ef744dd51ada14c99de22fcb41c1abea (patch)
tree68dca7a895e5730b561be173af977edfa40bbc4f
parentdc81516d860885e7e6b75c4ad978a09a2efb705d (diff)
bullet cone object collision
-rw-r--r--build/zombies.exebin2011444 -> 2012468 bytes
-rw-r--r--include/bullets.h1
-rw-r--r--src/bullets.c4
-rw-r--r--src/players.c46
4 files changed, 36 insertions, 15 deletions
diff --git a/build/zombies.exe b/build/zombies.exe
index 13f0d89..0dd5fcb 100644
--- a/build/zombies.exe
+++ b/build/zombies.exe
Binary files differ
diff --git a/include/bullets.h b/include/bullets.h
index 76cd01c..26e4c5f 100644
--- a/include/bullets.h
+++ b/include/bullets.h
@@ -24,5 +24,6 @@ int max_bullets = 500;
void clear_bullets();
void shoot(platform_window* window, u32 id, float dirx, float diry);
void draw_bullets(platform_window* window);
+object_type check_if_bullet_collided_with_object(bullet* b, platform_window* window);
#endif \ No newline at end of file
diff --git a/src/bullets.c b/src/bullets.c
index 26c7766..db23938 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -32,8 +32,8 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) {
float hh = get_height_of_tile_under_coords(p->playerx, p->playery);
float rads = atan2(dirx, diry);
- float target1 = rads - (g.bullet_spread/2);
- float target2 = rads + (g.bullet_spread/2);
+ float target1 = rads - (g.bullet_spread/2.0f);
+ float target2 = rads + (g.bullet_spread/2.0f);
float target_rand = 0.0f;
{
diff --git a/src/players.c b/src/players.c
index fbb413d..9e58afa 100644
--- a/src/players.c
+++ b/src/players.c
@@ -327,6 +327,7 @@ void update_players_server() {
static void draw_player_bullet_cone(platform_window* window, player* p) {
map_info info = get_map_info(window);
float bullet_range = 100.0f;
+ int divisions = 10;
float bulletx = p->gunx;
float bullety = p->guny;
@@ -335,23 +336,42 @@ static void draw_player_bullet_cone(platform_window* window, player* p) {
// https://stackoverflow.com/questions/11462239/how-to-move-point-along-circle
float rads = -atan2(p->diry, p->dirx);
rads += M_PI/2;
- float target1 = rads - (g.bullet_spread/2);
- float target2 = rads + (g.bullet_spread/2);
+ float target1 = rads - (g.bullet_spread/2.0f);
+ float target2 = rads + (g.bullet_spread/2.0f);
+
+ float prev_target = target1;
+ float increase_per_division = g.bullet_spread/divisions;
+ for (int i = 0; i < divisions; i++)
+ {
+ float current_target = prev_target + increase_per_division;
+
+ float cone_end_left_x = bulletx+(sin(prev_target))*bullet_range;
+ float cone_end_left_y = bullety+(cos(prev_target))*bullet_range;
- float cone_end_left_x = bulletx+(sin(target1))*bullet_range;
- float cone_end_left_y = bullety+(cos(target1))*bullet_range;
+ float cone_end_right_x = bulletx+(sin(current_target))*bullet_range;
+ float cone_end_right_y = bullety+(cos(current_target))*bullet_range;
- float cone_end_right_x = bulletx+(sin(target2))*bullet_range;
- float cone_end_right_y = bullety+(cos(target2))*bullet_range;
+ bullet b1 = {.active = true, .position = {.x = bulletx, .y = bullety, .z = p->gun_height}, .endx = cone_end_left_x, .endy = cone_end_left_y};
+ check_if_bullet_collided_with_object(&b1, window);
+ cone_end_left_x = b1.endx;
+ cone_end_left_y = b1.endy;
- float x1 = bulletx*info.tile_width + (bullety*info.px_incline);
- float y1 = bullety*info.tile_height - (p->height*info.px_raised_per_h);
- float x2 = cone_end_left_x*info.tile_width + (bullety*info.px_incline);
- float y2 = cone_end_left_y*info.tile_height - (p->height*info.px_raised_per_h);
- float x3 = cone_end_right_x*info.tile_width + (bullety*info.px_incline);
- float y3 = cone_end_right_y*info.tile_height - (p->height*info.px_raised_per_h);
+ bullet b2 = {.active = true, .position = {.x = bulletx, .y = bullety, .z = p->gun_height}, .endx = cone_end_right_x, .endy = cone_end_right_y};
+ check_if_bullet_collided_with_object(&b2, window);
+ cone_end_right_x = b2.endx;
+ cone_end_right_y = b2.endy;
- renderer->render_tri(x1,y1,x2,y2,x3,y3, rgba(255,0,0,40));
+ float x1 = bulletx*info.tile_width + (bullety*info.px_incline);
+ float y1 = bullety*info.tile_height - (p->gun_height*info.px_raised_per_h);
+ float x2 = cone_end_left_x*info.tile_width + (bullety*info.px_incline);
+ float y2 = cone_end_left_y*info.tile_height - (p->gun_height*info.px_raised_per_h);
+ float x3 = cone_end_right_x*info.tile_width + (bullety*info.px_incline);
+ float y3 = cone_end_right_y*info.tile_height - (p->gun_height*info.px_raised_per_h);
+
+ renderer->render_tri(x1,y1,x2,y2,x3,y3, rgba(255,0,0,40));
+
+ prev_target = current_target;
+ }
}
void draw_players(platform_window* window) {