diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-05-21 17:15:21 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-05-21 17:15:40 -0300 |
| commit | 1047bed569232312619122f56ee4a6b2a323cc1c (patch) | |
| tree | 205107433012bb44df614a59b03a82384ec13188 | |
| parent | fb31105dcd1298c95d4a1eaf494c0aefa5e9be95 (diff) | |
libphysics: update de um tick
| -rw-r--r-- | include/physics.h | 4 | ||||
| -rw-r--r-- | libphysics/test/vis.c | 17 | ||||
| -rw-r--r-- | libphysics/tick.c | 14 |
3 files changed, 30 insertions, 5 deletions
diff --git a/include/physics.h b/include/physics.h index 5c4f377..8ffa438 100644 --- a/include/physics.h +++ b/include/physics.h @@ -1,3 +1,5 @@ +#define PHX_TICK_TIME (1.0 / 480.0) + typedef int BodyID; typedef enum { @@ -25,3 +27,5 @@ void phxgetsize(BodyID, float size[2]); int phxnextcollevent(CollisionEvent *ev); void phxtick(float delta); + +void phxfixtick(void); diff --git a/libphysics/test/vis.c b/libphysics/test/vis.c index ad3cef8..30688df 100644 --- a/libphysics/test/vis.c +++ b/libphysics/test/vis.c @@ -7,7 +7,7 @@ #include "../dat.h" -#define SBODY 256 +#define SBODY 2048 static void process_events(void); static void render(void); @@ -50,7 +50,7 @@ main() phxsetpos(b, rand() % 800, rand() % 600); phxsetsize(b, 5.0, 5.0); - phxapplyaccel(b, (float[]){ 30000 * FLOAT_RAND, 30000 * FLOAT_RAND }); + phxapplyaccel(b, (float[]){ (150 / PHX_TICK_TIME) * FLOAT_RAND, (150 / PHX_TICK_TIME) * FLOAT_RAND }); } phxsetmap(8, 8, (int[]) { @@ -74,9 +74,16 @@ main() new_ticks = SDL_GetTicksNS(); delta = (new_ticks - old_ticks) / 1000000000.0; old_ticks = new_ticks; - phxtick(delta); - while(phxnextcollevent(&event)) { - printf("Collision with %d and %d\n", event.body1, event.body2); + + 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(); diff --git a/libphysics/tick.c b/libphysics/tick.c index f31930a..23f7fa0 100644 --- a/libphysics/tick.c +++ b/libphysics/tick.c @@ -39,6 +39,20 @@ phxtick(float delta) } 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) { static int count = 0; |
