From 31330b9cfe61bd6248184067d9e8c2774fd16e13 Mon Sep 17 00:00:00 2001 From: tovjemam Date: Sun, 14 Jun 2026 00:51:16 +0200 Subject: [PATCH] Make vehicle crash sounds based on velocity changes --- src/game/vehicle.cpp | 49 ++++++++++++++++++++++++++++---------------- src/game/vehicle.hpp | 1 + 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/game/vehicle.cpp b/src/game/vehicle.cpp index fc21193..2e56c2a 100644 --- a/src/game/vehicle.cpp +++ b/src/game/vehicle.cpp @@ -364,30 +364,43 @@ void game::Vehicle::UpdateCrash() } else { - if (crash_intensity_ > 1000.0f) + if (physics_) { - float volume = RandomFloat(0.9f, 1.2f); - float pitch = RandomFloat(1.0f, 1.3f); + auto bt_vel = physics_->GetBtBody().getLinearVelocity(); + glm::vec3 velocity(bt_vel.x(), bt_vel.y(), bt_vel.z()); + float diff = glm::distance(velocity, prev_velocity_); + prev_velocity_ = velocity; - if (crash_intensity_ > 12000.0f) + float snd_crash_intensity = glm::max(diff * 500.0f, crash_intensity_); + + if (snd_crash_intensity > 1000.0f) { - volume *= 1.7f; - pitch *= 0.8f; - } - if (crash_intensity_ > 4000.0f) - { - volume *= 1.3f; - pitch *= 0.8f; - } - else - { - volume *= 0.8f; - pitch *= 1.2f; + float volume = RandomFloat(0.9f, 1.2f); + float pitch = RandomFloat(1.0f, 1.3f); + + if (snd_crash_intensity > 12000.0f) + { + volume *= 1.7f; + pitch *= 0.8f; + } + if (snd_crash_intensity > 4000.0f) + { + volume *= 1.3f; + pitch *= 0.8f; + } + else + { + volume *= 0.8f; + pitch *= 1.2f; + } + + PlaySound("crash", volume, pitch); + no_crash_frames_ = 8 + rand() % 5; } - PlaySound("crash", volume, pitch); - no_crash_frames_ = 7 + rand() % 10; } + + } crash_intensity_ = 0.0f; diff --git a/src/game/vehicle.hpp b/src/game/vehicle.hpp index e551f5e..36a729b 100644 --- a/src/game/vehicle.hpp +++ b/src/game/vehicle.hpp @@ -134,6 +134,7 @@ private: float crash_intensity_ = 0.0f; size_t no_crash_frames_ = 0; + glm::vec3 prev_velocity_ = glm::vec3(0.0f); std::unique_ptr deformgrid_;