/apps/demos/scenery: ...almost...🚬

This commit is contained in:
veclavtalica 2025-03-04 03:24:20 +03:00
parent a97515e948
commit 32675c012c

View File

@ -53,14 +53,16 @@ static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
/* else if length(v(o) + (F(o)/m)*t) <= (f(k)*x(n)*t), v(o) = 0 */
/* else, F = -unit(v(o)*f(k)*x(n)) */
#define VEHICLE_MASS 150.0f
#define VEHICLE_MASS 200.0f
#define VEHICLE_LENGTH 3.0f
#define VEHICLE_WIDTH 1.7f
#define VEHICLE_HEIGHT 1.3f
/* spring constant */
#define VEHICLE_SPRING_K 80000.0f
#define VEHICLE_SPRING_K 20000.0f
#define VEHICLE_SPRING_GK 60000.0f
/* damping constant */
#define VEHICLE_SPRING_C 500.0f
#define VEHICLE_SPRING_GC 100.0f
#define VEHICLE_FRICTION_S 1000.0f
#define VEHICLE_FRICTION_K 100.0f
@ -143,10 +145,10 @@ static void process_vehicle(SceneIngame *scn) {
float const h = height_at(scn, (Vec2){ p.x, p.z });
if (h >= p.y) {
/* wheel processing */
if (i == 0 || i == 3) {
if (i == 0 || i == 3 || i == 4 || i == 7) {
if (scn->camera_mode == 2 && input_action_pressed("player_forward")) {
Vec3 const dir = i == 0 ? vec3_sub(vbp[0], vbp[1]) : vec3_sub(vbp[3], vbp[2]);
Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 18000));
Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 15000));
}
}
@ -154,7 +156,9 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const n = normal_at(scn, (Vec2){ p.x, p.z });
float const xn = (h - p.y) * n.y;
float const vn = vec3_dot(v, n);
Vec3 const Fn = vec3_scale(n, -VEHICLE_SPRING_K * xn - VEHICLE_SPRING_C * vn);
Vec3 const Fn = vec3_scale(n, -VEHICLE_SPRING_GK * xn - VEHICLE_SPRING_GC * vn);
draw_line_3d(vbp[i], vec3_sub(vbp[i], Fn), 1, (Color){255,255,0,255});
Facc[i] = vec3_sub(Facc[i], Fn);
/* friction force, perpendicular to normal force */
Vec3 const vo = vec3_sub(v, vec3_scale(n, vn));
@ -162,7 +166,7 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 Fo = vec3_sub(Facc[i], vec3_scale(n, vec3_dot(Facc[i], n)));
/* portion of total force along the surface */
float const Fol = vec3_length(Fo);
float const fkxn = VEHICLE_FRICTION_K * xn * 50;
float const fkxn = VEHICLE_FRICTION_K * xn;
/* at rest, might want to start moving */
if (fabsf(0.0f - vol) <= 0.0001f) {
/* cannot overcome static friction, force along the surface is zeroed */
@ -171,27 +175,19 @@ static void process_vehicle(SceneIngame *scn) {
else Fo = vec3_sub(Fo, vec3_scale(vec3_norm(Fo), fkxn));
/* not at rest, stop accelerating along the surface */
} else if (vol + (Fol / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration) {
log_info("test 1");
// vbv[i] = vec3_sub(vbp[i], vo);
vbv[i] = (Vec3){0};
vbv[i] = vec3_sub(vbv[i], vo);
/* just apply friction */
} else {
log_info("test 2, depth: %f", xn);
Fo = vec3_add(Fo, vec3_scale(vec3_norm(vo), -fkxn * 100));
Fo = vec3_add(Fo, vec3_scale(vec3_norm(vo), -fkxn * 1000));
}
Facc[i] = vec3_add(Facc[i], Fo);
}
/* total force, after ground interaction */
Vec3 const Ft = vec3_add(vec3_scale(Fn, -1), Fo);
Vec3 vd = vec3_scale(vec3_scale(Ft, (1.0f / VEHICLE_MASS)), ctx.frame_duration);
vbv[i] = vec3_add(vbv[i], vd);
vbp[i] = vec3_add(vbp[i], vec3_scale(vbv[i], ctx.frame_duration));
} else {
/* in air */
Vec3 vd = vec3_scale(vec3_scale(Facc[i], (1.0f / VEHICLE_MASS)), ctx.frame_duration);
vbv[i] = vec3_add(vbv[i], vd);
vbp[i] = vec3_add(vbp[i], vec3_scale(vbv[i], ctx.frame_duration));
}
}
}
@ -260,10 +256,10 @@ static Vec3 normal_at(SceneIngame *scn, Vec2 position) {
float const height1 = heightmap[x + 1][y];
float const height2 = heightmap[x][y + 1];
Vec3 const a = { .x = 1, .y = height0 - height1, .z = 0 };
Vec3 const a = { .x = -1, .y = height0 - height1, .z = 0 };
Vec3 const b = { .x = 0, .y = height0 - height2, .z = -1 };
return vec3_norm(vec3_cross(a, b));
return vec3_scale(vec3_norm(vec3_cross(a, b)), -1);
}
/* TODO: don't operate on triangles, instead interpolate on quads */
@ -349,7 +345,7 @@ static void generate_terrain(SceneIngame *scn) {
float height = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 1;
height += stb_perlin_noise3((float)x * TERRAIN_FREQUENCY / 10, (float)y * TERRAIN_FREQUENCY / 10, 0, 0, 0, 0) * 20 - 1;
heightmap[lx][ly] = height;
heightmap[lx][ly] = height * 0.3f;
}
}