/apps/demos/scenery: more than almost

This commit is contained in:
veclavtalica 2025-03-04 09:45:49 +03:00
parent 32675c012c
commit f90b973d86

View File

@ -59,9 +59,9 @@ static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
#define VEHICLE_HEIGHT 1.3f #define VEHICLE_HEIGHT 1.3f
/* spring constant */ /* spring constant */
#define VEHICLE_SPRING_K 20000.0f #define VEHICLE_SPRING_K 20000.0f
#define VEHICLE_SPRING_GK 60000.0f #define VEHICLE_SPRING_GK 80000.0f
/* damping constant */ /* damping constant */
#define VEHICLE_SPRING_C 500.0f #define VEHICLE_SPRING_C 800.0f
#define VEHICLE_SPRING_GC 100.0f #define VEHICLE_SPRING_GC 100.0f
#define VEHICLE_FRICTION_S 1000.0f #define VEHICLE_FRICTION_S 1000.0f
#define VEHICLE_FRICTION_K 100.0f #define VEHICLE_FRICTION_K 100.0f
@ -147,8 +147,8 @@ static void process_vehicle(SceneIngame *scn) {
/* wheel processing */ /* wheel processing */
if (i == 0 || i == 3 || i == 4 || i == 7) { if (i == 0 || i == 3 || i == 4 || i == 7) {
if (scn->camera_mode == 2 && input_action_pressed("player_forward")) { 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]); Vec3 dir = i == 0 ? vec3_sub(vbp[1], vbp[0]) : vec3_sub(vbp[2], vbp[3]);
Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 15000)); Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 6500));
} }
} }
@ -157,36 +157,46 @@ static void process_vehicle(SceneIngame *scn) {
float const xn = (h - p.y) * n.y; float const xn = (h - p.y) * n.y;
float const vn = vec3_dot(v, n); float const vn = vec3_dot(v, n);
Vec3 const Fn = vec3_scale(n, -VEHICLE_SPRING_GK * xn - VEHICLE_SPRING_GC * 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); Facc[i] = vec3_sub(Facc[i], Fn);
/* friction force, perpendicular to normal force */ /* friction force, perpendicular to normal force */
Vec3 const vo = vec3_sub(v, vec3_scale(n, vn)); /* TODO: is it right? aren't vn+vol should be = |v| */
Vec3 const von = vec3_norm(vec3_cross(n, vec3_cross(v, n)));
draw_line_3d(vbp[i], vec3_add(vbp[i], von), 1, (Color){255,255,0,255});
Vec3 const vo = vec3_scale(vec3_scale(von, vec3_length(v) - vn), -1);
float const vol = vec3_length(vo); float const vol = vec3_length(vo);
Vec3 Fo = vec3_sub(Facc[i], vec3_scale(n, vec3_dot(Facc[i], n))); Vec3 const Fon = vec3_norm(vec3_cross(n, vec3_cross(Facc[i], n)));
Vec3 Fo = vec3_scale(vec3_scale(Fon, vec3_length(Facc[i]) - vec3_dot(Facc[i], n)), -1);
draw_line_3d(vbp[i], vec3_add(vbp[i], Fon), 1, (Color){255,255,0,255});
/* portion of total force along the surface */ /* portion of total force along the surface */
float const Fol = vec3_length(Fo); float const Fol = vec3_length(Fo);
float const fkxn = VEHICLE_FRICTION_K * xn; float const fkxn = VEHICLE_FRICTION_K * xn;
/* at rest, might want to start moving */ /* at rest, might want to start moving */
if (fabsf(0.0f - vol) <= 0.0001f) { if (fabsf(0.0f - vol) <= 0.0001f) {
/* cannot overcome static friction, force along the surface is zeroed */ /* cannot overcome static friction, force along the surface is zeroed */
if (Fol <= VEHICLE_FRICTION_S * xn) Fo = (Vec3){0}; if (Fol <= VEHICLE_FRICTION_S * xn) {log_info("test 0"); Fo = vec3_scale(Fo, -1);}
/* resist the force by friction, while starting to move */ /* resist the force by friction, while starting to move */
else Fo = vec3_sub(Fo, vec3_scale(vec3_norm(Fo), fkxn)); else {log_info("test 1"); Fo = vec3_sub(Fo, vec3_scale(von, fkxn));}
/* not at rest, stop accelerating along the surface */ /* not at rest, stop accelerating along the surface */
} else if (vol + (Fol / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration) { } else if (vol + (Fol / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration * 2) {
vbv[i] = vec3_sub(vbv[i], vo); /* ugh ... */
vbv[i] = vec3_add(vbv[i], vo);
log_info("test 2");
/* just apply friction */ /* just apply friction */
} else { } else {
Fo = vec3_add(Fo, vec3_scale(vec3_norm(vo), -fkxn * 1000)); Fo = vec3_scale(von, -fkxn * 400);
} }
Facc[i] = vec3_add(Facc[i], Fo); Facc[i] = vec3_add(Facc[i], Fo);
} // Facc[i] = vec3_sub(Fo, Fn);
// Facc[i] = vec3_sub(Fo, Fn);
/* in air */
Vec3 vd = vec3_scale(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);
vbv[i] = vec3_add(vbv[i], vd); vbv[i] = vec3_add(vbv[i], vd);
vbp[i] = vec3_add(vbp[i], vec3_scale(vbv[i], ctx.frame_duration)); vbp[i] = vec3_add(vbp[i], vec3_scale(vbv[i], ctx.frame_duration));
} else {
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));
}
} }
} }
@ -208,7 +218,7 @@ static void process_vehicle_mode(State *state) {
yaws * pitchc, yaws * pitchc,
})); }));
Vec3 const orbit = vec3_sub(top_center, vec3_scale(looking_direction, 10)); Vec3 const orbit = vec3_sub(top_center, vec3_scale(looking_direction, 7.5));
draw_camera(orbit, looking_direction, (Vec3){0,1,0}, (float)M_PI_2 * 0.8f, 1, TERRAIN_RADIUS * sqrtf(3)); draw_camera(orbit, looking_direction, (Vec3){0,1,0}, (float)M_PI_2 * 0.8f, 1, TERRAIN_RADIUS * sqrtf(3));
@ -345,7 +355,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; 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; height += stb_perlin_noise3((float)x * TERRAIN_FREQUENCY / 10, (float)y * TERRAIN_FREQUENCY / 10, 0, 0, 0, 0) * 20 - 1;
heightmap[lx][ly] = height * 0.3f; heightmap[lx][ly] = height;
} }
} }
@ -481,11 +491,11 @@ static void ingame_tick(State *state) {
if (input_action_just_pressed("mouse_capture_toggle")) if (input_action_just_pressed("mouse_capture_toggle"))
scn->mouse_captured = !scn->mouse_captured; scn->mouse_captured = !scn->mouse_captured;
process_vehicle(scn);
ctx.mouse_capture = scn->mouse_captured; ctx.mouse_capture = scn->mouse_captured;
generate_terrain(scn); generate_terrain(scn);
process_vehicle(scn);
draw_terrain(scn); draw_terrain(scn);
draw_vehicle(scn); draw_vehicle(scn);