diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-04-24 20:55:49 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-04-24 20:55:49 -0300 |
| commit | 658dd23ba0e7114ceee0c78fb769f31f0aeeee0a (patch) | |
| tree | 0d7d9f72f6f9eae075baefcb7dc6ea6a73299241 | |
| parent | 15a8758f6a99d18ab481e971c7cdfa0c10b71ff6 (diff) | |
colisão do mapa
| -rw-r--r-- | dat.h | 1 | ||||
| -rw-r--r-- | physics.c | 45 | ||||
| -rw-r--r-- | sdl3_main.c | 58 |
3 files changed, 95 insertions, 9 deletions
@@ -5,3 +5,4 @@ typedef struct { } Body; int checkcollision(Body *b1, Body *b2); +void resolvecoll(Body *b1, Body *b2, float p[2]); @@ -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; } |
