diff options
author | perivesta <> | 2023-07-09 14:49:46 +0200 |
---|---|---|
committer | perivesta <> | 2023-07-09 14:49:46 +0200 |
commit | 420b84da5f73084b51c78e9bdae9881cad67ef21 (patch) | |
tree | e3e9b8d37c8fc835243aed98b1c5c4c6e76a72b2 /Scripts/car_behaviour.gd | |
parent | baf451d062acc110e791e73d28be9bdbf20a1eb3 (diff) | |
download | 2023-420b84da5f73084b51c78e9bdae9881cad67ef21.tar.gz 2023-420b84da5f73084b51c78e9bdae9881cad67ef21.tar.bz2 2023-420b84da5f73084b51c78e9bdae9881cad67ef21.zip |
new car physics
Diffstat (limited to 'Scripts/car_behaviour.gd')
-rw-r--r-- | Scripts/car_behaviour.gd | 45 |
1 files changed, 36 insertions, 9 deletions
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) + |