From baf451d062acc110e791e73d28be9bdbf20a1eb3 Mon Sep 17 00:00:00 2001 From: perivesta <> Date: Sun, 9 Jul 2023 13:05:58 +0200 Subject: better level end ui and some cleanup --- Nodes/Level_test.tscn | 12 +----------- Nodes/Level_test_2.tscn | 12 +----------- Nodes/ui.tscn | 2 +- 3 files changed, 3 insertions(+), 23 deletions(-) diff --git a/Nodes/Level_test.tscn b/Nodes/Level_test.tscn index f082198..6e2c195 100644 --- a/Nodes/Level_test.tscn +++ b/Nodes/Level_test.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=11 format=3 uid="uid://dliou1u4o6bib"] +[gd_scene load_steps=10 format=3 uid="uid://dliou1u4o6bib"] [ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_0esj3"] [ext_resource type="Texture2D" uid="uid://5xqahgawme4m" path="res://Assets/start.png" id="1_wenkl"] [ext_resource type="Texture2D" uid="uid://b8vhe5kt6037n" path="res://Assets/end.png" id="2_gp6ng"] -[ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="3_cgigr"] [ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="4_xvobs"] [ext_resource type="Script" path="res://Scripts/drawing.gd" id="5_x7h3g"] [ext_resource type="PackedScene" uid="uid://wkfai2lvnp7n" path="res://Nodes/checkpoint.tscn" id="7_52mhf"] @@ -50,15 +49,6 @@ curve = SubResource("Curve2D_0exre") [node name="TrackFollower" type="PathFollow2D" parent="DrawNode/TrackPath"] loop = false -[node name="Sprite2D" type="Sprite2D" parent="DrawNode/TrackPath/TrackFollower"] -visible = false -scale = Vector2(0.604, 0.604) -texture = ExtResource("3_cgigr") - -[node name="31005" type="Timer" parent="DrawNode/TrackPath/TrackFollower"] -wait_time = 0.2 -one_shot = true - [node name="Checkpoints" type="Node" parent="."] [node name="Checkpoint" parent="Checkpoints" instance=ExtResource("7_52mhf")] diff --git a/Nodes/Level_test_2.tscn b/Nodes/Level_test_2.tscn index 9376c7f..df32fcd 100644 --- a/Nodes/Level_test_2.tscn +++ b/Nodes/Level_test_2.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://b7nxnsf8dirqk"] +[gd_scene load_steps=10 format=3 uid="uid://b7nxnsf8dirqk"] [ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_amd1r"] [ext_resource type="PackedScene" uid="uid://c8r6i3bu37ogu" path="res://Nodes/ui.tscn" id="2_veexm"] @@ -6,7 +6,6 @@ [ext_resource type="Texture2D" uid="uid://b8vhe5kt6037n" path="res://Assets/end.png" id="4_1x7qu"] [ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="5_vbxx6"] [ext_resource type="Script" path="res://Scripts/drawing.gd" id="6_6cnd6"] -[ext_resource type="Texture2D" uid="uid://dg3v488vx0oj6" path="res://Assets/checkpoint.png" id="7_alupw"] [ext_resource type="PackedScene" uid="uid://wkfai2lvnp7n" path="res://Nodes/checkpoint.tscn" id="8_8dt61"] [sub_resource type="CircleShape2D" id="CircleShape2D_6g88q"] @@ -50,15 +49,6 @@ curve = SubResource("Curve2D_0exre") [node name="TrackFollower" type="PathFollow2D" parent="DrawNode/TrackPath"] loop = false -[node name="Sprite2D" type="Sprite2D" parent="DrawNode/TrackPath/TrackFollower"] -visible = false -scale = Vector2(0.604, 0.604) -texture = ExtResource("7_alupw") - -[node name="31005" type="Timer" parent="DrawNode/TrackPath/TrackFollower"] -wait_time = 0.2 -one_shot = true - [node name="Checkpoints" type="Node" parent="."] [node name="Checkpoint" parent="Checkpoints" instance=ExtResource("8_8dt61")] diff --git a/Nodes/ui.tscn b/Nodes/ui.tscn index fd035c2..dcbbfa2 100644 --- a/Nodes/ui.tscn +++ b/Nodes/ui.tscn @@ -46,7 +46,7 @@ offset_right = 69.0 offset_bottom = 42.0 text = "Time: " -[node name="PostGameUI" type="Control" parent="Control"] +[node name="PostGameUI" type="Panel" parent="Control"] visible = false layout_mode = 1 anchors_preset = 15 -- cgit From 420b84da5f73084b51c78e9bdae9881cad67ef21 Mon Sep 17 00:00:00 2001 From: perivesta <> Date: Sun, 9 Jul 2023 14:49:46 +0200 Subject: new car physics --- Nodes/Level_test.tscn | 2 -- Nodes/Level_test_2.tscn | 2 -- Nodes/car.tscn | 1 + Scripts/car_behaviour.gd | 45 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Nodes/Level_test.tscn b/Nodes/Level_test.tscn index 6e2c195..d512420 100644 --- a/Nodes/Level_test.tscn +++ b/Nodes/Level_test.tscn @@ -35,8 +35,6 @@ texture = ExtResource("2_gp6ng") shape = SubResource("CircleShape2D_6g88q") [node name="Car" parent="." instance=ExtResource("4_xvobs")] -max_speed = 500 -acceleration = 0.05 [node name="DrawNode" type="Node2D" parent="."] script = ExtResource("5_x7h3g") diff --git a/Nodes/Level_test_2.tscn b/Nodes/Level_test_2.tscn index df32fcd..fff7252 100644 --- a/Nodes/Level_test_2.tscn +++ b/Nodes/Level_test_2.tscn @@ -35,8 +35,6 @@ texture = ExtResource("4_1x7qu") shape = SubResource("CircleShape2D_6g88q") [node name="Car" parent="." instance=ExtResource("5_vbxx6")] -max_speed = 500 -acceleration = 0.05 [node name="DrawNode" type="Node2D" parent="."] script = ExtResource("6_6cnd6") diff --git a/Nodes/car.tscn b/Nodes/car.tscn index dbfb4e1..98b68ac 100644 --- a/Nodes/car.tscn +++ b/Nodes/car.tscn @@ -15,3 +15,4 @@ texture = SubResource("CanvasTexture_aacgf") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("RectangleShape2D_wvlyu") +debug_color = Color(0.916191, 7.77793e-05, 0.710129, 0.42) diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index 2ef9fb9..8c22bf3 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -1,9 +1,10 @@ extends CharacterBody2D # editor variables -@export var max_speed = 300 -@export var steer_force = 0.1 -@export var acceleration = 0.0 +@export var max_speed = 800 +@export var steer_force = 20 +@export var acceleration = 8 +@export var mass = 1.0 @export var look_ahead = 75 @export var num_rays = 32 @export var driving = false @@ -35,8 +36,9 @@ func _physics_process(delta): set_danger() choose_direction() - # example_movement(delta) - own_movement(delta) +# example_movement(delta) +# own_movement(delta) + own_movement_improved(delta) func set_interest(): @@ -91,13 +93,38 @@ func example_movement(delta): rotation = velocity.angle() move_and_collide(velocity * delta) + func own_movement(delta): var desired_velocity = chosen_dir.rotated(rotation) * max_speed + + var new_speed = lerpf(velocity.length(), max_speed, acceleration) + var new_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) + + velocity = Vector2.from_angle(new_angle) * new_speed + rotation = new_angle + move_and_collide(velocity * delta) + + +func own_movement_improved(delta): + var desired_velocity = chosen_dir.rotated(rotation) * max_speed + print('==========') + print('current speed: ', velocity.length(), ' | current angle: ', velocity.angle()) + print('desired speed: ', desired_velocity.length(), ' | desired angle: ', desired_velocity.angle()) - var actual_speed = lerpf(velocity.length(), max_speed, acceleration) - var actual_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) +# var possible_radius = mass * velocity.length_squared() / steer_force +# var possible_steer_angle = velocity.length() / possible_radius + var possible_steer_angle = steer_force / (mass * velocity.length()) + var angle_change = clampf(wrapf(desired_velocity.angle() - velocity.angle(), -PI, PI), -possible_steer_angle, possible_steer_angle) + var new_angle = wrapf(velocity.angle() + angle_change, -PI, PI) + print('possible_steer_angle: ', possible_steer_angle, ' | angle_change: ', angle_change, ' | new_angle: ', new_angle ) - velocity = Vector2.from_angle(actual_angle) * actual_speed - rotation = actual_angle + var wanted_speed = steer_force / (mass * absf(desired_velocity.angle() - velocity.angle())) + var new_max_speed = min(velocity.length() + (acceleration), max_speed) + var new_min_speed = max(velocity.length() - (acceleration), 0) + var new_speed = clampf(wanted_speed, new_min_speed, new_max_speed) + print('wanted: ', wanted_speed, ' | min: ', new_min_speed, ' | max: ', new_max_speed, ' | new: ', new_speed) + velocity = Vector2.from_angle(new_angle) * new_speed + rotation = new_angle move_and_collide(velocity * delta) + -- cgit