add tire trails

This commit is contained in:
interfisch 2024-11-10 20:49:52 +01:00
parent cd87a28274
commit e7ca0c8c64
4 changed files with 56 additions and 6 deletions

View file

@ -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

14
scenes/tire_trail.tscn Normal file
View file

@ -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")

View file

@ -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)

23
scripts/tiretrail.gd Normal file
View file

@ -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