diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-05-03 19:01:24 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-05-03 19:01:24 -0300 |
| commit | caefbf2e217e9a14ea62fa82caa796e924e52074 (patch) | |
| tree | 6bcca2cb9b292df05cc170c189dbfe11a5ab0726 | |
| parent | c50ad43317e7c03740885af5e43cd4e2fa750348 (diff) | |
otimizado colisão com mapa
| -rw-r--r-- | physics.c | 74 |
1 files changed, 47 insertions, 27 deletions
@@ -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; + } + } + } +} |
