summaryrefslogtreecommitdiff
path: root/physics.c
diff options
context:
space:
mode:
Diffstat (limited to 'physics.c')
-rw-r--r--physics.c74
1 files changed, 47 insertions, 27 deletions
diff --git a/physics.c b/physics.c
index 94b15e8..c583d22 100644
--- a/physics.c
+++ b/physics.c
@@ -7,6 +7,7 @@
static void mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy);
static int checkcollision(Body *a, Body *b);
static void resolvecoll(Body *a, Body *b, float p[2]);
+static void mapcollision(Body *a);
static EntityCollision ecoll[2048];
static int ecollstart, ecollend, ecollsi;
@@ -19,33 +20,7 @@ entphysics(float delta)
if(!e->active)
continue;
- 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(&e->body, &b)) {
- float p[2];
- resolvecoll(&e->body, &b, p);
-
- for(int i = 0; i < 2; i++)
- e->body.pos[i] -= p[i];
-
- if(p[0] != 0.0) {
- e->body.vel[0] = 0;
- } else {
- e->body.vel[1] = 0;
- }
- }
- }
+ mapcollision(&e->body);
for(int j = i; j >= 0; j--) {
Entity *f = entitybuffer + j;
@@ -147,4 +122,49 @@ deqcoll(void)
return c;
}
+void
+mapcollision(Body *a)
+{
+ int minx, miny, maxx, maxy;
+
+ minx = ((int)(a->pos[0] - a->size[0]) / ENTITY_SIZE) * ENTITY_SIZE;
+ maxx = ((int)(a->pos[0] + a->size[0]) / ENTITY_SIZE) * ENTITY_SIZE;
+ miny = ((int)(a->pos[1] - a->size[1]) / ENTITY_SIZE) * ENTITY_SIZE;
+ maxy = ((int)(a->pos[1] + a->size[1]) / ENTITY_SIZE) * ENTITY_SIZE;
+
+ for(int y = miny; y <= maxy + ENTITY_SIZE; y += ENTITY_SIZE)
+ for(int x = minx; x <= maxx + ENTITY_SIZE; x += ENTITY_SIZE) {
+ int tilex = x / (2 * ENTITY_SIZE);
+ int tiley = y / (2 * ENTITY_SIZE);
+ if(tilex < 0 || tilex >= map_width)
+ continue;
+ if(tiley < 0 || tiley >= map_height)
+ continue;
+
+ if(!map_data[tilex + tiley * map_width])
+ continue;
+
+ Body b = {
+ .pos = {
+ tilex * ENTITY_SIZE * 2.0,
+ tiley * ENTITY_SIZE * 2.0,
+ },
+ .size = { ENTITY_SIZE, ENTITY_SIZE },
+ };
+
+ if(checkcollision(a, &b)) {
+ float p[2];
+ resolvecoll(a, &b, p);
+
+ for(int i = 0; i < 2; i++)
+ a->pos[i] -= p[i];
+
+ if(p[0] != 0.0) {
+ a->vel[0] = 0;
+ } else {
+ a->vel[1] = 0;
+ }
+ }
+ }
+}