diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-06-02 23:42:05 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-06-02 23:42:05 -0300 |
| commit | 005f0a3185eb8085f6e0de11e3cfd0a3d9167719 (patch) | |
| tree | 27e8268b818068a4ca574025ef3b04917dd8a044 /libphysics | |
| parent | 518b93c3a40b0c60054f50356357efc0ead36895 (diff) | |
| parent | ff51d49dc9c7024d29b9177f8e748522625ff1f4 (diff) | |
Merge branch 'libphysics/generational_id'
Diffstat (limited to 'libphysics')
| -rw-r--r-- | libphysics/body.c | 38 | ||||
| -rw-r--r-- | libphysics/dat.h | 4 | ||||
| -rw-r--r-- | libphysics/narrow.c | 4 |
3 files changed, 36 insertions, 10 deletions
diff --git a/libphysics/body.c b/libphysics/body.c index d8a17e5..9495305 100644 --- a/libphysics/body.c +++ b/libphysics/body.c @@ -1,6 +1,7 @@ #include <vecmath.h> #include <physics.h> #include <stdlib.h> +#include <assert.h> #include "dat.h" @@ -19,44 +20,44 @@ phxnew(BodyType type) if((b = allocbody()) == -1) return b; - phxbodypool[b].type = type; + __getbodydata(b)->type = type; return b; } void phxdel(BodyID id) { - phxbodypool[id].active = 0; + __getbodydata(id)->active = 0; } void phxsetpos(BodyID b, float x, float y) { - vec2_dup(phxbodypool[b].pos, (vec2){ x, y }); + vec2_dup(__getbodydata(b)->pos, (vec2){ x, y }); } void phxsetsize(BodyID b, float w, float h) { - vec2_dup(phxbodypool[b].size, (vec2){ w, h }); + vec2_dup(__getbodydata(b)->size, (vec2){ w, h }); } void phxgetpos(BodyID b, float p[2]) { - vec2_dup(p, phxbodypool[b].pos); + vec2_dup(p, __getbodydata(b)->pos); } void phxgetsize(BodyID b, float p[2]) { - vec2_dup(p, phxbodypool[b].size); + vec2_dup(p, __getbodydata(b)->size); } void phxapplyaccel(BodyID b, float a[2]) { - vec2_add(phxbodypool[b].accel, phxbodypool[b].accel, a); + vec2_add(__getbodydata(b)->accel, __getbodydata(b)->accel, a); } BodyID @@ -75,5 +76,26 @@ allocbody(void) } phxbodypool[i].active = 1; - return i; + 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 index 035bdcc..452aee6 100644 --- a/libphysics/dat.h +++ b/libphysics/dat.h @@ -3,6 +3,7 @@ typedef struct Body { BodyType type; int active; + int generation; vec2 pos; vec2 size; vec2 vel; @@ -34,6 +35,9 @@ 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; diff --git a/libphysics/narrow.c b/libphysics/narrow.c index 14575be..f6fdde6 100644 --- a/libphysics/narrow.c +++ b/libphysics/narrow.c @@ -27,8 +27,8 @@ __phxnarrowphase(int n, TestCollision tests[n]) vec2_sub_scaled(a->pos, a->pos, p, 0.5); vec2_sub_scaled(b->pos, b->pos, p, -0.5); - event.body1 = c->a; - event.body2 = c->b; + event.body1 = __getbodyid(a); + event.body2 = __getbodyid(b); phxenqevent(&event); } } |
