diff options
Diffstat (limited to '')
-rw-r--r-- | Scripts/car_behaviour.gd | 33 |
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) |