Make agents multiply acceleration by delta

This fixes the fact that acceleration wasn't treated as acceleration
  over time, but was instant acceleration.
This commit is contained in:
Francois Belair 2020-03-03 14:45:18 -05:00
parent a7d83af547
commit 192490b757
24 changed files with 3098 additions and 3008 deletions

View File

@ -2,6 +2,10 @@
This document lists new features, improvements, changes, and bug fixes in every release of the add-on.
## Master ##
- Acceleration for agents are multiplied by delta so values are acceleration per second. The demos' values have been increased significantly to compensate.
## Godot Steering AI Framework 2.1.0 ##
### Features ###

View File

@ -1,7 +1,7 @@
extends Node
export (float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max
export (float, 0, 200, 2.0) var linear_acceleration_max := 80.0 setget set_linear_acceleration_max
export (float, 0, 3200, 100) var linear_speed_max := 800.0 setget set_linear_speed_max
export (float, 0, 10000, 100) var linear_acceleration_max := 80.0 setget set_linear_acceleration_max
export (float, 0, 100, 0.1) var arrival_tolerance := 25.0 setget set_arrival_tolerance
export (float, 0, 500, 10) var deceleration_radius := 125.0 setget set_deceleration_radius

View File

@ -12,6 +12,10 @@ radius = 23.2163
[node name="ArriveDemo" type="Node"]
script = ExtResource( 3 )
linear_speed_max = 1600.0
linear_acceleration_max = 5000.0
arrival_tolerance = 35.0
deceleration_radius = 180.0
[node name="BackgroudLayer" parent="." instance=ExtResource( 4 )]

View File

@ -33,6 +33,11 @@ albedo_color = Color( 0.0941176, 0.235294, 0.486275, 1 )
[node name="Arrive3dDemo" type="Node"]
script = ExtResource( 3 )
linear_speed_max = 50.0
linear_acceleration_max = 53.2
deceleration_radius = 10.8
angular_speed_max = 550
angular_accel_max = 910
[node name="Arriver" type="KinematicBody" parent="."]
script = ExtResource( 4 )

View File

@ -1,13 +1,13 @@
extends Node
export (float, 0, 50, 0.1) var linear_speed_max := 10.0 setget set_linear_speed_max
export (float, 0, 50, 0.1) var linear_acceleration_max := 1.0 setget set_linear_acceleration_max
export (float, 0, 100, 5) var linear_speed_max := 10.0 setget set_linear_speed_max
export (float, 0, 100, 0.1) var linear_acceleration_max := 1.0 setget set_linear_acceleration_max
export (float, 0, 50, 0.1) var arrival_tolerance := 0.5 setget set_arrival_tolerance
export (float, 0, 50, 0.1) var deceleration_radius := 5.0 setget set_deceleration_radius
export (int, 0, 359, 2) var angular_speed_max := 270 setget set_angular_speed_max
export (int, 0, 359, 2) var angular_accel_max := 45 setget set_angular_accel_max
export (int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance
export (int, 0, 359, 2) var angular_deceleration_radius := 45 setget set_angular_deceleration_radius
export (int, 0, 1080, 10) var angular_speed_max := 270 setget set_angular_speed_max
export (int, 0, 2048, 10) var angular_accel_max := 45 setget set_angular_accel_max
export (int, 0, 178, 2) var align_tolerance := 5 setget set_align_tolerance
export (int, 0, 180, 2) var angular_deceleration_radius := 45 setget set_angular_deceleration_radius
onready var target := $MouseTarget
onready var arriver := $Arriver

View File

@ -1,7 +1,7 @@
extends Node
export (float, 0, 2000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max
export (float, 0, 100, 2) var linear_acceleration_max := 40.0 setget set_linear_accel_max
export (float, 0, 1000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max
export (float, 0, 4000, 2) var linear_acceleration_max := 40.0 setget set_linear_accel_max
export (float, 0, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius
export var draw_proximity := true setget set_draw_proximity

View File

@ -8,7 +8,8 @@
[node name="AvoidCollisionsDemo" type="Node"]
script = ExtResource( 2 )
linear_speed_max = 400.0
linear_speed_max = 520.0
linear_acceleration_max = 2250.0
proximity_radius = 100.0
[node name="BackgroudLayer" parent="." instance=ExtResource( 5 )]

View File

@ -1,7 +1,7 @@
extends Node
export (int, 0, 359, 2) var angular_speed_max := 120 setget set_angular_speed_max
export (int, 0, 359, 2) var angular_accel_max := 10 setget set_angular_accel_max
export (int, 0, 1080, 2) var angular_speed_max := 120 setget set_angular_speed_max
export (int, 0, 2048, 2) var angular_accel_max := 10 setget set_angular_accel_max
export (int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance
export (int, 0, 359, 2) var deceleration_radius := 45 setget set_deceleration_radius
export (float, 0, 1000, 40) var player_speed := 600.0 setget set_player_speed

View File

@ -15,8 +15,9 @@ radius = 37.1052
[node name="FaceDemo" type="Node"]
script = ExtResource( 2 )
angular_speed_max = 160
angular_accel_max = 16
angular_speed_max = 662
angular_accel_max = 924
deceleration_radius = 136
[node name="BackgroudLayer" parent="." instance=ExtResource( 5 )]

View File

@ -1,7 +1,7 @@
extends Node
export (float, 0, 2000, 40) var linear_speed_max := 600.0 setget set_linear_speed_max
export (float, 0, 200, 10.0) var linear_acceleration_max := 40.0 setget set_linear_acceleration_max
export (float, 0, 9000, 10.0) var linear_acceleration_max := 40.0 setget set_linear_acceleration_max
export (float, 0, 100, 0.1) var arrival_tolerance := 10.0 setget set_arrival_tolerance
export (float, 0, 500, 10) var deceleration_radius := 100.0 setget set_deceleration_radius
export (float, 0, 5, 0.1) var predict_time := 0.3 setget set_predict_time

View File

@ -12,7 +12,8 @@ radius = 24.1954
[node name="FollowPathDemo" type="Node"]
script = ExtResource( 4 )
linear_acceleration_max = 50.0
linear_speed_max = 920.0
linear_acceleration_max = 3740.0
deceleration_radius = 200.0
[node name="BackgroudLayer" parent="." instance=ExtResource( 5 )]

View File

@ -3,11 +3,11 @@ extends Node
onready var spawner := $Spawner
export (float, 0, 2000, 40.0) var linear_speed_max := 600.0 setget set_linear_speed_max
export (float, 0, 200, 2.0) var linear_accel_max := 40.0 setget set_linear_accel_max
export (float, 0, 9000, 2.0) var linear_accel_max := 40.0 setget set_linear_accel_max
export (float, 0, 300, 2.0) var proximity_radius := 140.0 setget set_proximity_radius
export (float, 0, 10000, 100) var separation_decay_coefficient := 2000.0 setget set_separation_decay_coef
export (float, 0, 200000, 250) var separation_decay_coefficient := 2000.0 setget set_separation_decay_coef
export (float, 0, 2, 0.1) var cohesion_strength := 0.1 setget set_cohesion_strength
export (float, 0, 6, 0.1) var separation_strength := 1.5 setget set_separation_strength
export (float, 0, 10, 0.2) var separation_strength := 1.5 setget set_separation_strength
export var show_proximity_radius := true setget set_show_proximity_radius

View File

@ -8,8 +8,11 @@
[node name="GroupBehaviorsDemo" type="Node"]
script = ExtResource( 3 )
linear_accel_max = 4234.0
proximity_radius = 158.0
separation_decay_coefficient = 121500.0
cohesion_strength = 0.2
separation_strength = 4.5
separation_strength = 8.8
[node name="BackgroudLayer" parent="." instance=ExtResource( 5 )]

View File

@ -1,7 +1,7 @@
extends Node
export (float, 0, 2000, 40) var linear_speed_max := 120.0 setget set_linear_speed_max
export (float, 0, 200, 2) var linear_accel_max := 10.0 setget set_linear_accel_max
export (float, 0, 2000, 20) var linear_accel_max := 10.0 setget set_linear_accel_max
export (float, 0, 5, 0.1) var predict_time := 1.0 setget set_predict_time
onready var pursuer := $BoundaryManager/Pursuer

View File

@ -10,8 +10,8 @@
[node name="PursueVSSeekDemo" type="Node"]
script = ExtResource( 4 )
linear_speed_max = 400.0
linear_accel_max = 80.0
linear_speed_max = 1280.0
linear_accel_max = 1040.0
predict_time = 1.1
[node name="BackgroudLayer" parent="." instance=ExtResource( 7 )]

View File

@ -25,7 +25,7 @@ func _physics_process(delta: float) -> void:
_blend.calculate_steering(accel)
agent.angular_velocity = clamp(
agent.angular_velocity + accel.angular, -agent.angular_speed_max, agent.angular_speed_max
agent.angular_velocity + accel.angular * delta, -agent.angular_speed_max, agent.angular_speed_max
)
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
@ -33,7 +33,7 @@ func _physics_process(delta: float) -> void:
var linear_velocity := (
GSAIUtils.to_vector2(agent.linear_velocity)
+ (GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max)
+ (GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max * delta)
)
linear_velocity = linear_velocity.clamped(agent.linear_speed_max)
linear_velocity = linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
@ -51,14 +51,14 @@ func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float
var orient_behavior := GSAIFace.new(agent, _direction_face)
orient_behavior.alignment_tolerance = deg2rad(5)
orient_behavior.deceleration_radius = deg2rad(5)
orient_behavior.deceleration_radius = deg2rad(30)
_blend = GSAIBlend.new(agent)
_blend.add(behavior, 1)
_blend.add(orient_behavior, 1)
agent.angular_acceleration_max = deg2rad(40)
agent.angular_speed_max = deg2rad(90)
agent.angular_acceleration_max = deg2rad(1080)
agent.angular_speed_max = deg2rad(360)
agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max

View File

@ -77,7 +77,7 @@ func _ready() -> void:
# How close for the agent to be 'aligned', if not exact.
face.alignment_tolerance = deg2rad(5)
# When to start slowing down
face.deceleration_radius = deg2rad(45)
face.deceleration_radius = deg2rad(60)
# LookWhereYouGo turns the agent to keep looking towards its direction of travel. It will only
# be enabled while the agent is at low health.
@ -85,7 +85,7 @@ func _ready() -> void:
# How close for the agent to be 'aligned', if not exact
look.alignment_tolerance = deg2rad(5)
# When to start slowing down.
look.deceleration_radius = deg2rad(45)
look.deceleration_radius = deg2rad(60)
# Behaviors that are not enabled produce 0 acceleration.
# Adding our fleeing behaviors to a blend. The order does not matter.
@ -118,7 +118,7 @@ func _physics_process(delta: float) -> void:
# We add the discovered acceleration to our linear velocity. The toolkit does not limit
# velocity, just acceleration, so we clamp the result ourselves here.
velocity = (velocity + Vector2(acceleration.linear.x, acceleration.linear.y)).clamped(
velocity = (velocity + Vector2(acceleration.linear.x, acceleration.linear.y) * delta).clamped(
agent.linear_speed_max
)
@ -131,7 +131,7 @@ func _physics_process(delta: float) -> void:
# We then do something similar to apply our agent's rotational speed.
angular_velocity = clamp(
angular_velocity + acceleration.angular, -agent.angular_speed_max, agent.angular_speed_max
angular_velocity + acceleration.angular * delta, -agent.angular_speed_max, agent.angular_speed_max
)
# This applies drag on the agent's rotation, helping it slow down naturally.
angular_velocity = lerp(angular_velocity, 0, angular_drag)

View File

@ -40,13 +40,13 @@ func _physics_process(delta: float) -> void:
direction = GSAIUtils.angle_to_vector2(rotation)
velocity += direction * acceleration_max * movement
velocity += direction * acceleration_max * movement * delta
velocity = velocity.clamped(speed_max)
velocity = velocity.linear_interpolate(Vector2.ZERO, 0.1)
velocity = move_and_slide(velocity)
face.calculate_steering(accel)
angular_velocity += accel.angular
angular_velocity += accel.angular * delta
angular_velocity = clamp(angular_velocity, -agent.angular_speed_max, agent.angular_speed_max)
angular_velocity = lerp(angular_velocity, 0, 0.1)
rotation += angular_velocity * delta

View File

@ -17,6 +17,10 @@ position = Vector2( 402.346, 573.791 )
rotation = 1.5708
collision_mask = 2
script = ExtResource( 3 )
speed_max = 900.0
acceleration_max = 4200.0
rotation_speed_max = 360
rotation_accel_max = 1280
bullet = ExtResource( 4 )
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Player"]
@ -37,6 +41,10 @@ rotation = 1.5708
collision_layer = 2
collision_mask = 5
script = ExtResource( 2 )
speed_max = 600.0
acceleration_max = 2800.0
angular_speed_max = 360
angular_acceleration_max = 1280
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Agent"]
polygon = PoolVector2Array( 0, -32, -24, 32, 24, 32 )

View File

@ -4,8 +4,8 @@ extends Node
enum Mode { FLEE, SEEK }
export (Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
export (float, 0, 2000, 40) var linear_speed_max := 200.0 setget set_linear_speed_max
export (float, 0, 500, 0.5) var linear_accel_max := 10.0 setget set_linear_accel_max
export (float, 0, 1000, 30) var linear_speed_max := 200.0 setget set_linear_speed_max
export (float, 0, 2000, 40) var linear_accel_max := 10.0 setget set_linear_accel_max
export (float) var player_speed := 600.0 setget set_player_speed
var camera_boundaries: Rect2

View File

@ -20,6 +20,8 @@ extents = Vector2( 965.654, 10 )
[node name="SeekFleeDemo" type="Node"]
script = ExtResource( 3 )
linear_speed_max = 570.0
linear_accel_max = 1160.0
[node name="BackgroudLayer" parent="." instance=ExtResource( 8 )]

File diff suppressed because it is too large Load Diff

View File

@ -38,19 +38,19 @@ func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_apply_sliding_steering(acceleration.linear, delta)
_:
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
func _apply_sliding_steering(accel: Vector3) -> void:
func _apply_sliding_steering(accel: Vector3, delta: float) -> void:
var _body: KinematicBody2D = _body_ref.get_ref()
if not _body:
return
var velocity := GSAIUtils.to_vector2(linear_velocity + accel).clamped(linear_speed_max)
var velocity := GSAIUtils.to_vector2(linear_velocity + accel * delta).clamped(linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector2.ZERO, linear_drag_percentage)
velocity = _body.move_and_slide(velocity)
@ -63,7 +63,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
# warning-ignore:return_value_discarded
@ -77,7 +77,7 @@ func _apply_position_steering(accel: Vector3, delta: float) -> void:
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
_body.global_position += GSAIUtils.to_vector2(velocity) * delta
@ -90,7 +90,11 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
if not _body:
return
var velocity = angular_velocity + angular_acceleration
var velocity = clamp(
angular_velocity + angular_acceleration * delta,
-angular_acceleration_max,
angular_acceleration_max
)
if apply_angular_drag:
velocity = lerp(velocity, 0, angular_drag_percentage)
_body.rotation += velocity * delta

View File

@ -38,19 +38,19 @@ func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_apply_sliding_steering(acceleration.linear, delta)
_:
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
func _apply_sliding_steering(accel: Vector3) -> void:
func _apply_sliding_steering(accel: Vector3, delta: float) -> void:
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
velocity = _body.move_and_slide(velocity)
@ -63,7 +63,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
# warning-ignore:return_value_discarded
@ -77,7 +77,7 @@ func _apply_position_steering(accel: Vector3, delta: float) -> void:
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
_body.global_position += velocity * delta
@ -90,7 +90,11 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
if not _body:
return
var velocity = angular_velocity + angular_acceleration
var velocity = clamp(
angular_velocity + angular_acceleration * delta,
-angular_acceleration_max,
angular_acceleration_max
)
if apply_angular_drag:
velocity = lerp(velocity, 0, angular_drag_percentage)
_body.rotation.y += velocity * delta