diff options
| -rw-r--r-- | include/physics.h | 31 | ||||
| -rw-r--r-- | libphysics/Makefile | 23 | ||||
| -rw-r--r-- | libphysics/_map.c | 23 | ||||
| -rw-r--r-- | libphysics/aabb.c | 52 | ||||
| -rw-r--r-- | libphysics/blockmap.c | 146 | ||||
| -rw-r--r-- | libphysics/body.c | 101 | ||||
| -rw-r--r-- | libphysics/dat.h | 45 | ||||
| -rw-r--r-- | libphysics/event.c | 44 | ||||
| -rw-r--r-- | libphysics/narrow.c | 36 | ||||
| -rw-r--r-- | libphysics/test/Makefile | 26 | ||||
| -rw-r--r-- | libphysics/test/aabbcheck.c | 115 | ||||
| -rw-r--r-- | libphysics/test/perf.c | 35 | ||||
| -rw-r--r-- | libphysics/test/vis.c | 135 | ||||
| -rw-r--r-- | libphysics/tick.c | 142 |
14 files changed, 0 insertions, 954 deletions
diff --git a/include/physics.h b/include/physics.h deleted file mode 100644 index 8ffa438..0000000 --- a/include/physics.h +++ /dev/null @@ -1,31 +0,0 @@ -#define PHX_TICK_TIME (1.0 / 480.0) - -typedef int BodyID; - -typedef enum { - BTYPE_AABB, - BTYPE_TILEMAP, -} BodyType; - -typedef struct Body Body; -typedef struct CollisionEvent { - BodyID body1; - BodyID body2; -} CollisionEvent; - -BodyID phxnew(BodyType type); -void phxdel(BodyID); - -void phxsetmap(int w, int h, int *tiles); - -void phxsetpos(BodyID, float x, float y); -void phxsetsize(BodyID, float w, float h); -void phxapplyaccel(BodyID, float f[2]); - -void phxgetpos(BodyID, float pos[2]); -void phxgetsize(BodyID, float size[2]); - -int phxnextcollevent(CollisionEvent *ev); -void phxtick(float delta); - -void phxfixtick(void); diff --git a/libphysics/Makefile b/libphysics/Makefile deleted file mode 100644 index d951ff6..0000000 --- a/libphysics/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -OBJ=aabb.o\ - body.o\ - event.o\ - _map.o\ - tick.o\ - blockmap.o\ - narrow.o - -CFLAGS=-I../include -g -O3 -flto - -all: libphysics.a - -clean: - rm -f libphysics.a - rm -f $(OBJ) - -libphysics.a: $(OBJ) - ar rcs $@ $^ - -%.o: %.c dat.h - $(CC) $< $(CFLAGS) -c -o $@ - -.PHONY: all clean diff --git a/libphysics/_map.c b/libphysics/_map.c deleted file mode 100644 index 3057bf4..0000000 --- a/libphysics/_map.c +++ /dev/null @@ -1,23 +0,0 @@ -#include <vecmath.h> -#include <physics.h> - -#include <stdlib.h> -#include <string.h> - -#include "dat.h" - -int *phxmapbuffer; -int phxmapwidth, phxmapheight; - -void -phxsetmap(int w, int h, int *tiles) -{ - if(phxmapbuffer) - free(phxmapbuffer); - - phxmapwidth = w; - phxmapheight = h; - - phxmapbuffer = malloc(w * h * sizeof(phxmapbuffer[0])); - memcpy(phxmapbuffer, tiles, sizeof(phxmapbuffer[0]) * w * h); -} diff --git a/libphysics/aabb.c b/libphysics/aabb.c deleted file mode 100644 index 59648c8..0000000 --- a/libphysics/aabb.c +++ /dev/null @@ -1,52 +0,0 @@ -#include <vecmath.h> -#include <physics.h> -#include <stdlib.h> - -#include "dat.h" - -static void mink(Body *a, Body *b, vec2 min, vec2 max); - -int -phxaabbcheck(Body *a, Body *b) -{ - vec2 min, max; - mink(a, b, min, max); - return min[0] < 0 && max[0] > 0 && min[1] < 0 && max[1] > 0; -} - -void -phxaabbresolv(Body *a, Body *b, vec2 p, vec2 n) -{ - vec2 min, max; - mink(a, b, min, max); - - float hx = fabsf(min[0]) < fabsf(max[0]) ? fabsf(min[0]) : fabsf(max[0]); - float hy = fabsf(min[1]) < fabsf(max[1]) ? fabsf(min[1]) : fabsf(max[1]); - - if(hx < hy) { - n[1] = 0.0; - n[0] = (fabsf(min[0]) < fabsf(max[0]) ? -1.0 : 1.0); - } else { - n[0] = 0; - n[1] = (fabsf(min[1]) < fabsf(max[1]) ? -1.0 : 1.0); - } - - vec2_mul(p, (vec2){ hx, hy }, n); -} - - -void -mink(Body *a, Body *b, vec2 min, vec2 max) -{ - 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]; - } - - for(int i = 0; i < 2; i++) { - min[i] = p[i] - s[i]; - max[i] = p[i] + s[i]; - } -} diff --git a/libphysics/blockmap.c b/libphysics/blockmap.c deleted file mode 100644 index 26af021..0000000 --- a/libphysics/blockmap.c +++ /dev/null @@ -1,146 +0,0 @@ -#include <vecmath.h> -#include <physics.h> - -#include <stdlib.h> -#include <string.h> - -#include "dat.h" - -#define BUCKETS 0x10000 /* must be a power of 2 */ - -#define BUCKETMASK (BUCKETS - 1) - -#define PHI 1.618033988749895 -#define HMUL (int)(BUCKETS / PHI) - -#define LENGTH(X) (sizeof (X) / sizeof (X)[0]) - -static int hash(int a); -static void makeblockmap(Body *b); - -static void enqueuetest(TestCollision *t); -static void bodycollisionmap(Body *); -static void bodycollisionmapsub(Body *, BlockmapNode *); -static BlockmapNode *allocnode(void); - -static void phxmakeblkmap(void); -static BlockmapNode *phxnodelist(float x, float y); - -static BlockmapNode nodes[(1 << 20)]; -static int nodesi; -static BlockmapNode *lists[BUCKETS]; - -static TestCollision testcollision[0x1000]; -static int testcollisioncount; - -void -__phxbroadphasereset(void) -{ - static int count = 0; - if(count == 0) - phxmakeblkmap(); - count = (count + 1) % 4; -} - -void -__phxbroadphase(int n, BodyID bid[n]) -{ - testcollisioncount = 0; - for(int i = 0; i < n; i++) { - Body *b = phxbodypool + bid[i]; - bodycollisionmap(b); - } - __phxnarrowphase(testcollisioncount, testcollision); -} - -void -phxmakeblkmap() -{ - nodesi = 0; - - for(int i = 0; i < BUCKETS; i++) - lists[i] = NULL; - - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - makeblockmap(b); - } -} - -void -makeblockmap(Body *b) -{ - BlockmapNode *n; - if((n = allocnode()) == NULL) - return; - - n->id = b - phxbodypool; - n->next = NULL; - - int mapx = (int)floorf(b->pos[0] / BLOCK_SIZE); - int mapy = (int)floorf(b->pos[1] / BLOCK_SIZE); - int h = hash(mapx + hash(mapy)); - - n->next = lists[h]; - lists[h] = n; -} - -int -hash(int a) -{ - return (a * HMUL) & BUCKETMASK; -} - -BlockmapNode * -allocnode(void) -{ - if(nodesi == (sizeof(nodes) / sizeof(nodes[0]))) - return NULL; - return nodes + nodesi++; -} - -BlockmapNode * -phxnodelist(float x, float y) -{ - int mapx = (int)floorf(x / BLOCK_SIZE); - int mapy = (int)floorf(y / BLOCK_SIZE); - int h = hash(mapx + hash(mapy)); - return lists[h]; -} - -void -bodycollisionmap(Body *a) -{ - for(int y = -1; y <= 1; y++) - for(int x = -1; x <= 1; x++) { - bodycollisionmapsub(a, phxnodelist( - a->pos[0] + x * BLOCK_SIZE, - a->pos[1] + y * BLOCK_SIZE - )); - } -} - -void -bodycollisionmapsub(Body *a, BlockmapNode *n) -{ - while(n) { - Body *b = phxbodypool + n->id; - enqueuetest(&(TestCollision) { - .a = a - phxbodypool, - .b = b - phxbodypool - }); - n = n->next; - } -} - -void -enqueuetest(TestCollision *t) -{ - testcollision[testcollisioncount++] = *t; - if(testcollisioncount >= LENGTH(testcollision)) { - __phxnarrowphase(testcollisioncount, testcollision); - testcollisioncount = 0; - } -} diff --git a/libphysics/body.c b/libphysics/body.c deleted file mode 100644 index 9495305..0000000 --- a/libphysics/body.c +++ /dev/null @@ -1,101 +0,0 @@ -#include <vecmath.h> -#include <physics.h> -#include <stdlib.h> -#include <assert.h> - -#include "dat.h" - -#define POOL_SIZE (1024 * 1024) - -Body phxbodypool[POOL_SIZE]; -int phxbodypoolsize; - -static BodyID allocbody(void); - -BodyID -phxnew(BodyType type) -{ - BodyID b; - - if((b = allocbody()) == -1) - return b; - - __getbodydata(b)->type = type; - return b; -} - -void -phxdel(BodyID id) -{ - __getbodydata(id)->active = 0; -} - -void -phxsetpos(BodyID b, float x, float y) -{ - vec2_dup(__getbodydata(b)->pos, (vec2){ x, y }); -} - -void -phxsetsize(BodyID b, float w, float h) -{ - vec2_dup(__getbodydata(b)->size, (vec2){ w, h }); -} - -void -phxgetpos(BodyID b, float p[2]) -{ - vec2_dup(p, __getbodydata(b)->pos); -} - -void -phxgetsize(BodyID b, float p[2]) -{ - vec2_dup(p, __getbodydata(b)->size); -} - -void -phxapplyaccel(BodyID b, float a[2]) -{ - vec2_add(__getbodydata(b)->accel, __getbodydata(b)->accel, a); -} - -BodyID -allocbody(void) -{ - int i; - for(i = 0; i < phxbodypoolsize; i++) { - if(!phxbodypool[i].active) - break; - } - - if(i >= phxbodypoolsize) { - if(phxbodypoolsize == POOL_SIZE) - return -1; - i = phxbodypoolsize++; - } - - phxbodypool[i].active = 1; - phxbodypool[i].generation++; - - return __getbodyid(phxbodypool + i); -} - -Body * -__getbodydata(BodyID id) -{ - unsigned int i = id >> 16; - unsigned int g = id & 0xFFFF; - - assert(i < POOL_SIZE); - assert((phxbodypool[i].generation & 0xFFFF) == g); - - return phxbodypool + i; -} - -BodyID -__getbodyid(Body *b) -{ - unsigned int i = b - phxbodypool; - return (i << 16) | (phxbodypool[i].generation & 0xFFFF); -} diff --git a/libphysics/dat.h b/libphysics/dat.h deleted file mode 100644 index 452aee6..0000000 --- a/libphysics/dat.h +++ /dev/null @@ -1,45 +0,0 @@ -#define BLOCK_SIZE 16 - -typedef struct Body { - BodyType type; - int active; - int generation; - vec2 pos; - vec2 size; - vec2 vel; - vec2 accel; -} Body; - -typedef struct BlockmapNode BlockmapNode; -struct BlockmapNode { - BodyID id; - BlockmapNode *next; -}; - -typedef struct TestCollision TestCollision; -struct TestCollision { - BodyID a, b; -}; - -void phxmapcollision(Body *); -void phxbodycollision(Body *, Body *); - -int phxaabbcheck(Body *a, Body *b); -void phxcollisionmap(Body *a); -void phxaabbresolv(Body *a, Body *b, vec2 p, vec2 n); - -int phxenqevent(CollisionEvent *ev); -int phxdeqevent(CollisionEvent *ev); - -void __phxbroadphasereset(void); -void __phxbroadphase(int n, BodyID bid[n]); -void __phxnarrowphase(int n, TestCollision coll[n]); - -Body *__getbodydata(BodyID id); -BodyID __getbodyid(Body *); - -extern int phxmapwidth, phxmapheight; -extern int *phxmapbuffer; - -extern Body phxbodypool[]; -extern int phxbodypoolsize; diff --git a/libphysics/event.c b/libphysics/event.c deleted file mode 100644 index 8df3134..0000000 --- a/libphysics/event.c +++ /dev/null @@ -1,44 +0,0 @@ -#include <vecmath.h> -#include <physics.h> -#include <stdlib.h> - -#include "dat.h" - -#define EVENT_POOL_SIZE (1 << 20) - -static CollisionEvent event[EVENT_POOL_SIZE]; -static int eventi, evente, events; - -int -phxenqevent(CollisionEvent *ev) -{ - if(events == EVENT_POOL_SIZE) - return 0; - - event[evente] = *ev; - evente = (evente + 1) % EVENT_POOL_SIZE; - events++; - - return 1; -} - -int -phxdeqevent(CollisionEvent *ev) -{ - CollisionEvent *evv; - if(events == 0) - return 0; - - evv = event + eventi; - eventi = (eventi + 1) % EVENT_POOL_SIZE; - - *ev = *evv; - events--; - return 1; -} - -int -phxnextcollevent(CollisionEvent *ev) -{ - return phxdeqevent(ev); -} diff --git a/libphysics/narrow.c b/libphysics/narrow.c deleted file mode 100644 index f6fdde6..0000000 --- a/libphysics/narrow.c +++ /dev/null @@ -1,36 +0,0 @@ -#include <vecmath.h> -#include <physics.h> -#include <stdlib.h> - -#include "dat.h" - -void -__phxnarrowphase(int n, TestCollision tests[n]) -{ - CollisionEvent event; - - for(int i = 0; i < n; i++) { - TestCollision *c = tests + i; - Body *a = c->a + phxbodypool; - Body *b = c->b + phxbodypool; - - if(a < b && phxaabbcheck(a, b)) { - float p[2], n[2], rvel[2], j; - phxaabbresolv(a, b, p, n); - - vec2_sub(rvel, a->vel, b->vel); - j = vec2_dot(rvel, n); - - vec2_sub_scaled(a->vel, a->vel, n, 0.5); - vec2_sub_scaled(b->vel, b->vel, n, -0.5); - - vec2_sub_scaled(a->pos, a->pos, p, 0.5); - vec2_sub_scaled(b->pos, b->pos, p, -0.5); - - event.body1 = __getbodyid(a); - event.body2 = __getbodyid(b); - phxenqevent(&event); - } - } -} - diff --git a/libphysics/test/Makefile b/libphysics/test/Makefile deleted file mode 100644 index 7826f84..0000000 --- a/libphysics/test/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -CFLAGS=-I../../include -g -O3 -flto -LDFLAGS=-L../ -lphysics `pkg-config --libs sdl3` -lm -flto -O3 - -all: vis perf aabbcheck - -clean: - rm -f vis - rm -f vis.o - -vis: ../libphysics.a vis.o - $(CC) $^ $(LDFLAGS) -o $@ - -perf: ../libphysics.a perf.o - $(CC) $^ $(LDFLAGS) -o $@ - -aabbcheck: ../libphysics.a aabbcheck.o - $(CC) $^ $(LDFLAGS) -o $@ - - -%.o: %.c - $(CC) $< $(CFLAGS) -c -o $@ - -../libphysics.a: - $(MAKE) -C .. all - -.PHONY: all clean ../libphysics.a diff --git a/libphysics/test/aabbcheck.c b/libphysics/test/aabbcheck.c deleted file mode 100644 index 788035b..0000000 --- a/libphysics/test/aabbcheck.c +++ /dev/null @@ -1,115 +0,0 @@ -#include <stdio.h> -#include <stdbool.h> -#include <SDL3/SDL.h> - -#include <vecmath.h> -#include <physics.h> -#include "../dat.h" - -static void process_events(); - -static SDL_Window *window; -static SDL_Renderer *renderer; -static bool running; -static Uint64 old_ticks, new_ticks; - -static void renderbody(Body *); - -static Body mousebody = { - .size = { 25, 25 }, -}; - -static Body body = { - .pos = { 400, 300 }, - .size = { 200, 200 } -}; - -int -main() -{ - if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { - fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError()); - return 1; - } - - if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) { - fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); - return -1; - } - - running = true; - old_ticks = SDL_GetTicksNS(); - while(running) { - double delta; - process_events(); - - new_ticks = SDL_GetTicksNS(); - delta = (new_ticks - old_ticks) / 1000000000.0; - old_ticks = new_ticks; - - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); - SDL_RenderClear(renderer); - if(phxaabbcheck(&body, &mousebody)) { - SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00); - } else { - SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00); - } - - renderbody(&body); - renderbody(&mousebody); - - if(phxaabbcheck(&body, &mousebody)) { - float n[2], p[2]; - - phxaabbresolv(&body, &mousebody, p, n); - SDL_RenderLine(renderer, - body.pos[0], - body.pos[1], - body.pos[0] + n[0] * body.size[0], - body.pos[1] + n[1] * body.size[1] - ); - - SDL_RenderLine(renderer, - mousebody.pos[0] - n[0] * mousebody.size[0], - mousebody.pos[1] - n[1] * mousebody.size[1], - mousebody.pos[0] - n[0] * mousebody.size[0] + p[0], - mousebody.pos[1] - n[1] * mousebody.size[1] + p[1] - ); - } - - SDL_RenderPresent(renderer); - } - - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; -} - -void -process_events() -{ - SDL_Event event; - - while(SDL_PollEvent(&event)) { - switch(event.type) { - case SDL_EVENT_QUIT: - running = false; - break; - case SDL_EVENT_MOUSE_MOTION: - mousebody.pos[0] = event.motion.x; - mousebody.pos[1] = event.motion.y; - } - } -} - -void -renderbody(Body *b) -{ - SDL_RenderRect(renderer, &(SDL_FRect){ - .x = b->pos[0] - b->size[0], - .y = b->pos[1] - b->size[1], - .w = b->size[0] * 2.0, - .h = b->size[1] * 2.0 - }); -} - diff --git a/libphysics/test/perf.c b/libphysics/test/perf.c deleted file mode 100644 index 7e2dd51..0000000 --- a/libphysics/test/perf.c +++ /dev/null @@ -1,35 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <physics.h> -#include <assert.h> - -#define SBODY 8192 -#define FLOAT_RAND (2 * (rand() / (float)RAND_MAX) - 1) - -int -main() -{ - for(int i = 0; i < SBODY; i++) { - BodyID b = phxnew(BTYPE_AABB); - assert(b != -1); - - phxsetpos(b, rand() % 800, rand() % 600); - phxsetsize(b, 5.0, 5.0); - phxapplyaccel(b, (float[]){ 50000 * FLOAT_RAND, 50000 * FLOAT_RAND }); - } - - phxsetmap(8, 8, (int[]) { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 1, 0, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 0, 1, 0, 0, 0, 1, 0, - }); - - phxtick(10.0); - - return 0; -} diff --git a/libphysics/test/vis.c b/libphysics/test/vis.c deleted file mode 100644 index 63e2b9d..0000000 --- a/libphysics/test/vis.c +++ /dev/null @@ -1,135 +0,0 @@ -#include <SDL3/SDL.h> -#include <vecmath.h> -#include <physics.h> - -#include <stdlib.h> -#include <assert.h> - -#include "../dat.h" - -#define SBODY 4096 - -static void process_events(void); -static void render(void); - -static SDL_Window *window; -static SDL_Renderer *renderer; -static bool running; -static Uint64 old_ticks, new_ticks; - -void -renderrect(vec2 p, vec2 s) -{ - SDL_RenderRect(renderer, &(SDL_FRect){ - .x = p[0] - s[0], - .y = p[1] - s[1], - .w = s[0] * 2.0, - .h = s[1] * 2.0 - }); -} - -int -main() -{ - if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { - fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError()); - return 1; - } - - if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) { - fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); - return -1; - } - SDL_SetRenderVSync(renderer, 1); - - #define FLOAT_RAND (2 * (rand() / (float)RAND_MAX) - 1) - - for(int i = 0; i < SBODY; i++) { - BodyID b = phxnew(BTYPE_AABB); - assert(b != -1); - - phxsetpos(b, rand() % 800, rand() % 600); - phxsetsize(b, 5.0, 5.0); - phxapplyaccel(b, (float[]){ (150 / PHX_TICK_TIME) * FLOAT_RAND, (150 / PHX_TICK_TIME) * FLOAT_RAND }); - } - - phxsetmap(8, 8, (int[]) { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 1, 0, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 1, 1, 0, 0, 1, 1, 0, - 0, 0, 1, 0, 0, 0, 1, 0, - }); - - running = 1; - old_ticks = SDL_GetTicksNS(); - while(running) { - CollisionEvent event; - double delta; - process_events(); - - new_ticks = SDL_GetTicksNS(); - delta = (new_ticks - old_ticks) / 1000000000.0; - old_ticks = new_ticks; - - static float accum = 0; - - accum += delta; - while(accum >= PHX_TICK_TIME) { - phxfixtick(); - while(phxnextcollevent(&event)) { - printf("Collision with %d and %d\n", event.body1, event.body2); - } - accum -= PHX_TICK_TIME; - } - - render(); - } - - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; -} - -void -process_events() -{ - SDL_Event event; - - while(SDL_PollEvent(&event)) { - switch(event.type) { - case SDL_EVENT_QUIT: - running = false; - break; - } - } -} - -void -render(void) -{ - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); - SDL_RenderClear(renderer); - - SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF); - for(int y = 0; y < phxmapheight; y++) - for(int x = 0; x < phxmapwidth; x++) { - if(!phxmapbuffer[x + y * phxmapwidth]) - continue; - - renderrect((vec2){ x * 32, y * 32 }, (vec2){ 16, 16 }); - } - - SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); - for(int i = 0; i < phxbodypoolsize; i++) { - vec2 p, s; - Body *b = phxbodypool + i; - if(!b->active) - continue; - renderrect(b->pos, b->size); - } - SDL_RenderPresent(renderer); -} diff --git a/libphysics/tick.c b/libphysics/tick.c deleted file mode 100644 index dbc5f27..0000000 --- a/libphysics/tick.c +++ /dev/null @@ -1,142 +0,0 @@ -#include <vecmath.h> -#include <physics.h> -#include <stdlib.h> - -#include "dat.h" - -#define TILE_SIZE 16 - -#define LENGTH(X) (sizeof (X) / sizeof (X)[0]) - -static void tick(void); -static void mapcollision(Body *); -static void bodycollision(Body *); - -static void bodycollisionmap(Body *); -static void bodycollisionmapsub(Body *, BlockmapNode *); - -static BodyID bids[0x10000]; -static int bidsi; - - -void -phxtick(float delta) -{ - static float time; - - time += delta; - if(time >= PHX_TICK_TIME) { - while(time >= PHX_TICK_TIME) { - tick(); - time -= PHX_TICK_TIME; - } - - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - - b->accel[0] = 0.0; - b->accel[1] = 0.0; - } - } -} - -void -phxfixtick(void) -{ - tick(); - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - - b->accel[0] = 0.0; - b->accel[1] = 0.0; - } -} - -void -tick(void) -{ - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - - vec2_add_scaled(b->vel, b->vel, b->accel, PHX_TICK_TIME); - } - - __phxbroadphasereset(); - bidsi = 0; - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - - mapcollision(b); - bids[bidsi++] = i; - if(bidsi >= LENGTH(bids)) { - __phxbroadphase(bidsi, bids); - bidsi = 0; - } - } - __phxbroadphase(bidsi, bids); - - for(int i = 0; i < phxbodypoolsize; i++) { - Body *b = phxbodypool + i; - if(!b->active) - continue; - - vec2_add_scaled(b->pos, b->pos, b->vel, PHX_TICK_TIME); - } -} - -void -mapcollision(Body *a) -{ - int minx, miny, maxx, maxy; - - minx = ((int)(a->pos[0] - a->size[0]) / TILE_SIZE) * TILE_SIZE; - maxx = ((int)(a->pos[0] + a->size[0]) / TILE_SIZE) * TILE_SIZE; - miny = ((int)(a->pos[1] - a->size[1]) / TILE_SIZE) * TILE_SIZE; - maxy = ((int)(a->pos[1] + a->size[1]) / TILE_SIZE) * TILE_SIZE; - - for(int y = miny; y <= maxy + TILE_SIZE; y += TILE_SIZE) - for(int x = minx; x <= maxx + TILE_SIZE; x += TILE_SIZE) { - int tilex = x / (2 * TILE_SIZE); - int tiley = y / (2 * TILE_SIZE); - - if(tilex < 0 || tilex >= phxmapwidth) - continue; - if(tiley < 0 || tiley >= phxmapheight) - continue; - - if(!phxmapbuffer[tilex + tiley * phxmapwidth]) - continue; - - Body b = { - .pos = { - tilex * TILE_SIZE * 2.0, - tiley * TILE_SIZE * 2.0, - }, - .size = { TILE_SIZE, TILE_SIZE }, - }; - - if(phxaabbcheck(a, &b)) { - float p[2], n[2]; - phxaabbresolv(a, &b, p, n); - - vec2_sub(a->pos, a->pos, p); - - if(p[0] != 0.0) { - a->vel[0] = 0; - } else { - a->vel[1] = 0; - } - } - } -} - - - |
