summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2022-12-09 21:04:34 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2022-12-09 21:04:34 +0100
commitac4b9bd1ce461ecd2d54b8dbed633658b0431f8b (patch)
tree9246b6352b0ee70599b400f00c161a68d40b852e
parent1966263dd141a1c012a272a84af96206d68da55c (diff)
fix collision
-rw-r--r--build/zombies.exebin1658230 -> 1659927 bytes
-rw-r--r--bullets.c44
-rw-r--r--include/map.h2
-rw-r--r--include/math_helper.h2
-rw-r--r--map.c30
-rw-r--r--math_helper.c28
-rw-r--r--players.c10
-rw-r--r--zombies.c1
8 files changed, 77 insertions, 40 deletions
diff --git a/build/zombies.exe b/build/zombies.exe
index 5f9da2b..e6ff922 100644
--- a/build/zombies.exe
+++ b/build/zombies.exe
Binary files differ
diff --git a/bullets.c b/bullets.c
index 374c74a..dae2a9b 100644
--- a/bullets.c
+++ b/bullets.c
@@ -133,6 +133,31 @@ bool check_if_bullet_collided_with_zombie(bullet b, platform_window* window, boo
return result;
}
+static bool check_if_bullet_collided_with_ground(bullet *b, platform_window* window) {
+ map_info info = get_map_info(window);
+ float dirx = (b->endx - b->position.x);
+ float diry = (b->endy - b->position.y);
+ float length = sqrt(dirx * dirx + diry * diry);
+ dirx /= length;
+ diry /= length;
+ double nr_tiles_to_check = length*4; // check 4 points per tile.
+
+ for (int i = 1; i < nr_tiles_to_check; i++) {
+ float xtocheck = b->position.x + (dirx*i/4);
+ float ytocheck = b->position.y + (diry*i/4);
+ if (!is_in_bounds(xtocheck, ytocheck)) break;
+ tile tile = get_tile_under_coords(window, xtocheck, ytocheck);
+
+ float h = get_height_of_tile_under_coords(window, xtocheck, ytocheck);
+ if (b->position.z <= h) {
+ b->endx = xtocheck;
+ b->endy = ytocheck;
+ return true;
+ }
+ }
+ return false;
+}
+
void draw_bullets(platform_window* window) {
float size = get_bullet_size(window);
map_info info = get_map_info(window);
@@ -140,19 +165,12 @@ void draw_bullets(platform_window* window) {
for (int i = 0; i < max_bullets; i++) {
bullet b = bullets[i];
if (!b.active) continue;
-
- /*
- if (is_in_bounds(window, b.position.x, b.position.y)) {
- tile t = get_tile_under_coords(window, b.position.x, b.position.y);
-
- float h = get_height_of_tile_under_coords(window, bullets[i].position.x, bullets[i].position.y);
- if (h >= b.position.z) {
- bullets[i].active = false; // hit the ground.
- }
+
+ if (check_if_bullet_collided_with_ground(&b, window)) {
+ bullets[i].endy = b.endy;
+ bullets[i].endx = b.endx;
+ b = bullets[i];
}
- else {
- bullets[i].active = false;
- }*/
if (check_if_bullet_collided_with_object(&b, window)) {
bullets[i].endy = b.endy;
@@ -165,7 +183,7 @@ void draw_bullets(platform_window* window) {
}
bullets[i].alive_time += update_delta;
- if (bullets[i].alive_time > 0.03f) bullets[i].active = false;
+ bullets[i].active = false;
if (!b.active) continue;
diff --git a/include/map.h b/include/map.h
index 80772b1..7c0716d 100644
--- a/include/map.h
+++ b/include/map.h
@@ -62,7 +62,7 @@ tile get_tile_under_coords(platform_window* window, float x, float y);
float get_height_of_tile_under_coords(platform_window* window, float tocheckx, float tochecky);
int get_tile_height(platform_window* window);
int get_tile_width(platform_window* window);
-bool is_in_bounds(platform_window* window, float x, float y);
+bool is_in_bounds(float x, float y);
void draw_grid(platform_window* window);
map_info get_map_info(platform_window* window);
diff --git a/include/math_helper.h b/include/math_helper.h
index 1b84ebf..7040086 100644
--- a/include/math_helper.h
+++ b/include/math_helper.h
@@ -8,7 +8,7 @@
#include "map.h"
#define MAP_RENDER_DEPTH renderer->set_render_depth(1);
-#define BULLET_RENDER_DEPTH(_h) renderer->set_render_depth(4 + ceil(_h));
+#define BULLET_RENDER_DEPTH(_h) renderer->set_render_depth(5 + ceil(_h));
#define OBJECT_RENDER_DEPTH(_h) renderer->set_render_depth(5 + ceil(_h));
bool onSegment(vec2f p, vec2f q, vec2f r);
diff --git a/map.c b/map.c
index 971650b..e6f7fd5 100644
--- a/map.c
+++ b/map.c
@@ -152,15 +152,18 @@ inline int get_tile_height(platform_window* window) {
return tile_height;
}
-bool is_in_bounds(platform_window* window, float x, float y) {
- int tile_width = get_tile_width(window);
- int tile_height = get_tile_height(window);
- map_info info = get_map_info(window);
- int xdiff_between_bottom_and_top = info.px_incline;
+bool is_in_bounds(float x, float y) {
return (x >= 0 && x <= MAP_SIZE_X && y >= 0 && y < MAP_SIZE_Y);
}
+static float offset = 0.0f;
+static bool dir = true;
void draw_grid(platform_window* window) {
+ /*if (dir) offset += 0.005f;
+ else offset -= 0.005f;
+ if (offset >= 0.5f) dir = false;
+ if (offset <= -0.5f) dir = true;*/
+
map_info info = get_map_info(window);
for (int y = 0; y < MAP_SIZE_Y; y++) {
@@ -168,9 +171,9 @@ void draw_grid(platform_window* window) {
for (int x = MAP_SIZE_X-1; x >= 0; x--) {
tile tile = map_loaded[y][x];
- int xdiff_between_bottom_and_top = info.px_incline;
- int render_x = (info.tile_width * x) + (xdiff_between_bottom_and_top * y);
- int render_y = info.tile_height * y;
+ float xdiff_between_bottom_and_top = info.px_incline;
+ float render_x = (info.tile_width * x) + (xdiff_between_bottom_and_top * y);
+ float render_y = info.tile_height * y;
render_y -= tile.height*info.px_raised_per_h;
vec2f topleft;
@@ -233,19 +236,10 @@ void draw_grid(platform_window* window) {
}
inline map_info get_map_info(platform_window* window) {
- /*
- static float offset = 0.0f;
- static bool dir = true;
- if (dir) offset += 0.00001f;
- else offset -= 0.00001f;
- if (offset >= 0.5f) dir = false;
- if (offset <= -0.5f) dir = true;
- */
-
map_info info;
info.tile_width = get_tile_width(window);
info.tile_height = get_tile_height(window);
- info.px_incline = info.tile_width/3; // info.tile_width/3;
+ info.px_incline = info.tile_width/3; // info.tile_width/3; // offset*info.tile_width;
info.px_raised_per_h = info.tile_height/2.5;
return info;
} \ No newline at end of file
diff --git a/math_helper.c b/math_helper.c
index b6e0cb3..9ae2c9e 100644
--- a/math_helper.c
+++ b/math_helper.c
@@ -20,6 +20,24 @@ int orientation(vec2f p, vec2f q, vec2f r)
return (val > 0)? 1: 2; // clock or counterclock wise
}
+vec2f get_dir_of_line(vec2f p1, vec2f p2) {
+ float dirx = (p1.x - p2.x);
+ float diry = (p1.y - p2.y);
+ double length = sqrt(dirx * dirx + diry * diry);
+ dirx /= length;
+ diry /= length;
+ return (vec2f){dirx, diry};
+}
+
+bool neg2(float p1, float p2) {
+ return p1 < 0.0f && p2 < 0.0f || abs(p1-p2) < 1.0f;
+}
+
+bool pos2(float p1, float p2) {
+ return (p1 >= 0.0f && p2 >= 0.0f) || abs(p1-p2) < 1.0f;
+}
+
+
bool lines_intersect(vec2f p1, vec2f q1, vec2f p2, vec2f q2)
{
int o1 = orientation(p1, q1, p2);
@@ -28,8 +46,14 @@ bool lines_intersect(vec2f p1, vec2f q1, vec2f p2, vec2f q2)
int o4 = orientation(p2, q2, q1);
// General case
- if (o1 != o2 && o3 != o4)
- return true;
+ if (o1 != o2 && o3 != o4) {
+ vec2f bdir = get_dir_of_line(p1, q1);
+ vec2f pdir = get_dir_of_line(p1, p2);
+ if ((neg2(bdir.x, pdir.x) || pos2(bdir.x, pdir.x)) && (neg2(bdir.y, pdir.y) || pos2(bdir.y, pdir.y))) return true; // going down
+ //return true;
+
+ //return false;
+ }
// Special Cases
// p1, q1 and p2 are collinear and p2 lies on segment p1q1
diff --git a/players.c b/players.c
index 300f0c2..0ad042f 100644
--- a/players.c
+++ b/players.c
@@ -47,7 +47,7 @@ void take_player_input(platform_window* window) {
if (keyboard_is_key_down(KEY_W)) {
float newy = playery - speed;
- if (is_in_bounds(window, playerx, newy)) {
+ if (is_in_bounds(playerx, newy)) {
playery = newy;
object o = check_if_player_collided_with_object(window);
if (o.active) playery = o.position.y+o.size.y - get_player_size_in_tile() + pad_between_player_and_obj;
@@ -56,7 +56,7 @@ void take_player_input(platform_window* window) {
if (keyboard_is_key_down(KEY_S)) {
float newy = playery + speed;
- if (is_in_bounds(window, playerx, newy)) {
+ if (is_in_bounds(playerx, newy)) {
playery = newy;
object o = check_if_player_collided_with_object(window);
if (o.active) playery = o.position.y - get_player_size_in_tile() - pad_between_player_and_obj;
@@ -65,7 +65,7 @@ void take_player_input(platform_window* window) {
if (keyboard_is_key_down(KEY_A)) {
float newx = playerx - speed;
- if (is_in_bounds(window, newx, playery)) {
+ if (is_in_bounds(newx, playery)) {
playerx = newx;
object o = check_if_player_collided_with_object(window);
if (o.active) playerx = o.position.x+o.size.x + pad_between_player_and_obj;
@@ -74,7 +74,7 @@ void take_player_input(platform_window* window) {
if (keyboard_is_key_down(KEY_D)) {
float newx = playerx + speed;
- if (is_in_bounds(window, newx, playery)) {
+ if (is_in_bounds(newx, playery)) {
playerx = newx;
object o = check_if_player_collided_with_object(window);
if (o.active) playerx = o.position.x-get_player_size_in_tile() - pad_between_player_and_obj;
@@ -91,7 +91,7 @@ void draw_player(platform_window* window) {
sec_since_last_shot += update_delta;
if (is_left_down()) {
- if (sec_since_last_shot > 0.3f) {
+ if (sec_since_last_shot > 0.1f) {
shoot(window);
sec_since_last_shot = 0.0f;
}
diff --git a/zombies.c b/zombies.c
index 80a34ad..6b646fe 100644
--- a/zombies.c
+++ b/zombies.c
@@ -1,6 +1,7 @@
#include "include/zombies.h"
void spawn_zombie(int x, int y) {
+ return;
for (int i = 0; i < max_zombies; i++) {
zombie o = zombies[i];
if (o.alive) continue;