From 88e3751834af72c413afa25a7c17a5994f570bfb Mon Sep 17 00:00:00 2001 From: IcECreAm777 Date: Sat, 8 Jul 2023 01:52:33 +0200 Subject: end of day commit --- Scripts/car_behaviour.gd | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ Scripts/scene_control.gd | 12 ++++++++ 2 files changed, 90 insertions(+) create mode 100644 Scripts/car_behaviour.gd create mode 100644 Scripts/scene_control.gd (limited to 'Scripts') diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd new file mode 100644 index 0000000..dfc9ea4 --- /dev/null +++ b/Scripts/car_behaviour.gd @@ -0,0 +1,78 @@ +extends StaticBody2D + +# editor variables +@export var max_speed = 350 +@export var steer_force = 0.1 +@export var look_ahead = 100 +@export var num_rays = 8 + +# context array +var ray_directions = [] +var interest = [] +var danger = [] + +var chosen_dir = Vector2.ZERO +var velocity = Vector2.ZERO +var acceleration = Vector2.ZERO + + +# Called when the node enters the scene tree for the first time. +func _ready(): + interest.resize(num_rays) + danger.resize(num_rays) + ray_directions.resize(num_rays) + for i in num_rays: + var angle = i * 2 * PI / num_rays + ray_directions[i] = Vector2.RIGHT.rotated(angle) + print(ray_directions) + + +func _physics_process(delta): + 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) + +func set_interest(): + # Set interest in each slot based on world direction + if owner and owner.has_method("get_path_direction"): + var path_direction = owner.get_path_direction(position) + for i in num_rays: + var d = ray_directions[i].rotated(rotation).dot(path_direction) + interest[i] = max(0, d) + # If no world path, use default interest + else: + set_default_interest() + +func set_default_interest(): + # Default to moving forward + for i in num_rays: + var d = ray_directions[i].rotated(rotation).dot(transform.x) + interest[i] = max(0, d) + +func set_danger(): + # Cast rays to find danger directions + var space_state = get_world_2d().direct_space_state + + var params = PhysicsRayQueryParameters2D.new() + params.from = position + params.exclude = [self] + + for i in num_rays: + params.to = position + ray_directions[i].rotated(rotation) * look_ahead + var result = space_state.intersect_ray(params) + danger[i] = 1.0 if result else 0.0 + +func choose_direction(): + # Eliminate interest in slots with danger + for i in num_rays: + if danger[i] > 0.0: + interest[i] = 0.0 + # Choose direction based on remaining interest + chosen_dir = Vector2.ZERO + for i in num_rays: + chosen_dir += ray_directions[i] * interest[i] + chosen_dir = chosen_dir.normalized() diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd new file mode 100644 index 0000000..99bc3a2 --- /dev/null +++ b/Scripts/scene_control.gd @@ -0,0 +1,12 @@ +extends Node + +class_name SceneControl + +@onready var path : Path2D = $Path2D +@onready var path_follow : PathFollow2D = $Path2D/PathFollow2D + + +func get_path_direction(pos): + var offset = path.curve.get_closest_offset(pos) + path_follow.h_offset = offset + return path_follow.transform.x -- cgit From 9ab7bc4c1e8bb266de9f100213e4036b1cd83c11 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Sat, 8 Jul 2023 01:38:26 +0100 Subject: why does this work gdscript HELLO? --- Scripts/car_behaviour.gd | 6 +++--- Scripts/scene_control.gd | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'Scripts') diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index dfc9ea4..c90fe05 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -1,10 +1,10 @@ extends StaticBody2D # editor variables -@export var max_speed = 350 +@export var max_speed = 3500 @export var steer_force = 0.1 -@export var look_ahead = 100 -@export var num_rays = 8 +@export var look_ahead = 75 +@export var num_rays = 32 # context array var ray_directions = [] diff --git a/Scripts/scene_control.gd b/Scripts/scene_control.gd index 99bc3a2..9f231e0 100644 --- a/Scripts/scene_control.gd +++ b/Scripts/scene_control.gd @@ -3,7 +3,8 @@ extends Node class_name SceneControl @onready var path : Path2D = $Path2D -@onready var path_follow : PathFollow2D = $Path2D/PathFollow2D + +@on#ready var path_follow : PathFollow2D = $Path2D/PathFollow2D func get_path_direction(pos): -- cgit From f7fd3f9dacf87e4d6f95aa0fdbb0bdb309bdbc3a Mon Sep 17 00:00:00 2001 From: perivesta <> Date: Sat, 8 Jul 2023 10:18:16 +0200 Subject: move/rename scene --- Scripts/car_behaviour.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Scripts') diff --git a/Scripts/car_behaviour.gd b/Scripts/car_behaviour.gd index c90fe05..b71ebb6 100644 --- a/Scripts/car_behaviour.gd +++ b/Scripts/car_behaviour.gd @@ -1,7 +1,7 @@ extends StaticBody2D # editor variables -@export var max_speed = 3500 +@export var max_speed = 300 @export var steer_force = 0.1 @export var look_ahead = 75 @export var num_rays = 32 -- cgit