init commit

This commit is contained in:
Luxferre committed 2026-11-30 22:31:51 +02:00
commit 15ab48343a
187 files changed
+12644

No files matched your search

+450
View File
@@ -0,0 +1,450 @@
#include "sim/physics.hpp"
#include <algorithm>
#include <cmath>
namespace top_octane::sim {
namespace {
constexpr float kGravity = 13.5f;
constexpr float kWallBounce = 0.32f;
} // namespace
bool resolve_world_collisions(CraftState& craft, const assets::LevelData& level,
float& out_impact_speed, bool& out_scraping) {
out_impact_speed = 0.0f;
out_scraping = false;
if (craft.recovery_state != RecoveryState::Active) {
return false;
}
const VehicleSpec& spec = kVehicleSpecs[std::clamp(craft.vehicle_idx, 0, 5)];
const float radius = spec.collision_radius;
bool hit = false;
auto record_impact = [&](float vn_mag) {
hit = true;
out_impact_speed = std::max(out_impact_speed, vn_mag);
if (vn_mag < 4.5f) {
out_scraping = true;
}
};
// Map boundary clamping
constexpr float kMargin = 1.5f;
if (craft.curr_pose.pos.x < kMargin) {
craft.curr_pose.pos.x = kMargin;
if (craft.vel.x < 0.0f) {
record_impact(-craft.vel.x);
craft.vel.x = -craft.vel.x * kWallBounce;
} else {
hit = true;
}
} else if (craft.curr_pose.pos.x > assets::kMapWidth - 1.0f - kMargin) {
craft.curr_pose.pos.x = assets::kMapWidth - 1.0f - kMargin;
if (craft.vel.x > 0.0f) {
record_impact(craft.vel.x);
craft.vel.x = -craft.vel.x * kWallBounce;
} else {
hit = true;
}
}
if (craft.curr_pose.pos.z < kMargin) {
craft.curr_pose.pos.z = kMargin;
if (craft.vel.z < 0.0f) {
record_impact(-craft.vel.z);
craft.vel.z = -craft.vel.z * kWallBounce;
} else {
hit = true;
}
} else if (craft.curr_pose.pos.z > assets::kMapHeight - 1.0f - kMargin) {
craft.curr_pose.pos.z = assets::kMapHeight - 1.0f - kMargin;
if (craft.vel.z > 0.0f) {
record_impact(craft.vel.z);
craft.vel.z = -craft.vel.z * kWallBounce;
} else {
hit = true;
}
}
// Overhead gantry / tunnel ceiling clamp (before horizontal column pushout so crafts clear slot 1+)
float floor_y = 0.0f;
float ceil_y = 1e6f;
level.sample_surface_and_ceiling(craft.curr_pose.pos.x, craft.curr_pose.pos.z,
craft.curr_pose.pos.y, floor_y, ceil_y);
if (ceil_y < 1e5f) {
float max_craft_y = std::max(floor_y + 0.14f, ceil_y - 0.46f);
if (craft.curr_pose.pos.y > max_craft_y) {
craft.curr_pose.pos.y = max_craft_y;
if (craft.vel.y > 0.0f) {
craft.vel.y = 0.0f;
}
}
}
// 2-pass seam-free 3D Column AABB vs Craft Circle pushout
for (int pass = 0; pass < 2; ++pass) {
int cx = static_cast<int>(std::floor(craft.curr_pose.pos.x));
int cz = static_cast<int>(std::floor(craft.curr_pose.pos.z));
for (int nz = cz - 1; nz <= cz + 1; ++nz) {
for (int nx = cx - 1; nx <= cx + 1; ++nx) {
if (!level.is_cell_solid_at_y(nx, nz, craft.curr_pose.pos.y)) {
continue;
}
float min_x = static_cast<float>(nx);
float max_x = min_x + 1.0f;
float min_z = static_cast<float>(nz);
float max_z = min_z + 1.0f;
float closest_x = std::clamp(craft.curr_pose.pos.x, min_x, max_x);
float closest_z = std::clamp(craft.curr_pose.pos.z, min_z, max_z);
float dx = craft.curr_pose.pos.x - closest_x;
float dz = craft.curr_pose.pos.z - closest_z;
float dist_sq = dx * dx + dz * dz;
if (dist_sq >= radius * radius) {
continue;
}
Vec2 norm;
float pen = 0.0f;
bool inside_box = (dist_sq <= 1e-8f);
if (!inside_box) {
float dist = std::sqrt(dist_sq);
norm = Vec2(dx / dist, dz / dist);
pen = radius - dist;
} else {
float dL = std::abs(craft.curr_pose.pos.x - min_x);
float dR = std::abs(max_x - craft.curr_pose.pos.x);
float dT = std::abs(craft.curr_pose.pos.z - min_z);
float dB = std::abs(max_z - craft.curr_pose.pos.z);
float md = std::min({dL, dR, dT, dB});
if (md == dL) norm = Vec2(-1, 0);
else if (md == dR) norm = Vec2(1, 0);
else if (md == dT) norm = Vec2(0, -1);
else norm = Vec2(0, 1);
pen = radius + md;
}
// Suppress internal AABB seams between adjacent solid columns of compatible height
float h_here = level.cell_height_world(nx, nz);
if (std::abs(norm.x) > 0.15f) {
int adj_x = nx + (norm.x > 0.0f ? 1 : -1);
if (level.is_cell_solid_at_y(adj_x, nz, craft.curr_pose.pos.y) &&
std::abs(level.cell_height_world(adj_x, nz) - h_here) < 0.35f) {
norm.x = 0.0f;
}
}
if (std::abs(norm.y) > 0.15f) {
int adj_z = nz + (norm.y > 0.0f ? 1 : -1);
if (level.is_cell_solid_at_y(nx, adj_z, craft.curr_pose.pos.y) &&
std::abs(level.cell_height_world(nx, adj_z) - h_here) < 0.35f) {
norm.y = 0.0f;
}
}
float nlen = std::sqrt(norm.x * norm.x + norm.y * norm.y);
if (nlen < 1e-4f) {
continue;
}
norm.x /= nlen;
norm.y /= nlen;
if (!inside_box) {
pen = radius - (dx * norm.x + dz * norm.y);
if (pen <= 0.0f) continue;
}
craft.curr_pose.pos.x += norm.x * pen;
craft.curr_pose.pos.z += norm.y * pen;
float vn = craft.vel.x * norm.x + craft.vel.z * norm.y;
if (vn < 0.0f) {
record_impact(-vn);
craft.vel.x -= (1.0f + kWallBounce) * vn * norm.x;
craft.vel.z -= (1.0f + kWallBounce) * vn * norm.y;
// Gentle tangential drag along wall without stalling slide speed
craft.vel.x *= 0.985f;
craft.vel.z *= 0.985f;
} else if (pen > 0.02f) {
hit = true;
}
}
}
}
// Steep cliff deflection (sheer cliffs have surf_n.y < 0.49; steepest Track 5 road is ~0.535)
if (!craft.airborne) {
Vec3 surf_n = level.sample_normal_world(craft.curr_pose.pos.x, craft.curr_pose.pos.z);
if (surf_n.y < 0.49f) {
float hn_len = std::sqrt(surf_n.x * surf_n.x + surf_n.z * surf_n.z);
if (hn_len > 1e-4f) {
Vec2 cliff_n(surf_n.x / hn_len, surf_n.z / hn_len);
float vn = craft.vel.x * cliff_n.x + craft.vel.z * cliff_n.y;
if (vn < 0.0f) {
float push = (0.49f - surf_n.y) * 0.28f;
craft.curr_pose.pos.x += cliff_n.x * push;
craft.curr_pose.pos.z += cliff_n.y * push;
craft.vel.x -= (1.0f + kWallBounce) * vn * cliff_n.x;
craft.vel.z -= (1.0f + kWallBounce) * vn * cliff_n.y;
record_impact(-vn * 0.85f);
}
}
}
}
return hit;
}
CraftStepFeedback step_craft_physics(CraftState& craft, const CraftInput& input,
const assets::LevelData& level, float dt) {
CraftStepFeedback fb;
craft.prev_pose = craft.curr_pose;
if (craft.recovery_state != RecoveryState::Active) {
craft.vel = Vec3(0, 0, 0);
craft.scrape_intensity = 0.0f;
return fb;
}
const VehicleSpec& spec = kVehicleSpecs[std::clamp(craft.vehicle_idx, 0, 5)];
if (craft.damage_flash > 0.0f) craft.damage_flash = std::max(0.0f, craft.damage_flash - dt);
if (craft.minigun_cd > 0.0f) craft.minigun_cd = std::max(0.0f, craft.minigun_cd - dt);
if (craft.missile_cd > 0.0f) craft.missile_cd = std::max(0.0f, craft.missile_cd - dt);
if (craft.boost_cooldown > 0.0f) craft.boost_cooldown = std::max(0.0f, craft.boost_cooldown - dt);
if (craft.refuel_sound_cd > 0.0f) craft.refuel_sound_cd = std::max(0.0f, craft.refuel_sound_cd - dt);
// Steering & banking
float steer = std::clamp(input.steer, -1.0f, 1.0f);
float air_turn_mult = craft.airborne ? 1.45f : 1.0f;
float target_yaw_rate = -steer * spec.turn_rate * air_turn_mult;
craft.yaw_rate = lerp(craft.yaw_rate, target_yaw_rate, std::min(1.0f, dt * 11.0f));
float prev_yaw = craft.curr_pose.yaw;
craft.curr_pose.yaw = wrap_angle(craft.curr_pose.yaw + craft.yaw_rate * dt);
if (craft.airborne) {
craft.air_spin_accum += std::abs(wrap_angle(craft.curr_pose.yaw - prev_yaw));
}
Vec3 fwd(std::sin(craft.curr_pose.yaw), 0.0f, std::cos(craft.curr_pose.yaw));
Vec3 right(-fwd.z, 0.0f, fwd.x); // local -X is right side of craft
// Fuel consumption & boost handling
bool has_fuel = craft.fuel > 0.05f;
if (!has_fuel) {
fb.out_of_fuel = true;
}
bool want_boost = input.boost && has_fuel && craft.boost_cooldown <= 0.0f;
if (want_boost) {
craft.boost_timer = std::min(craft.boost_timer + dt, 2.5f);
fb.boosting = true;
} else if (craft.boost_timer > 0.0f) {
craft.boost_timer = std::max(0.0f, craft.boost_timer - dt * 2.0f);
}
float throttle = has_fuel ? std::clamp(input.throttle, 0.0f, 1.0f) : 0.0f;
float brake = std::clamp(input.brake, 0.0f, 1.0f);
float boost_mult = 1.0f + 0.14f * static_cast<float>(craft.booster_level - 1);
float max_spd = spec.top_speed + (fb.boosting ? spec.boost_bonus * boost_mult : 0.0f);
float accel = spec.accel * (fb.boosting ? 1.65f * boost_mult : 1.0f);
// Fuel burn proportional to throttle + boost
float burn = (throttle * 0.48f + (fb.boosting ? 2.4f : 0.0f) + 0.06f) * dt;
if (has_fuel) {
float used = std::min(craft.fuel, burn);
craft.fuel -= used;
craft.fuel_used += used;
}
// Forward/reverse thrust and hover lateral grip
if (!craft.airborne) {
craft.vel += fwd * (throttle * accel * dt);
if (brake > 0.0f) {
float fwd_spd = craft.vel.dot(fwd);
if (fwd_spd > 0.8f) {
craft.vel -= fwd * (brake * spec.reverse_accel * 1.6f * dt);
} else {
craft.vel -= fwd * (brake * spec.reverse_accel * dt);
}
}
// Lateral hover grip suppresses excessive sideways slide while retaining drift feel
float lat_v = craft.vel.dot(right);
float lat_damp = std::exp(-spec.lateral_grip * dt);
craft.vel -= right * (lat_v * (1.0f - lat_damp));
// Aerodynamic drag
float drag = (throttle > 0.05f || fb.boosting) ? 0.55f : 1.65f;
float hspd = craft.speed_horizontal();
if (hspd > max_spd) {
float excess = hspd - max_spd;
float scale = (hspd - std::min(excess, hspd * dt * 3.5f)) / std::max(hspd, 1e-4f);
craft.vel.x *= scale;
craft.vel.z *= scale;
} else {
float d = std::exp(-drag * dt);
craft.vel.x *= d;
craft.vel.z *= d;
}
} else {
// Gentle air control
craft.vel += fwd * (throttle * accel * 0.25f * dt);
}
// Sub-stepped integration + column/wall collision (prevents tunneling at top speed)
constexpr int kSubSteps = 2;
float sub_dt = dt / static_cast<float>(kSubSteps);
for (int s = 0; s < kSubSteps; ++s) {
craft.curr_pose.pos.x += craft.vel.x * sub_dt;
craft.curr_pose.pos.z += craft.vel.z * sub_dt;
// Vertical hover cushion vs gravity (supports elevated columns and overhead gantries)
float floor_y = 0.0f;
float ceil_y = 1e6f;
level.sample_surface_and_ceiling(craft.curr_pose.pos.x, craft.curr_pose.pos.z,
craft.curr_pose.pos.y, floor_y, ceil_y);
float target_y = floor_y + spec.hover_offset;
if (craft.curr_pose.pos.y <= target_y + 0.08f && craft.vel.y <= 3.2f) {
// Landed or hovering smoothly
if (craft.airborne) {
float deg = rad_to_deg(craft.air_spin_accum);
if (deg >= 660.0f) fb.stunt_degrees = 720;
else if (deg >= 310.0f) fb.stunt_degrees = 360;
else if (deg >= 145.0f) fb.stunt_degrees = 180;
craft.airborne = false;
craft.airtime = 0.0f;
craft.air_spin_accum = 0.0f;
}
float y_err = target_y - craft.curr_pose.pos.y;
craft.vel.y += (y_err * 95.0f - craft.vel.y * 14.0f) * sub_dt;
craft.curr_pose.pos.y += craft.vel.y * sub_dt;
// Launch into air when cresting a hill fast!
float next_floor = 0.0f;
float next_ceil = 1e6f;
level.sample_surface_and_ceiling(
craft.curr_pose.pos.x + craft.vel.x * sub_dt * 2.0f,
craft.curr_pose.pos.z + craft.vel.z * sub_dt * 2.0f,
craft.curr_pose.pos.y, next_floor, next_ceil);
float ground_drop = (floor_y - next_floor) / std::max(sub_dt * 2.0f, 1e-4f);
if (ceil_y > 1e5f && ground_drop > 3.5f && craft.speed_horizontal() > 10.0f) {
craft.airborne = true;
craft.vel.y = std::max(craft.vel.y, 2.2f);
} else if (craft.curr_pose.pos.y < floor_y + 0.14f) {
craft.curr_pose.pos.y = floor_y + 0.14f;
if (craft.vel.y < 0.0f) craft.vel.y = 0.0f;
}
} else {
// Airborne ballistic motion
if (!craft.airborne) {
craft.airborne = true;
craft.air_spin_accum = 0.0f;
}
craft.airtime += sub_dt;
craft.vel.y -= kGravity * sub_dt;
craft.curr_pose.pos.y += craft.vel.y * sub_dt;
if (craft.curr_pose.pos.y <= target_y) {
float deg = rad_to_deg(craft.air_spin_accum);
if (deg >= 660.0f) fb.stunt_degrees = 720;
else if (deg >= 310.0f) fb.stunt_degrees = 360;
else if (deg >= 145.0f) fb.stunt_degrees = 180;
craft.curr_pose.pos.y = target_y;
craft.vel.y = std::max(0.0f, -craft.vel.y * 0.15f);
craft.airborne = false;
craft.airtime = 0.0f;
craft.air_spin_accum = 0.0f;
}
}
// Banked slope acceleration on tilted track surfaces
if (!craft.airborne) {
Vec3 sub_n = level.sample_normal_world(craft.curr_pose.pos.x, craft.curr_pose.pos.z);
if (sub_n.y >= 0.49f && sub_n.y < 0.78f) {
craft.vel.x += sub_n.x * 5.0f * sub_dt;
craft.vel.z += sub_n.z * 5.0f * sub_dt;
}
}
float sub_impact = 0.0f;
bool sub_scraping = false;
if (resolve_world_collisions(craft, level, sub_impact, sub_scraping)) {
fb.collided_wall = true;
fb.impact_speed = std::max(fb.impact_speed, sub_impact);
fb.scraping |= sub_scraping;
}
}
// Smooth continuous scrape intensity for metallic wall-rubbing audio feedback
float target_scrape = fb.scraping ? std::clamp(craft.speed_horizontal() / spec.top_speed, 0.25f, 1.0f) : 0.0f;
craft.scrape_intensity = lerp(craft.scrape_intensity, target_scrape, std::min(1.0f, dt * 16.0f));
// Visual pitch & roll banking from terrain slope + steering
Vec3 surf_n = level.sample_normal_world(craft.curr_pose.pos.x, craft.curr_pose.pos.z);
float slope_pitch = -std::asin(std::clamp(surf_n.dot(fwd), -0.7f, 0.7f));
float speed_ratio = std::clamp(craft.speed_horizontal() / spec.top_speed, 0.0f, 1.2f);
float target_roll = steer * 0.34f * speed_ratio;
float target_pitch = (craft.airborne ? std::clamp(-craft.vel.y * 0.04f, -0.35f, 0.35f) : slope_pitch) +
(fb.boosting ? -0.06f : 0.0f);
craft.curr_pose.pitch = lerp(craft.curr_pose.pitch, target_pitch, std::min(1.0f, dt * 10.0f));
craft.curr_pose.roll = lerp(craft.curr_pose.roll, target_roll, std::min(1.0f, dt * 10.0f));
return fb;
}
bool resolve_craft_pair_collision(CraftState& a, CraftState& b, float& out_rel_speed) {
if (a.recovery_state != RecoveryState::Active || b.recovery_state != RecoveryState::Active) {
return false;
}
Vec3 d = b.curr_pose.pos - a.curr_pose.pos;
if (std::abs(d.y) > 1.2f) return false;
const VehicleSpec& sa = kVehicleSpecs[std::clamp(a.vehicle_idx, 0, 5)];
const VehicleSpec& sb = kVehicleSpecs[std::clamp(b.vehicle_idx, 0, 5)];
float min_sep = sa.collision_radius + sb.collision_radius;
float dist_sq = d.x * d.x + d.z * d.z;
if (dist_sq >= min_sep * min_sep) return false;
float dist = std::sqrt(std::max(dist_sq, 1e-6f));
Vec2 n = (dist > 1e-4f) ? Vec2(d.x / dist, d.z / dist) : Vec2(1.0f, 0.0f);
float pen = (min_sep - dist) * 0.5f;
float ma = sa.mass;
float mb = sb.mass;
float inv_sum = 1.0f / (ma + mb);
a.curr_pose.pos.x -= n.x * pen * (mb * inv_sum) * 2.0f;
a.curr_pose.pos.z -= n.y * pen * (mb * inv_sum) * 2.0f;
b.curr_pose.pos.x += n.x * pen * (ma * inv_sum) * 2.0f;
b.curr_pose.pos.z += n.y * pen * (ma * inv_sum) * 2.0f;
float rvx = b.vel.x - a.vel.x;
float rvz = b.vel.z - a.vel.z;
float rel_n = rvx * n.x + rvz * n.y;
out_rel_speed = std::abs(rel_n);
if (rel_n < 0.0f) {
float j = -(1.28f) * rel_n * inv_sum;
a.vel.x -= j * mb * n.x;
a.vel.z -= j * mb * n.y;
b.vel.x += j * ma * n.x;
b.vel.z += j * ma * n.y;
}
return true;
}
CraftPose interpolate_pose(const CraftState& craft, float alpha) {
CraftPose out;
out.pos = lerp(craft.prev_pose.pos, craft.curr_pose.pos, alpha);
out.yaw = lerp_angle(craft.prev_pose.yaw, craft.curr_pose.yaw, alpha);
out.pitch = lerp(craft.prev_pose.pitch, craft.curr_pose.pitch, alpha);
out.roll = lerp(craft.prev_pose.roll, craft.curr_pose.roll, alpha);
return out;
}
} // namespace top_octane::sim