From e7ca0c8c641a25b72dd7063dfddb301490feda49 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 10 Nov 2024 20:49:52 +0100 Subject: [PATCH] add tire trails --- scenes/car.tscn | 13 ++++++++++++- scenes/tire_trail.tscn | 14 ++++++++++++++ scripts/car.gd | 12 +++++++----- scripts/tiretrail.gd | 23 +++++++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 scenes/tire_trail.tscn create mode 100644 scripts/tiretrail.gd diff --git a/scenes/car.tscn b/scenes/car.tscn index 5ba4396..6840523 100644 --- a/scenes/car.tscn +++ b/scenes/car.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=47 format=3 uid="uid://0g7qqh7naniv"] +[gd_scene load_steps=48 format=3 uid="uid://0g7qqh7naniv"] [ext_resource type="Script" path="res://scripts/car_node.gd" id="1_0tin3"] [ext_resource type="Script" path="res://scripts/car.gd" id="1_i5tet"] [ext_resource type="Texture2D" uid="uid://mqdujngircok" path="res://sprites/car_features.png" id="3_ts6mm"] [ext_resource type="Texture2D" uid="uid://e5aeyl47wi8p" path="res://sprites/car_body.png" id="4_lps13"] +[ext_resource type="PackedScene" uid="uid://c3fdxjrpcgnnp" path="res://scenes/tire_trail.tscn" id="5_2bgdv"] [ext_resource type="Script" path="res://scripts/label_round.gd" id="5_vheit"] [ext_resource type="PackedScene" uid="uid://dl7r8s5sxyvlw" path="res://scenes/enginesound.tscn" id="6_v21se"] [ext_resource type="AudioStream" uid="uid://dq5rei2w1isu4" path="res://sounds/crash/Car_Crash_01.mp3" id="7_7erwn"] @@ -148,6 +149,16 @@ texture = ExtResource("4_lps13") rotation = -1.57079 shape = SubResource("CapsuleShape2D_bj1hp") +[node name="PositionFrontLeftTire" type="Node2D" parent="CharacterBody_Car"] +position = Vector2(10, -7) + +[node name="Trail" parent="CharacterBody_Car/PositionFrontLeftTire" instance=ExtResource("5_2bgdv")] + +[node name="PositionFrontRightTire" type="Node2D" parent="CharacterBody_Car"] +position = Vector2(10, 7) + +[node name="Trail" parent="CharacterBody_Car/PositionFrontRightTire" instance=ExtResource("5_2bgdv")] + [node name="RayCast_FL" type="RayCast2D" parent="CharacterBody_Car"] target_position = Vector2(256, -128) collision_mask = 2 diff --git a/scenes/tire_trail.tscn b/scenes/tire_trail.tscn new file mode 100644 index 0000000..cbb1ec9 --- /dev/null +++ b/scenes/tire_trail.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://c3fdxjrpcgnnp"] + +[ext_resource type="Script" path="res://scripts/tiretrail.gd" id="1_cvwoe"] + +[sub_resource type="Curve" id="Curve_ckg0w"] +_data = [Vector2(0, 0), 0.0, 3.77363, 0, 0, Vector2(1, 0), -4.04783, 0.0, 0, 0] +point_count = 2 + +[node name="Trail" type="Line2D"] +z_index = 9 +width = 4.0 +width_curve = SubResource("Curve_ckg0w") +default_color = Color(0, 0, 0, 1) +script = ExtResource("1_cvwoe") diff --git a/scripts/car.gd b/scripts/car.gd index c52e5d0..5e123af 100644 --- a/scripts/car.gd +++ b/scripts/car.gd @@ -202,7 +202,7 @@ func get_input(delta:float): if steer_direction<1: resetcar_steerangle=min(-resetcar_steerangle,+resetcar_steerangle) #calculate steering direction for next autoreset -var mind=1 + func calculate_steering(delta:float): var rear_wheel = position - transform.x *wheel_base/2.0 var front_wheel = position + transform.x *wheel_base/2.0 @@ -218,12 +218,14 @@ func calculate_steering(delta:float): var d = new_heading.dot(velocity.normalized()) if d > 0: - mind=min(d,mind) - if d==mind: - print("mind="+str(mind)) velocity = velocity.lerp(new_heading * velocity.length(), traction) - if d<0.80 and velocity.length()>slip_speed and applied_engine_power>engine_power/2: + if d<0.82 and velocity.length()>slip_speed and applied_engine_power>engine_power/2: sfx.sliding(velocity.length()) + $PositionFrontLeftTire/Trail.setSliding(true) + $PositionFrontRightTire/Trail.setSliding(true) + elif d>0.98: #keep trails a bit longer + $PositionFrontLeftTire/Trail.setSliding(false) + $PositionFrontRightTire/Trail.setSliding(false) elif d<0: velocity = - new_heading * min(velocity.length(),max_speed_reverse) diff --git a/scripts/tiretrail.gd b/scripts/tiretrail.gd new file mode 100644 index 0000000..9b36977 --- /dev/null +++ b/scripts/tiretrail.gd @@ -0,0 +1,23 @@ +extends Line2D + +var point +var isSliding=false +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + set_as_top_level(true) + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _physics_process(delta): + pass +func _process(delta: float) -> void: + if isSliding: + point = get_parent().global_position + add_point(point) + if points.size()>100: + remove_point(0) + +func setSliding(psliding:bool): + if not isSliding and psliding: #just started sliding + clear_points() + isSliding=psliding