/apps/demos/scenery: no drifting :(

This commit is contained in:
veclavtalica 2025-03-07 00:51:07 +03:00
parent 787977b747
commit 5df80addeb

View File

@ -58,13 +58,15 @@ static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
#define VEHICLE_WIDTH 1.7f
#define VEHICLE_HEIGHT 1.3f
/* spring constant */
#define VEHICLE_SPRING_K 20000.0f
#define VEHICLE_SPRING_GK 80000.0f
#define VEHICLE_SPRING_K 22000.0f
#define VEHICLE_SPRING_K_SHOCK 18000.0f
#define VEHICLE_SPRING_GK 70000.0f
/* damping constant */
#define VEHICLE_SPRING_C 800.0f
#define VEHICLE_SPRING_C_SHOCK 500.0f
#define VEHICLE_SPRING_GC 100.0f
#define VEHICLE_FRICTION_S 1000.0f
#define VEHICLE_FRICTION_K 100.0f
#define VEHICLE_FRICTION_K 110.0f
#define VEHICLE_FRICTION_V 4000.0f
/* TODO: shock springs, that are more loose, which are used to simulate the wheels */
@ -122,13 +124,23 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 Facc[8] = {0};
/* steering */
if (input_action_pressed("player_left"))
bool steered = false;
if (input_action_pressed("player_left")) {
vehicle_turning_extend -= vehicle_turning_speed;
steered = true;
}
if (input_action_pressed("player_right"))
if (input_action_pressed("player_right")) {
vehicle_turning_extend += vehicle_turning_speed;
steered = true;
}
if (!steered)
vehicle_turning_extend -= copysignf(vehicle_turning_speed * 0.9f, vehicle_turning_extend);
vehicle_turning_extend = clampf(vehicle_turning_extend, -vehicle_turning_extend_limit, vehicle_turning_extend_limit);
if (fabsf(vehicle_turning_extend) <= 0.11f)
vehicle_turning_extend = 0;
Vec3 const Fg = { .y = -VEHICLE_MASS * G_CONST };
for (size_t i = 0; i < 8; ++i)
@ -146,7 +158,9 @@ static void process_vehicle(SceneIngame *scn) {
float const lar = vec3_length(vec3_sub(vbpi[vbs[i][1]], vbpi[vbs[i][0]]));
float const x = vec3_length(pd) - lar;
float const v = vec3_dot(vec3_sub(v1, v0), pn);
Vec3 const Fs = vec3_scale(pn, -VEHICLE_SPRING_K * x - VEHICLE_SPRING_C * v);
float const spring_k = i == 2 | i == 5 || i == 8 || i == 11 ? VEHICLE_SPRING_K_SHOCK : VEHICLE_SPRING_K;
float const spring_c = i == 2 | i == 5 || i == 8 || i == 11 ? VEHICLE_SPRING_C_SHOCK : VEHICLE_SPRING_C;
Vec3 const Fs = vec3_scale(pn, -spring_k * x - spring_c * v);
Facc[vbs[i][0]] = vec3_sub(Facc[vbs[i][0]], Fs);
Facc[vbs[i][1]] = vec3_add(Facc[vbs[i][1]], Fs);
}
@ -156,16 +170,16 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const p = vbp[i];
Vec3 const v = vbv[i];
float const h = height_at(scn, (Vec2){ p.x, p.z });
Vec3 const fwd = vec3_norm(vec3_sub(vbp[1], vbp[0]));
if (h >= p.y) {
/* back wheel processing: acceleration */
if (i == 0 || i == 3 || i == 4 || i == 7) {
if (i == 0 || i == 3) {
float scale = 0;
if (scn->camera_mode == 2 && input_action_pressed("player_forward"))
scale += 1;
if (scn->camera_mode == 2 && input_action_pressed("player_backward"))
scale -= 1;
Vec3 const dir = vec3_sub(vbp[1], vbp[0]);
Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 6500 * scale));
Facc[i] = vec3_add(Facc[i], vec3_scale(fwd, 6500 * scale));
}
/* normal force, for displacement */
@ -194,15 +208,22 @@ static void process_vehicle(SceneIngame *scn) {
/* not at rest, stop accelerating along the surface */
} else if (vol + (Fol / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration * 2) {
/* ugh ... */
vbv[i] = vec3_add(vbv[i], vo);
vbv[i] = vec3_add(v, vo);
/* just apply friction */
} else {
Fo = vec3_scale(von, -fkxn * 400);
}
Facc[i] = vec3_add(Facc[i], Fo);
/* rear wheel friction */
if (i == 0 || i == 3) {
Vec3 const pn = vec3_cross(fwd, n);
Vec3 const Fp = vec3_scale(pn, vec3_dot(v, pn) * -VEHICLE_FRICTION_V);
Facc[i] = vec3_add(Facc[i], Fp);
}
/* front wheel processing */
if (i == 1 || i == 5 || i == 2 || i == 6) {
if (i == 1 || i == 2) {
/* steering influences "center of turning", which is a point */
/* laying on line defined by rear axle */
/* front arms are rotated to be perpendicular to center of turning, */
@ -226,9 +247,9 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const n = vec3_norm(vec3_cross(a, b));
Vec3 const wheel = vec3_norm(vec3_rotate(arm, -arm_angle, n));
Vec3 const p = vec3_norm(vec3_cross(wheel, n));
draw_line_3d(r, vec3_add(r, wheel), 1, (Color){0,255,255,255});
draw_line_3d(r, vec3_add(r, p), 1, (Color){0,255,255,255});
Vec3 const Fp = vec3_scale(p, vec3_dot(vbv[i], p) * -VEHICLE_FRICTION_V);
Vec3 const Fp = vec3_scale(p, vec3_dot(v, p) * -VEHICLE_FRICTION_V);
Facc[i] = vec3_add(Facc[i], Fp);
}
}