/apps/demos/scenery: box is almost coherent...

This commit is contained in:
veclavtalica 2025-03-02 03:37:02 +03:00
parent 55829a1bef
commit 4e5ff9433c

View File

@ -19,7 +19,7 @@
#define PLAYER_HEIGHT 0.6f #define PLAYER_HEIGHT 0.6f
#define TREE_DENSITY 0.03f #define TREE_DENSITY 0.03f
#define G_CONST 0.1f #define G_CONST 10.0f
/* TODO: pregenerate grid of levels of detail */ /* TODO: pregenerate grid of levels of detail */
static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE]; static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
@ -58,11 +58,11 @@ static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
#define VEHICLE_WIDTH 1.7f #define VEHICLE_WIDTH 1.7f
#define VEHICLE_HEIGHT 1.3f #define VEHICLE_HEIGHT 1.3f
/* spring constant */ /* spring constant */
#define VEHICLE_SPRING_K 400.0f #define VEHICLE_SPRING_K 50000.0f
/* damping constant */ /* damping constant */
#define VEHICLE_SPRING_C 20.0f #define VEHICLE_SPRING_C 200.0f
#define VEHICLE_FRICTION_S 1000000.0f #define VEHICLE_FRICTION_S 50000.0f
#define VEHICLE_FRICTION_K 1000000.0f #define VEHICLE_FRICTION_K 700.0f
/* initial, ideal corner positions */ /* initial, ideal corner positions */
static const Vec3 vbpi[8] = { static const Vec3 vbpi[8] = {
@ -119,7 +119,7 @@ static void process_vehicle(SceneIngame *scn) {
Facc[i] = vec3_add(Facc[i], Fg); Facc[i] = vec3_add(Facc[i], Fg);
/* apply springs */ /* apply springs */
for (size_t i = 0; i < 24; ++i) { for (size_t i = 0; i < 28; ++i) {
Vec3 const p0 = vbp[vbs[i][0]]; Vec3 const p0 = vbp[vbs[i][0]];
Vec3 const p1 = vbp[vbs[i][1]]; Vec3 const p1 = vbp[vbs[i][1]];
Vec3 const v0 = vbv[vbs[i][0]]; Vec3 const v0 = vbv[vbs[i][0]];
@ -140,7 +140,7 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const p = vbp[i]; Vec3 const p = vbp[i];
Vec3 const v = vbv[i]; Vec3 const v = vbv[i];
float const h = height_at(scn, (Vec2){ p.x, p.z }); float const h = height_at(scn, (Vec2){ p.x, p.z });
if (h > p.y) { if (h >= p.y) {
/* displacement force */ /* displacement force */
Vec3 const n = normal_at(scn, (Vec2){ p.x, p.z }); Vec3 const n = normal_at(scn, (Vec2){ p.x, p.z });
float const xn = (h - p.y) * n.y; float const xn = (h - p.y) * n.y;
@ -151,22 +151,25 @@ static void process_vehicle(SceneIngame *scn) {
/* friction force, perpendicular to displacement */ /* friction force, perpendicular to displacement */
/* portions aligned to surface normal */ /* portions aligned to surface normal */
Vec3 const vov = vec3_sub(v, vec3_scale(n, vn)); Vec3 const vov = vec3_sub(v, vec3_scale(n, vn));
draw_line_3d(vbp[i], vec3_add(vbp[i], vec3_scale(vov, 3)), 1, (Color){255, 0, 0, 255});
float const vo = vec3_length(vov); float const vo = vec3_length(vov);
Vec3 const Fov = vec3_sub(Facc[i], vec3_scale(n, vec3_dot(Facc[i], n))); Vec3 const Fov = vec3_sub(Facc[i], vec3_scale(n, vec3_dot(Facc[i], n)));
float const Fo = vec3_length(Fov); float const Fo = vec3_length(Fov);
float const fkxn = VEHICLE_FRICTION_K * xn; float const fkxn = VEHICLE_FRICTION_K * xn;
if (fabsf(0.0f - vo) <= 0.0001f) { if (fabsf(0.0f - vo) <= 0.001f) {
/* cannot overcome static friction, zero force along the surface */ /* cannot overcome static friction, zero force along the surface */
if (Fo <= VEHICLE_FRICTION_S * xn) { if (Fo <= VEHICLE_FRICTION_S * xn) {
Facc[i] = vec3_sub(Facc[i], Fov); // Facc[i] = vec3_sub(Facc[i], Fov);
Facc[i] = (Vec3){0};
} }
/* apply kinematic friction */ /* apply kinematic friction */
else { else {
Facc[i] = vec3_sub(Facc[i], vec3_scale(vec3_norm(Fov), fkxn)); Facc[i] = vec3_sub(Fov, vec3_scale(vec3_norm(Fov), fkxn));
} }
/* velocity with gain along the surface will not overcome */ /* velocity with gain along the surface will not overcome */
} else if (vo + (Fo / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration) { } else if (vec3_length(vec3_add(vov, vec3_scale(vec3_scale(Fov, (1.0f / VEHICLE_MASS)), ctx.frame_duration))) <= fkxn * ctx.frame_duration) {
vbv[i] = vec3_sub(vbv[i], vov); // vbv[i] = vec3_sub(vbv[i], vov);
vbv[i] = (Vec3){0};
} else { } else {
Facc[i] = vec3_sub(Facc[i], vec3_scale(vec3_norm(vov), fkxn)); Facc[i] = vec3_sub(Facc[i], vec3_scale(vec3_norm(vov), fkxn));
} }
@ -175,10 +178,10 @@ static void process_vehicle(SceneIngame *scn) {
/* integrate forces and velocity */ /* integrate forces and velocity */
for (size_t i = 0; i < 8; ++i) { for (size_t i = 0; i < 8; ++i) {
Vec3 const vd = vec3_scale(Facc[i], (1.0f / VEHICLE_MASS) * ctx.frame_duration); Vec3 vd = vec3_scale(vec3_scale(Facc[i], (1.0f / VEHICLE_MASS)), ctx.frame_duration);
if (vec3_length(vd) <= 0.02f) vd = (Vec3){0}; /* TODO: dirty hack... */
vbv[i] = vec3_add(vbv[i], vd); vbv[i] = vec3_add(vbv[i], vd);
vbp[i] = vec3_add(vbp[i], vbv[i]); vbp[i] = vec3_add(vbp[i], vec3_scale(vbv[i], ctx.frame_duration));
vbv[i] = vec3_scale(vbv[i], 0.99f);
} }
} }