aboutsummaryrefslogtreecommitdiffstats
path: root/Scripts
diff options
context:
space:
mode:
authorLibravatar IcECreAm777 <hgk.1998@googlemail.com>2023-07-08 14:03:54 +0200
committerLibravatar IcECreAm777 <hgk.1998@googlemail.com>2023-07-08 14:03:54 +0200
commitbf17c8a97b77cdb783a2751e7ee4a90843a00f06 (patch)
tree04085f13de39abb186607f80e1132feff8c4e42f /Scripts
parent892ed4854bcccbac546f5ac32c0c5fa0245f5fa7 (diff)
download2023-bf17c8a97b77cdb783a2751e7ee4a90843a00f06.tar.gz
2023-bf17c8a97b77cdb783a2751e7ee4a90843a00f06.tar.bz2
2023-bf17c8a97b77cdb783a2751e7ee4a90843a00f06.zip
split acceleration and steering
Diffstat (limited to 'Scripts')
-rw-r--r--Scripts/car_behaviour.gd33
1 files changed, 27 insertions, 6 deletions
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)