Last active
July 28, 2020 15:18
-
-
Save jes/53bf05cb82207087ee1f2b7964ac06b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the tyre squeal model from this clip: https://youtu.be/Likwgc5s_T0 | |
# You need an "AudioStreamPlayer3D" at the position of each wheel, and then run | |
# this function from _process_physics, with each wheel node passed | |
func tyre_squeal(tyre): | |
# increase squeal amplitude with road speed | |
var speed_factor = 1 | |
if (get_speed_kph() < 100): | |
speed_factor = 0.5 + 0.5 * (get_speed_kph()/100) | |
# increase squeal amplitude with a guess at the weight on this corner... | |
var SUSP_MIN = 0.08 | |
var SUSP_MAX = 0.10 | |
var susp_pos = (tyre.translation.y - SUSP_MIN) / (SUSP_MAX - SUSP_MIN) | |
var squeal_rate = clamp(susp_pos, 0.0, 1.0) * slip_ratio * speed_factor | |
var audio_node = tyre.get_node("AudioStreamPlayer3D") | |
var old_db = audio_node.unit_db | |
var new_db = -80 | |
if (squeal_rate > 0.01): | |
if (!audio_node.playing): | |
audio_node.playing = true | |
audio_node.pitch_scale = 1.1 - squeal_rate * 0.6 | |
new_db = 10*log(squeal_rate) # note log(squeal_rate) is always negative | |
else: | |
audio_node.playing = true | |
audio_node.unit_db = old_db * 0.95 + new_db * 0.05 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment