prosperon/source/engine/warp.c

64 lines
1.4 KiB
C
Raw Normal View History

2024-01-04 08:14:37 -06:00
#include "warp.h"
#include "stb_ds.h"
#include "log.h"
static warp_gravity **warps = NULL;
warp_damp *warp_damp_make()
{
warp_damp *d = calloc(sizeof(*d),1);
return d;
}
void warp_damp_free(warp_damp *d) { free(d); }
2024-01-04 08:14:37 -06:00
warp_gravity *warp_gravity_make()
{
2024-01-04 09:46:28 -06:00
warp_gravity *n = calloc(sizeof(*n),1);
2024-01-04 08:14:37 -06:00
n->strength = 9.8;
n->t.scale = (HMM_Vec3){0,-1,0};
n->planar_force = (HMM_Vec3){0,-1,0};
2024-01-04 08:14:37 -06:00
arrput(warps, n);
return n;
}
2024-02-23 16:05:30 -06:00
void warp_gravity_free(warp_gravity *n) {
for (int i = 0; i < arrlen(warps); i++) {
if (warps[i] == n) {
arrdelswap(warps, i);
break;
}
}
free(n);
}
2024-01-14 10:24:31 -06:00
HMM_Vec3 warp_damp_force(warp_damp *d, HMM_Vec3 pos, HMM_Vec3 vel)
2024-01-04 08:14:37 -06:00
{
2024-01-14 10:24:31 -06:00
return HMM_MulV3(vel, d->damp);
}
2024-01-04 08:14:37 -06:00
2024-01-14 10:24:31 -06:00
HMM_Vec3 warp_gravity_force(warp_gravity *g, HMM_Vec3 pos)
{
2024-01-04 08:14:37 -06:00
HMM_Vec3 f = (HMM_Vec3){0,0,0};
if (g->strength == 0) return f;
if (g->spherical) {
HMM_Vec3 dir = HMM_SubV3(g->t.pos, pos);
float len = HMM_LenV3(dir);
if (len == 0) return f;
HMM_Vec3 norm = HMM_NormV3(HMM_SubV3(g->t.pos, pos));
return HMM_MulV3F(norm,g->strength);
} else {
return HMM_MulV3F(g->planar_force, g->strength);
2024-01-04 08:14:37 -06:00
}
}
HMM_Vec3 warp_force(HMM_Vec3 pos, warpmask mask)
{
HMM_Vec3 f = (HMM_Vec3){0,0,0};
for (int i = 0; i < arrlen(warps); i++) {
2024-01-14 10:24:31 -06:00
if (!(mask & warps[i]->mask)) continue;
2024-01-04 08:14:37 -06:00
f = HMM_AddV3(f, warp_gravity_force(warps[i], pos));
}
return f;
}