summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dat.h1
-rw-r--r--physics.c45
-rw-r--r--sdl3_main.c58
3 files changed, 95 insertions, 9 deletions
diff --git a/dat.h b/dat.h
index e862217..f0e4283 100644
--- a/dat.h
+++ b/dat.h
@@ -5,3 +5,4 @@ typedef struct {
} Body;
int checkcollision(Body *b1, Body *b2);
+void resolvecoll(Body *b1, Body *b2, float p[2]);
diff --git a/physics.c b/physics.c
index f8d785b..5b32356 100644
--- a/physics.c
+++ b/physics.c
@@ -1,20 +1,49 @@
+#include <math.h>
+
#include "dat.h"
+static void mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy);
+
int
checkcollision(Body *a, Body *b)
{
- float p[2], s[2], minx, miny, maxx, maxy;
+ float minx, miny, maxx, maxy;
+
+ mink(a, b, &minx, &miny, &maxx, &maxy);
+
+ return minx < 0 && maxx > 0 && miny < 0 && maxy > 0;
+}
+
+void
+resolvecoll(Body *a, Body *b, float p[2])
+{
+ float minx, miny, maxx, maxy;
+ mink(a, b, &minx, &miny, &maxx, &maxy);
+
+ float hx = fabsf(minx) < fabsf(maxx) ? fabsf(minx) : fabsf(maxx);
+ float hy = fabsf(miny) < fabsf(maxy) ? fabsf(miny) : fabsf(maxy);
+
+ if(hx < hy) {
+ p[1] = 0;
+ p[0] = hx * (fabsf(minx) < fabsf(maxx) ? -1.0 : 1.0);
+ } else {
+ p[0] = 0;
+ p[1] = hy * (fabsf(miny) < fabsf(maxy) ? -1.0 : 1.0);
+ }
+}
+
+void
+mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy)
+{
+ float p[2], s[2];
for(int i = 0; i < 2; i++) {
s[i] = a->size[i] + b->size[i];
p[i] = a->pos[i] - b->pos[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;
+ *minx = p[0] - s[0];
+ *maxx = p[0] + s[0];
+ *miny = p[1] - s[1];
+ *maxy = p[1] + s[1];
}
-
diff --git a/sdl3_main.c b/sdl3_main.c
index b5f1404..5a4ec6f 100644
--- a/sdl3_main.c
+++ b/sdl3_main.c
@@ -34,7 +34,7 @@ static SDL_Renderer *renderer;
static bool running;
static Uint64 old_ticks, new_ticks;
-static Player player;
+static Player player = { .body.size = { ENTITY_SIZE, ENTITY_SIZE } };
static Ghost ghosts[1024];
static Bullet bullet[1024];
@@ -184,6 +184,34 @@ update(float delta)
if(kstate[SDL_SCANCODE_D])
player.body.vel[0] += SPEED;
+ for(int y = 0; y < map_height; y++)
+ for(int x = 0; x < map_width; x++) {
+ if(!map_data[x + y * map_width])
+ continue;
+
+ Body b = {
+ .pos = {
+ x * ENTITY_SIZE * 2.0,
+ y * ENTITY_SIZE * 2.0,
+ },
+ .size = { ENTITY_SIZE, ENTITY_SIZE },
+ };
+
+ if(checkcollision(&player.body, &b)) {
+ float p[2];
+ resolvecoll(&player.body, &b, p);
+
+ for(int i = 0; i < 2; i++)
+ player.body.pos[i] -= p[i];
+
+ if(p[0] != 0.0) {
+ player.body.vel[0] = 0;
+ } else {
+ player.body.vel[1] = 0;
+ }
+ }
+ }
+
if(shot) {
shot = false;
int i;
@@ -260,6 +288,34 @@ update(float delta)
}
}
+ for(int y = 0; y < map_height; y++)
+ for(int x = 0; x < map_width; x++) {
+ if(!map_data[x + y * map_width])
+ continue;
+
+ Body b = {
+ .pos = {
+ x * ENTITY_SIZE * 2.0,
+ y * ENTITY_SIZE * 2.0,
+ },
+ .size = { ENTITY_SIZE, ENTITY_SIZE },
+ };
+
+ if(checkcollision(&ghosts[i].body, &b)) {
+ float p[2];
+ resolvecoll(&ghosts[i].body, &b, p);
+
+ for(int i = 0; i < 2; i++)
+ ghosts[i].body.pos[i] -= p[i];
+
+ if(p[0] != 0.0) {
+ vx = 0;
+ } else {
+ vy = 0;
+ }
+ }
+ }
+
ghosts[i].body.vel[0] = vx;
ghosts[i].body.vel[1] = vy;
}