diff options
| author | Joel Stålnacke <joel@saker.fi> | 2026-03-23 19:50:18 +0200 |
|---|---|---|
| committer | Joel Stålnacke <joel@saker.fi> | 2026-03-23 19:50:18 +0200 |
| commit | 3a1f9fe6800ef5fe7bbf1c0a5976720383ba84fe (patch) | |
| tree | f1218eb4085a654f6bb70671b41b4a74e646c052 /lib/physics.c | |
| parent | 3870982f640260822bf605e693782ce9f5d79cf6 (diff) | |
Rewrite native code in Odin and use ECSodin
Diffstat (limited to 'lib/physics.c')
| -rw-r--r-- | lib/physics.c | 146 |
1 files changed, 0 insertions, 146 deletions
diff --git a/lib/physics.c b/lib/physics.c deleted file mode 100644 index 0039bd7..0000000 --- a/lib/physics.c +++ /dev/null @@ -1,146 +0,0 @@ -#include <assert.h> -#include <stdlib.h> -#include <string.h> - -#include "common.h" -#include "player.h" -#include "snapshot.h" -#include "types.h" - -#include "physics.h" - -struct context * -dp_physics_context_create(int32_t snapshots, int32_t players, int32_t balls) -{ - int32_t i, j; - struct context *ctx = NULL; - - if (!(ctx = malloc(sizeof(*ctx)))) - return NULL; - memset(ctx, 0, sizeof(*ctx)); - - ctx->snapshots_len = snapshots; - if (!(ctx->snapshots = malloc(sizeof(*ctx->snapshots) * snapshots))) - { - free(ctx); - return NULL; - } - memset(ctx->snapshots, 0, sizeof(*ctx->snapshots) * snapshots); - - for (i = 0; i < snapshots; i++) - { - if (dp_snapshot_init(&ctx->snapshots[i], players, balls) != 0) - { - for (j = 0; j < i; j++) - { - dp_snapshot_deinit(&ctx->snapshots[j]); - } - free(ctx); - } - } - - return ctx; -} - -void -dp_physics_context_free(struct context *ctx) -{ - int32_t i; - if (!ctx) - return; - if (ctx->snapshots) - { - for (i = 0; i < ctx->snapshots_len; i++) - { - dp_snapshot_deinit(&ctx->snapshots[i]); - } - free(ctx->snapshots); - } - free(ctx); -} - -struct snapshot * -dp_physics_context_get_snapshot(struct context *ctx) -{ - int32_t i; - struct snapshot *s; - - for (i = 0; i < ctx->snapshots_len; i++) - { - s = &ctx->snapshots[i]; - if (!s->active) - { - s->active = true; - return s; - } - } - return NULL; -} - -void -dp_physics_context_return_snapshot(struct context *ctx, struct snapshot *s) -{ - int32_t i; - - assert(s->active); - - for (i = 0; i < ctx->snapshots_len; i++) - { - if (s == &ctx->snapshots[i]) - { - s->active = false; - } - } - - assert(!s->active); -} - -static void -dp_physics_tick_player(const struct player *cur, double delta, struct player *next) -{ - vec2 acc; - - next->state = cur->state; - if (next->state == STATE_REMOVED) - next->state = STATE_INACTIVE; - if (next->state == STATE_INACTIVE) - return; - - acc = dp_player_calculate_acceleration(cur); - next->vel = dp_vec2_add(cur->vel, dp_vec2_mul(acc, delta)); - next->pos = dp_vec2_add(cur->pos, dp_vec2_mul(next->vel, delta)); -} - -static void -dp_physics_tick_ball(const struct ball *cur, double delta, struct ball *next) -{ - next->state = cur->state; - if (next->state == STATE_REMOVED) - next->state = STATE_INACTIVE; - if (next->state == STATE_INACTIVE) - return; - - next->pos = dp_vec2_add(cur->pos, dp_vec2_mul(cur->vel, delta)); -} - -int -dp_physics_tick(const struct snapshot *cur, double delta, struct snapshot *next) -{ - int32_t i; - - assert(cur->active); - assert(next->active); - - for (i = 0; i < cur->players_len; i++) - { - dp_physics_tick_player(&cur->players[i], delta, &next->players[i]); - } - - for (i = 0; i < cur->balls_len; i++) - { - dp_physics_tick_ball(&cur->balls[i], delta, &next->balls[i]); - } - - next->tick = cur->tick + 1; - return 0; -} |
