From bf17c8a97b77cdb783a2751e7ee4a90843a00f06 Mon Sep 17 00:00:00 2001 From: IcECreAm777 Date: Sat, 8 Jul 2023 14:03:54 +0200 Subject: split acceleration and steering --- Nodes/drawing_and_driving.tscn | 4 ++- Scripts/car_behaviour.gd | 33 ++++++++++++++++++---- root_scene.tscn | 63 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 root_scene.tscn diff --git a/Nodes/drawing_and_driving.tscn b/Nodes/drawing_and_driving.tscn index 32ef3e1..865fd4e 100644 --- a/Nodes/drawing_and_driving.tscn +++ b/Nodes/drawing_and_driving.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=6 format=3 uid="uid://bj5wruvb2hg20"] +[gd_scene load_steps=6 format=3 uid="uid://cb06k14t2slax"] [ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_6bj13"] [ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="1_ktvsb"] @@ -11,6 +11,8 @@ script = ExtResource("1_6bj13") [node name="Car" parent="." instance=ExtResource("1_ktvsb")] +max_speed = 500 +acceleration = 0.05 [node name="DrawNode" type="Node2D" parent="."] script = ExtResource("3_el555") diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index afbc32f..0463dc1 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -3,6 +3,7 @@ extends StaticBody2D # editor variables @export var max_speed = 300 @export var steer_force = 0.1 +@export var acceleration = 0.0 @export var look_ahead = 75 @export var num_rays = 32 @export var driving = false @@ -14,7 +15,7 @@ var danger = [] var chosen_dir = Vector2.ZERO var velocity = Vector2.ZERO -var acceleration = Vector2.ZERO +var current_speed = 0 # Called when the node enters the scene tree for the first time. @@ -27,18 +28,18 @@ func _ready(): ray_directions[i] = Vector2.RIGHT.rotated(angle) print(ray_directions) - func _physics_process(delta): if not driving: return + # Code for getting the next desired direction based on the environment set_interest() set_danger() choose_direction() - var desired_velocity = chosen_dir.rotated(rotation) * max_speed - velocity = velocity.lerp(desired_velocity, steer_force) - rotation = velocity.angle() - move_and_collide(velocity * delta) + + # example_movement(delta) + own_movement(delta) + func set_interest(): # Set interest in each slot based on world direction @@ -84,3 +85,23 @@ func choose_direction(): for i in num_rays: chosen_dir += ray_directions[i] * interest[i] chosen_dir = chosen_dir.normalized() + + +func example_movement(delta): + var desired_velocity = chosen_dir.rotated(rotation) * max_speed + velocity = velocity.lerp(desired_velocity, steer_force) + rotation = velocity.angle() + move_and_collide(velocity * delta) + +func own_movement(delta): + var desired_velocity = chosen_dir.rotated(rotation) * max_speed + + var actual_speed = lerpf(velocity.length(), max_speed, acceleration) + var actual_angle = lerp_angle(velocity.angle(), desired_velocity.angle(), steer_force) + + print("lerped speed: ", actual_speed, " | lerped angle: ", actual_angle) + + velocity = Vector2.from_angle(actual_angle) * actual_speed + rotation = actual_angle + + move_and_collide(velocity * delta) diff --git a/root_scene.tscn b/root_scene.tscn new file mode 100644 index 0000000..628d0cd --- /dev/null +++ b/root_scene.tscn @@ -0,0 +1,63 @@ +[gd_scene load_steps=5 format=3 uid="uid://dksiactnihgg3"] + +[ext_resource type="Script" path="res://Scripts/scene_control.gd" id="1_eopk0"] +[ext_resource type="PackedScene" uid="uid://cqglbcitm2wlb" path="res://Nodes/simple_wall.tscn" id="1_o5f4t"] +[ext_resource type="PackedScene" uid="uid://c1mvbh2fwqtkb" path="res://Nodes/car.tscn" id="2_22f5l"] + +[sub_resource type="Curve2D" id="Curve2D_rg63l"] +bake_interval = 10.0 +_data = { +"points": PackedVector2Array(0, 0, 0, 0, 495, 54, 0, 0, 0, 0, 567, 56, -17.0089, 38.4276, 17.0089, -38.4276, 627, 32, -18.8988, 13.2292, 18.8988, -13.2292, 648, -102, -31.498, -18.8988, 31.498, 18.8988, 718, -102, 8.18949, -25.8284, -8.18949, 25.8284, 720, 263, 26.4583, 13.2292, -26.4583, -13.2292, 639, 286, 46.6171, 22.6786, -46.6171, -22.6786, 500, 170, 0, 0, 0, 0, 369, 136, -3.1498, 47.247, 3.1498, -47.247, 312, 90, 0, 0, 0, 0, 368, 53, 0, 0, 0, 0, 495, 54) +} +point_count = 12 + +[node name="Node2D" type="Node2D"] +position = Vector2(0, 1) +script = ExtResource("1_eopk0") + +[node name="SimpleWall" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(520, 240) + +[node name="SimpleWall10" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(645, -4) + +[node name="SimpleWall8" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(427, 139) + +[node name="SimpleWall7" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(236, 321) + +[node name="SimpleWall6" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(494, 417) +rotation = 0.716721 + +[node name="SimpleWall4" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(618, 493) + +[node name="SimpleWall2" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(680, 235) +scale = Vector2(1.28, 16.6) + +[node name="SimpleWall3" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(762, 238) +scale = Vector2(1.28, 27.28) + +[node name="SimpleWall9" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(581, -122) +scale = Vector2(1.28, 27.28) + +[node name="SimpleWall5" parent="." instance=ExtResource("1_o5f4t")] +position = Vector2(273, 258) +scale = Vector2(1.28, 27.28) + +[node name="Path2D" type="Path2D" parent="."] +position = Vector2(0, 147) +curve = SubResource("Curve2D_rg63l") + +[node name="PathFollow2D" type="PathFollow2D" parent="Path2D"] +position = Vector2(495, 54) +rotation = 0.0277706 + +[node name="Car" parent="." instance=ExtResource("2_22f5l")] +position = Vector2(390, 202) +max_speed = 100 -- cgit