From 479522d2922b1f51284aa0aa1f1d707911bbd95c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 8 Nov 2021 19:53:23 +0100 Subject: [PATCH] Add vehicle sounds and adjust RPM --- scenes/test_level.tscn | 1 - vehicles/buggy.gd | 44 ++++++++++++++++++++++++++++++++-------- vehicles/buggy.tscn | 14 ++++++++++--- vehicles/buggy_test.tscn | 1 - 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/scenes/test_level.tscn b/scenes/test_level.tscn index 65b4a90..18854bb 100644 --- a/scenes/test_level.tscn +++ b/scenes/test_level.tscn @@ -35,7 +35,6 @@ shape = SubResource( 3 ) [node name="buggy" parent="." instance=ExtResource( 1 )] transform = Transform( 1.00456, 0, 0, 0, 1.00456, 0, 0, 0, 1.00456, -38.0928, 1.38435, -0.0872202 ) mass = 250.0 -STEER_SPEED = 1.5 [node name="camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5.88022, 23.0639 ) diff --git a/vehicles/buggy.gd b/vehicles/buggy.gd index 2e6cfae..eb72c85 100644 --- a/vehicles/buggy.gd +++ b/vehicles/buggy.gd @@ -14,19 +14,28 @@ export (Curve) var steer_curve = null var steer_target = 0.0 var steer_angle = 0.0 -export (float) var MAX_ENGINE_FORCE = 50.0 +export (float) var MAX_ENGINE_FORCE = 85.0 export (float) var MAX_BRAKE_FORCE = 50.0 -export (float) var THROTTLE_POWER = 10000.0 +export (float) var THROTTLE_POWER = 6000.0 export (float) var MAX_RPM_LOSS_PS = 3000.0 -export (Array) var gear_ratios = [ 6, 5, 4, 3, 2, 1.0 ] -export (float) var reverse_ratio = -6.0 -export (float) var final_drive = 3.38 -export (float) var max_rpm = 7000 +export (Array) var gear_ratios = [ 3.4, 2.5, 2.0, 1.5, 1.25 ] +export (float) var reverse_ratio = -3 +export (float) var final_drive = 3.45 +export (float) var max_rpm = 3500 export (float) var min_rpm = 900 export (float) var gear_switch_time = 0.2 export (Curve) var power_curve = null + +var idle_sound_start: float = 0.5 +var idle_sound_end: float = 1 + +var throttle_sound_start: float = 2.5 +var throttle_sound_end: float = 3.5 + +var sound_time_left: float = 0.0 + var clutch_position: float = 0.0 var rpm = 0 var gear = 1 @@ -38,6 +47,9 @@ onready var flwheel: VehicleWheel = $front_left onready var rrwheel: VehicleWheel = $rear_right onready var rlwheel: VehicleWheel = $rear_left +onready var engine_sound_player: AudioStreamPlayer3D = $engine_sound_player +onready var engine_sound_playback: AudioStreamPlayback = $engine_sound_player.get_stream_playback() + var traction_wheels: Array func _ready(): @@ -45,6 +57,9 @@ func _ready(): if wheel.use_as_traction: traction_wheels.append(wheel) + _generate_engine_sound(0) + engine_sound_player.play() + func _get_gear_ratio(): if gear == 0: return 0 @@ -90,9 +105,9 @@ func _physics_process(delta: float): if gear != 0: rpm = lerp(rpm, transmission_rpm, delta * (1 - clutch_position)) - rpm = _lerp_rpm(rpm, min_rpm, delta, delta * clutch_position * 3) + rpm = _lerp_rpm(rpm, min_rpm, delta, delta * clutch_position) else: - rpm = _lerp_rpm(rpm, min_rpm, delta, delta * 3) + rpm = _lerp_rpm(rpm, min_rpm, delta, delta) if _has_traction(): rpm += throttle * delta * clutch_position * THROTTLE_POWER else: @@ -101,6 +116,8 @@ func _physics_process(delta: float): var rpm_factor = clamp(rpm / max_rpm, 0.0, 1.0) var power_factor = power_curve.interpolate_baked(rpm_factor) + _generate_engine_sound(rpm_factor) + # Transfer to transmission var transmission_input = power_factor * (1 - clutch_position) * _get_gear_ratio() @@ -114,7 +131,7 @@ func _physics_process(delta: float): rlwheel.brake = handbrake * MAX_BRAKE_FORCE / 2 var speed = wheel_rpm * 2.0 * PI * rrwheel.wheel_radius / 60.0 * 3600.0 / 1000.0 - $Info.text = "Gear: %d, KPH: %.0f, RPM: %.0f, TRPM: %.0f, Engine force: %.0f" % [ gear, speed, rpm, transmission_rpm, engine_force ] + $Info.text = "Gear: %d, KPH: %.0f, RPM: %.0f, WheelRPM: %.0f, FinalRPM: %.0f, TRPM: %.0f, Engine force: %.0f" % [ gear, speed, rpm, wheel_rpm, final_rpm, transmission_rpm, engine_force ] var steering_input = Input.get_action_strength("steer_left") - Input.get_action_strength("steer_right") if abs(steering_input) < 0.05: @@ -129,3 +146,12 @@ func _physics_process(delta: float): steer_angle = steering_input * lerp(max_steer_angle_rad, speed_steer_angle_rad, steer_speed_factor) steering = steer_angle + +func _generate_engine_sound(rpm_factor): + engine_sound_player.pitch_scale = 0.5 + rpm_factor + var to_fill = engine_sound_playback.get_frames_available() + var factor = rpm_factor + while to_fill > 0: + engine_sound_playback.push_frame(Vector2(1.0, 1.0) * factor) + factor += cos(factor) * sin(factor) + to_fill -= 1 diff --git a/vehicles/buggy.tscn b/vehicles/buggy.tscn index 914268d..152b750 100644 --- a/vehicles/buggy.tscn +++ b/vehicles/buggy.tscn @@ -1,17 +1,20 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://assets/basic_buggy.glb" type="PackedScene" id=1] [ext_resource path="res://vehicles/buggy.gd" type="Script" id=3] [sub_resource type="Curve" id=2] -_data = [ Vector2( 0, 0 ), 0.0, 0.0, 0, 0, Vector2( 0.910377, 0.990909 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), 0.0, 0.0, 0, 0 ] +_data = [ Vector2( 0, 0 ), 0.0, 0.0, 0, 0, Vector2( 0.259434, 0.463636 ), 0.0, 0.0, 0, 0, Vector2( 0.59434, 0.872727 ), 0.0, 0.0, 0, 0, Vector2( 0.900943, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), 0.0, 0.0, 0, 0 ] [sub_resource type="BoxShape" id=1] extents = Vector3( 0.521885, 0.334919, 1.78849 ) +[sub_resource type="AudioStreamGenerator" id=3] +mix_rate = 5500.0 + [node name="buggy" instance=ExtResource( 1 )] +mass = 150.0 script = ExtResource( 3 ) -STEER_SPEED = 1.5 power_curve = SubResource( 2 ) [node name="chassis" parent="." index="0"] @@ -57,3 +60,8 @@ margin_bottom = 14.0 __meta__ = { "_edit_use_anchors_": false } + +[node name="engine_sound_player" type="AudioStreamPlayer3D" parent="." index="8"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.14201, 0.968668 ) +stream = SubResource( 3 ) +unit_db = 14.0 diff --git a/vehicles/buggy_test.tscn b/vehicles/buggy_test.tscn index bbeb73a..a6f4ac9 100644 --- a/vehicles/buggy_test.tscn +++ b/vehicles/buggy_test.tscn @@ -8,7 +8,6 @@ data = PoolVector3Array( -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1 ) [node name="buggy" instance=ExtResource( 1 )] -STEER_SPEED = 1.5 [node name="Camera" type="Camera" parent="." index="7"] transform = Transform( -0.0903024, 0, -0.995914, 0, 1, 0, 0.995914, 0, -0.0903024, -3.4904, 1.22508, 0 )