summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoresquizo <esquizo+noreply@esquizo.net>2026-04-22 14:33:57 -0300
committeresquizo <esquizo+noreply@esquizo.net>2026-04-22 14:33:57 -0300
commit216d4a05d3b878741f46cc748794a92faa78aa63 (patch)
tree3f96f6149b6d5168fae164b8d11b11bd45331ec3
parentd2b4066cab63fb51ad17c90baadcfa6fa1f82d07 (diff)
adicionado checkcollision
-rw-r--r--sdl3_main.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/sdl3_main.c b/sdl3_main.c
index 7ecca2b..26b447d 100644
--- a/sdl3_main.c
+++ b/sdl3_main.c
@@ -12,6 +12,8 @@ static void render(void);
static void update(float delta);
static void process_events(void);
+static int checkcollision(float pos1[2], float size1[2], float pos2[2], float size2[2]);
+
typedef struct {
float x, y;
} Player;
@@ -193,21 +195,13 @@ update(float delta)
for(int j = 0; j < 1024; j++) {
if(!ghosts[j].active)
continue;
-
- float dx = bullet[i].x - ghosts[j].x;
- float dy = bullet[i].y - ghosts[j].y;
-
- float hs = (ENTITY_SIZE + BULLET_SIZE);
-
- float minx = dx - hs;
- float miny = dy - hs;
- float maxx = dx + hs;
- float maxy = dy + hs;
- bool contains_x = minx < 0 && maxx > 0;
- bool contains_y = miny < 0 && maxy > 0;
-
- if(contains_x && contains_y) {
+ int r = checkcollision(
+ (float[2]){ bullet[i].x, bullet[i].y }, (float[2]){ BULLET_SIZE, BULLET_SIZE },
+ (float[2]){ ghosts[j].x, ghosts[j].y }, (float[2]){ ENTITY_SIZE, ENTITY_SIZE }
+ );
+
+ if(r) {
ghosts[j].active = false;
bullet[i].active = false;
}
@@ -273,6 +267,7 @@ update(float delta)
bullet[i].y += delta * bullet[i].vy;
}
}
+
void
process_events()
{
@@ -292,3 +287,21 @@ process_events()
}
}
+int
+checkcollision(float p1[2], float s1[2], float p2[2], float s2[2])
+{
+ float p[2], s[2], minx, miny, maxx, maxy;
+
+
+ for(int i = 0; i < 2; i++) {
+ s[i] = s1[i] + s2[i];
+ p[i] = p1[i] - p2[i];
+ }
+
+ minx = p[0] - s[0];
+ maxx = p[0] + s[0];
+ miny = p[1] - s[1];
+ maxy = p[1] + s[1];
+
+ return minx < 0 && maxx > 0 && miny < 0 && maxy > 0;
+}