mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
Refactor var names for consistency aka min/max
This commit is contained in:
parent
30a1a299d7
commit
8228694713
@ -1,8 +1,8 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
export(float, 0, 2000, 40) var max_linear_speed := 800.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max
|
||||||
export(float, 0, 200, 2.0) var max_linear_acceleration := 80.0 setget set_max_linear_acceleration
|
export(float, 0, 200, 2.0) 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, 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
|
export(float, 0, 500, 10) var deceleration_radius := 125.0 setget set_deceleration_radius
|
||||||
|
|
||||||
@ -16,8 +16,8 @@ onready var arriver := $Arriver
|
|||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
arriver.setup(
|
arriver.setup(
|
||||||
max_linear_speed,
|
linear_speed_max,
|
||||||
max_linear_acceleration,
|
linear_acceleration_max,
|
||||||
arrival_tolerance,
|
arrival_tolerance,
|
||||||
deceleration_radius
|
deceleration_radius
|
||||||
)
|
)
|
||||||
@ -51,17 +51,17 @@ func set_deceleration_radius(value: float) -> void:
|
|||||||
arriver.arrive.deceleration_radius = value
|
arriver.arrive.deceleration_radius = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
arriver.agent.max_linear_speed = value
|
arriver.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_acceleration(value: float) -> void:
|
func set_linear_acceleration_max(value: float) -> void:
|
||||||
max_linear_acceleration = value
|
linear_acceleration_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
arriver.agent.max_linear_acceleration = value
|
arriver.agent.linear_acceleration_max = value
|
||||||
|
@ -14,18 +14,18 @@ func _physics_process(delta: float) -> void:
|
|||||||
_update_agent()
|
_update_agent()
|
||||||
_accel = arrive.calculate_steering(_accel)
|
_accel = arrive.calculate_steering(_accel)
|
||||||
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
||||||
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.max_linear_speed)
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.linear_speed_max)
|
||||||
_velocity = move_and_slide(_velocity)
|
_velocity = move_and_slide(_velocity)
|
||||||
|
|
||||||
|
|
||||||
func setup(
|
func setup(
|
||||||
max_linear_speed: float,
|
linear_speed_max: float,
|
||||||
max_linear_acceleration: float,
|
linear_acceleration_max: float,
|
||||||
arrival_tolerance: float,
|
arrival_tolerance: float,
|
||||||
deceleration_radius: float
|
deceleration_radius: float
|
||||||
) -> void:
|
) -> void:
|
||||||
agent.max_linear_speed = max_linear_speed
|
agent.linear_speed_max = linear_speed_max
|
||||||
agent.max_linear_acceleration = max_linear_acceleration
|
agent.linear_acceleration_max = linear_acceleration_max
|
||||||
agent.position = Vector3(global_position.x, global_position.y, 0)
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
||||||
arrive.deceleration_radius = deceleration_radius
|
arrive.deceleration_radius = deceleration_radius
|
||||||
arrive.arrival_tolerance = arrival_tolerance
|
arrive.arrival_tolerance = arrival_tolerance
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
export(float, 0, 2000, 40) var max_linear_speed := 350.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max
|
||||||
export(float, 0, 100, 2) var max_linear_acceleration := 40.0 setget set_max_linear_accel
|
export(float, 0, 100, 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(float, 0, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius
|
||||||
export var draw_proximity := true setget set_draw_proximity
|
export var draw_proximity := true setget set_draw_proximity
|
||||||
|
|
||||||
onready var spawner := $Spawner
|
onready var spawner := $Spawner
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
spawner.set_max_linear_speed(value)
|
spawner.set_linear_speed_max(value)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
max_linear_acceleration = value
|
linear_acceleration_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
spawner.set_max_linear_accel(value)
|
spawner.set_linear_accel_max(value)
|
||||||
|
|
||||||
|
|
||||||
func set_proximity_radius(value: float) -> void:
|
func set_proximity_radius(value: float) -> void:
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
[node name="AvoidCollisionsDemo" type="Node2D"]
|
[node name="AvoidCollisionsDemo" type="Node2D"]
|
||||||
script = ExtResource( 2 )
|
script = ExtResource( 2 )
|
||||||
max_linear_speed = 360.0
|
|
||||||
proximity_radius = 100.0
|
proximity_radius = 100.0
|
||||||
|
|
||||||
[node name="Spawner" type="Node2D" parent="."]
|
[node name="Spawner" type="Node2D" parent="."]
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
extends KinematicBody2D
|
extends KinematicBody2D
|
||||||
|
|
||||||
|
|
||||||
onready var collision := $CollisionShape2D
|
|
||||||
onready var agent := GSTSteeringAgent.new()
|
|
||||||
onready var proximity := GSTRadiusProximity.new(agent, [], 140)
|
|
||||||
onready var avoid := GSTAvoidCollisions.new(agent, proximity)
|
|
||||||
onready var target := GSTAgentLocation.new()
|
|
||||||
onready var seek := GSTSeek.new(agent, target)
|
|
||||||
onready var priority := GSTPriority.new(agent, 0.0001)
|
|
||||||
onready var sprite := $Sprite
|
|
||||||
|
|
||||||
var draw_proximity: bool
|
var draw_proximity: bool
|
||||||
|
|
||||||
var _boundary_right: float
|
var _boundary_right: float
|
||||||
@ -20,6 +11,15 @@ var _velocity := Vector2.ZERO
|
|||||||
var _direction := Vector2()
|
var _direction := Vector2()
|
||||||
var _drag: = 0.1
|
var _drag: = 0.1
|
||||||
|
|
||||||
|
onready var collision := $CollisionShape2D
|
||||||
|
onready var agent := GSTSteeringAgent.new()
|
||||||
|
onready var proximity := GSTRadiusProximity.new(agent, [], 140)
|
||||||
|
onready var avoid := GSTAvoidCollisions.new(agent, proximity)
|
||||||
|
onready var target := GSTAgentLocation.new()
|
||||||
|
onready var seek := GSTSeek.new(agent, target)
|
||||||
|
onready var priority := GSTPriority.new(agent, 0.0001)
|
||||||
|
onready var sprite := $Sprite
|
||||||
|
|
||||||
|
|
||||||
func _draw() -> void:
|
func _draw() -> void:
|
||||||
if draw_proximity:
|
if draw_proximity:
|
||||||
@ -31,13 +31,13 @@ func _physics_process(delta: float) -> void:
|
|||||||
_accel = priority.calculate_steering(_accel)
|
_accel = priority.calculate_steering(_accel)
|
||||||
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
||||||
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
||||||
_velocity = _velocity.clamped(agent.max_linear_speed)
|
_velocity = _velocity.clamped(agent.linear_speed_max)
|
||||||
_velocity = move_and_slide(_velocity)
|
_velocity = move_and_slide(_velocity)
|
||||||
|
|
||||||
|
|
||||||
func setup(
|
func setup(
|
||||||
max_linear_speed: float,
|
linear_speed_max: float,
|
||||||
max_linear_accel: float,
|
linear_accel_max: float,
|
||||||
proximity_radius: float,
|
proximity_radius: float,
|
||||||
boundary_right: float,
|
boundary_right: float,
|
||||||
boundary_bottom: float,
|
boundary_bottom: float,
|
||||||
@ -47,8 +47,8 @@ func setup(
|
|||||||
rng.randomize()
|
rng.randomize()
|
||||||
_direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized()
|
_direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized()
|
||||||
_update_agent()
|
_update_agent()
|
||||||
agent.max_linear_speed = max_linear_speed
|
agent.linear_speed_max = linear_speed_max
|
||||||
agent.max_linear_acceleration = max_linear_accel
|
agent.linear_acceleration_max = linear_accel_max
|
||||||
proximity.radius = proximity_radius
|
proximity.radius = proximity_radius
|
||||||
_boundary_bottom = boundary_bottom
|
_boundary_bottom = boundary_bottom
|
||||||
_boundary_right = boundary_right
|
_boundary_right = boundary_right
|
||||||
@ -65,22 +65,22 @@ func set_proximity_agents(agents: Array) -> void:
|
|||||||
proximity.agents = agents
|
proximity.agents = agents
|
||||||
|
|
||||||
|
|
||||||
func set_random_nonoverlapping_position(others: Array, min_distance_from_boundary: float) -> void:
|
func set_random_nonoverlapping_position(others: Array, distance_from_boundary_min: float) -> void:
|
||||||
var rng := RandomNumberGenerator.new()
|
var rng := RandomNumberGenerator.new()
|
||||||
rng.randomize()
|
rng.randomize()
|
||||||
var max_tries := max(100, others.size() * others.size())
|
var tries_max := max(100, others.size() * others.size())
|
||||||
while max_tries >= 0:
|
while tries_max > 0:
|
||||||
max_tries -= 1
|
tries_max -= 1
|
||||||
global_position.x = rng.randf_range(
|
global_position.x = rng.randf_range(
|
||||||
min_distance_from_boundary, _boundary_right-min_distance_from_boundary
|
distance_from_boundary_min, _boundary_right-distance_from_boundary_min
|
||||||
)
|
)
|
||||||
global_position.y = rng.randf_range(
|
global_position.y = rng.randf_range(
|
||||||
min_distance_from_boundary, _boundary_bottom-min_distance_from_boundary
|
distance_from_boundary_min, _boundary_bottom-distance_from_boundary_min
|
||||||
)
|
)
|
||||||
var done := true
|
var done := true
|
||||||
for i in range(others.size()):
|
for i in range(others.size()):
|
||||||
var other: Node2D = others[i]
|
var other: Node2D = others[i]
|
||||||
if other.global_position.distance_to(position) <= _radius*2 + min_distance_from_boundary:
|
if other.global_position.distance_to(position) <= _radius*2 + distance_from_boundary_min:
|
||||||
done = false
|
done = false
|
||||||
if done:
|
if done:
|
||||||
break
|
break
|
||||||
|
@ -18,8 +18,8 @@ func _ready() -> void:
|
|||||||
var avoider := avoider_template.instance()
|
var avoider := avoider_template.instance()
|
||||||
add_child(avoider)
|
add_child(avoider)
|
||||||
avoider.setup(
|
avoider.setup(
|
||||||
owner.max_linear_speed,
|
owner.linear_speed_max,
|
||||||
owner.max_linear_acceleration,
|
owner.linear_acceleration_max,
|
||||||
owner.proximity_radius,
|
owner.proximity_radius,
|
||||||
boundaries.x,
|
boundaries.x,
|
||||||
boundaries.y,
|
boundaries.y,
|
||||||
@ -39,14 +39,14 @@ func _physics_process(delta: float) -> void:
|
|||||||
child.global_position = child.global_position.posmodv(boundaries)
|
child.global_position = child.global_position.posmodv(boundaries)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
for child in get_children():
|
for child in get_children():
|
||||||
child.agent.max_linear_speed = value
|
child.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
for child in get_children():
|
for child in get_children():
|
||||||
child.agent.max_linear_acceleration = value
|
child.agent.linear_acceleration_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_proximity_radius(value: float) -> void:
|
func set_proximity_radius(value: float) -> void:
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
|
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, 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
|
||||||
|
|
||||||
onready var player := $Player
|
onready var player := $Player
|
||||||
onready var gui := $GUI
|
onready var gui := $GUI
|
||||||
onready var turret := $Turret
|
onready var turret := $Turret
|
||||||
|
|
||||||
export(int, 0, 359, 2) var max_angular_speed := 120 setget set_max_angular_speed
|
|
||||||
export(int, 0, 359, 2) var max_angular_accel := 10 setget set_max_angular_accel
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
player.speed = player_speed
|
player.speed = player_speed
|
||||||
@ -18,8 +18,8 @@ func _ready() -> void:
|
|||||||
player.agent,
|
player.agent,
|
||||||
deg2rad(align_tolerance),
|
deg2rad(align_tolerance),
|
||||||
deg2rad(deceleration_radius),
|
deg2rad(deceleration_radius),
|
||||||
deg2rad(max_angular_accel),
|
deg2rad(angular_accel_max),
|
||||||
deg2rad(max_angular_speed)
|
deg2rad(angular_speed_max)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -39,20 +39,20 @@ func set_deceleration_radius(value: int) -> void:
|
|||||||
turret.face.deceleration_radius = deg2rad(value)
|
turret.face.deceleration_radius = deg2rad(value)
|
||||||
|
|
||||||
|
|
||||||
func set_max_angular_accel(value: int) -> void:
|
func set_angular_accel_max(value: int) -> void:
|
||||||
max_angular_accel = value
|
angular_accel_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
turret.agent.max_angular_acceleration = deg2rad(value)
|
turret.agent.angular_acceleration_max = deg2rad(value)
|
||||||
|
|
||||||
|
|
||||||
func set_max_angular_speed(value: int) -> void:
|
func set_angular_speed_max(value: int) -> void:
|
||||||
max_angular_speed = value
|
angular_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
turret.agent.max_angular_speed = deg2rad(value)
|
turret.agent.angular_speed_max = deg2rad(value)
|
||||||
|
|
||||||
|
|
||||||
func set_player_speed(value: float) -> void:
|
func set_player_speed(value: float) -> void:
|
||||||
|
@ -18,7 +18,11 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
_accel = face.calculate_steering(_accel)
|
_accel = face.calculate_steering(_accel)
|
||||||
agent.angular_velocity += _accel.angular
|
agent.angular_velocity = clamp(
|
||||||
|
agent.angular_velocity + _accel.angular,
|
||||||
|
-agent.angular_speed_max,
|
||||||
|
agent.angular_speed_max
|
||||||
|
)
|
||||||
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
|
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
|
||||||
agent.orientation += agent.angular_velocity * delta
|
agent.orientation += agent.angular_velocity * delta
|
||||||
rotation = agent.orientation
|
rotation = agent.orientation
|
||||||
@ -32,15 +36,15 @@ func setup(
|
|||||||
player_agent: GSTAgentLocation,
|
player_agent: GSTAgentLocation,
|
||||||
align_tolerance: float,
|
align_tolerance: float,
|
||||||
deceleration_radius: float,
|
deceleration_radius: float,
|
||||||
max_angular_accel: float,
|
angular_accel_max: float,
|
||||||
max_angular_speed: float
|
angular_speed_max: float
|
||||||
) -> void:
|
) -> void:
|
||||||
face = GSTFace.new(agent, player_agent)
|
face = GSTFace.new(agent, player_agent)
|
||||||
|
|
||||||
face.alignment_tolerance = align_tolerance
|
face.alignment_tolerance = align_tolerance
|
||||||
face.deceleration_radius = deceleration_radius
|
face.deceleration_radius = deceleration_radius
|
||||||
|
|
||||||
agent.max_angular_acceleration = max_angular_accel
|
agent.angular_acceleration_max = angular_accel_max
|
||||||
agent.max_angular_speed = max_angular_speed
|
agent.angular_speed_max = angular_speed_max
|
||||||
agent.position = Vector3(global_position.x, global_position.y, 0)
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
export(float, 0, 2000, 40) var max_linear_speed := 600.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40) var linear_speed_max := 600.0 setget set_linear_speed_max
|
||||||
export(float, 0, 200, 10.0) var max_linear_acceleration := 40.0 setget set_max_linear_acceleration
|
export(float, 0, 200, 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, 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, 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
|
export(float, 0, 5, 0.1) var predict_time := 0.3 setget set_predict_time
|
||||||
@ -16,27 +16,27 @@ func _ready() -> void:
|
|||||||
follower.setup(
|
follower.setup(
|
||||||
path_offset,
|
path_offset,
|
||||||
predict_time,
|
predict_time,
|
||||||
max_linear_acceleration,
|
linear_acceleration_max,
|
||||||
max_linear_speed,
|
linear_speed_max,
|
||||||
deceleration_radius,
|
deceleration_radius,
|
||||||
arrival_tolerance
|
arrival_tolerance
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
follower.agent.max_linear_speed = value
|
follower.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_acceleration(value: float) -> void:
|
func set_linear_acceleration_max(value: float) -> void:
|
||||||
max_linear_acceleration = value
|
linear_acceleration_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
follower.agent.max_linear_acceleration = value
|
follower.agent.linear_acceleration_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_arrival_tolerance(value: float) -> void:
|
func set_arrival_tolerance(value: float) -> void:
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
extends KinematicBody2D
|
extends KinematicBody2D
|
||||||
|
|
||||||
|
|
||||||
|
var _velocity := Vector2.ZERO
|
||||||
|
var _accel := GSTTargetAcceleration.new()
|
||||||
|
var _valid := false
|
||||||
|
var _drag := 0.1
|
||||||
|
|
||||||
onready var agent := GSTSteeringAgent.new()
|
onready var agent := GSTSteeringAgent.new()
|
||||||
onready var path := GSTPath.new([
|
onready var path := GSTPath.new([
|
||||||
Vector3(global_position.x, global_position.y, 0),
|
Vector3(global_position.x, global_position.y, 0),
|
||||||
@ -8,25 +13,20 @@ onready var path := GSTPath.new([
|
|||||||
], true)
|
], true)
|
||||||
onready var follow := GSTFollowPath.new(agent, path, 0, 0)
|
onready var follow := GSTFollowPath.new(agent, path, 0, 0)
|
||||||
|
|
||||||
var _velocity := Vector2.ZERO
|
|
||||||
var _accel := GSTTargetAcceleration.new()
|
|
||||||
var _valid := false
|
|
||||||
var _drag := 0.1
|
|
||||||
|
|
||||||
|
|
||||||
func setup(
|
func setup(
|
||||||
path_offset: float,
|
path_offset: float,
|
||||||
predict_time: float,
|
predict_time: float,
|
||||||
max_accel: float,
|
accel_max: float,
|
||||||
max_speed: float,
|
speed_max: float,
|
||||||
decel_radius: float,
|
decel_radius: float,
|
||||||
arrival_tolerance: float
|
arrival_tolerance: float
|
||||||
) -> void:
|
) -> void:
|
||||||
owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
|
owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
|
||||||
follow.path_offset = path_offset
|
follow.path_offset = path_offset
|
||||||
follow.prediction_time = predict_time
|
follow.prediction_time = predict_time
|
||||||
agent.max_linear_acceleration = max_accel
|
agent.linear_acceleration_max = accel_max
|
||||||
agent.max_linear_speed = max_speed
|
agent.linear_speed_max = speed_max
|
||||||
follow.deceleration_radius = decel_radius
|
follow.deceleration_radius = decel_radius
|
||||||
follow.arrival_tolerance = arrival_tolerance
|
follow.arrival_tolerance = arrival_tolerance
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ func _physics_process(delta: float) -> void:
|
|||||||
_accel = follow.calculate_steering(_accel)
|
_accel = follow.calculate_steering(_accel)
|
||||||
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
||||||
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
|
||||||
_velocity = _velocity.clamped(agent.max_linear_speed)
|
_velocity = _velocity.clamped(agent.linear_speed_max)
|
||||||
_velocity = move_and_slide(_velocity)
|
_velocity = move_and_slide(_velocity)
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@ extends Node2D
|
|||||||
|
|
||||||
onready var spawner := $Spawner
|
onready var spawner := $Spawner
|
||||||
|
|
||||||
export(float, 0, 2000, 40.0) var max_linear_speed := 600.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40.0) var linear_speed_max := 600.0 setget set_linear_speed_max
|
||||||
export(float, 0, 200, 2.0) var max_linear_accel := 40.0 setget set_max_linear_accel
|
export(float, 0, 200, 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, 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, 10000, 100) 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, 2, 0.1) var cohesion_strength := 0.1 setget set_cohesion_strength
|
||||||
@ -14,8 +14,8 @@ export var show_proximity_radius := true setget set_show_proximity_radius
|
|||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
spawner.setup(
|
spawner.setup(
|
||||||
max_linear_speed,
|
linear_speed_max,
|
||||||
max_linear_accel,
|
linear_accel_max,
|
||||||
proximity_radius,
|
proximity_radius,
|
||||||
separation_decay_coefficient,
|
separation_decay_coefficient,
|
||||||
cohesion_strength,
|
cohesion_strength,
|
||||||
@ -24,20 +24,20 @@ func _ready() -> void:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
spawner.set_max_linear_speed(value)
|
spawner.set_linear_speed_max(value)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
max_linear_accel = value
|
linear_accel_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
spawner.set_max_linear_accel(value)
|
spawner.set_linear_accel_max(value)
|
||||||
|
|
||||||
|
|
||||||
func set_proximity_radius(value: float) -> void:
|
func set_proximity_radius(value: float) -> void:
|
||||||
|
@ -14,8 +14,8 @@ var _velocity := Vector2()
|
|||||||
|
|
||||||
|
|
||||||
func setup(
|
func setup(
|
||||||
max_linear_speed: float,
|
linear_speed_max: float,
|
||||||
max_linear_accel: float,
|
linear_accel_max: float,
|
||||||
proximity_radius: float,
|
proximity_radius: float,
|
||||||
separation_decay_coefficient: float,
|
separation_decay_coefficient: float,
|
||||||
cohesion_strength: float,
|
cohesion_strength: float,
|
||||||
@ -24,8 +24,8 @@ func setup(
|
|||||||
_color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1))
|
_color = Color(rand_range(0.5, 1), rand_range(0.25, 1), rand_range(0, 1))
|
||||||
$Sprite.modulate = _color
|
$Sprite.modulate = _color
|
||||||
|
|
||||||
agent.max_linear_acceleration = max_linear_accel
|
agent.linear_acceleration_max = linear_accel_max
|
||||||
agent.max_linear_speed = max_linear_speed
|
agent.linear_speed_max = linear_speed_max
|
||||||
|
|
||||||
proximity = GSTRadiusProximity.new(agent, [], proximity_radius)
|
proximity = GSTRadiusProximity.new(agent, [], proximity_radius)
|
||||||
separation = GSTSeparation.new(agent, proximity)
|
separation = GSTSeparation.new(agent, proximity)
|
||||||
@ -47,7 +47,7 @@ func _physics_process(delta: float) -> void:
|
|||||||
acceleration = blend.calculate_steering(acceleration)
|
acceleration = blend.calculate_steering(acceleration)
|
||||||
_velocity += Vector2(acceleration.linear.x, acceleration.linear.y)
|
_velocity += Vector2(acceleration.linear.x, acceleration.linear.y)
|
||||||
_velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1)
|
_velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1)
|
||||||
_velocity = _velocity.clamped(agent.max_linear_speed)
|
_velocity = _velocity.clamped(agent.linear_speed_max)
|
||||||
move_and_slide(_velocity)
|
move_and_slide(_velocity)
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@ export var member: PackedScene
|
|||||||
|
|
||||||
|
|
||||||
func setup(
|
func setup(
|
||||||
max_linear_speed: float,
|
linear_speed_max: float,
|
||||||
max_linear_accel: float,
|
linear_accel_max: float,
|
||||||
proximity_radius: float,
|
proximity_radius: float,
|
||||||
separation_decay_coefficient: float,
|
separation_decay_coefficient: float,
|
||||||
cohesion_strength: float,
|
cohesion_strength: float,
|
||||||
@ -20,8 +20,8 @@ func setup(
|
|||||||
follower.position += Vector2(rand_range(-60, 60), rand_range(-60, 60))
|
follower.position += Vector2(rand_range(-60, 60), rand_range(-60, 60))
|
||||||
followers.append(follower)
|
followers.append(follower)
|
||||||
follower.setup(
|
follower.setup(
|
||||||
max_linear_speed,
|
linear_speed_max,
|
||||||
max_linear_accel,
|
linear_accel_max,
|
||||||
proximity_radius,
|
proximity_radius,
|
||||||
separation_decay_coefficient,
|
separation_decay_coefficient,
|
||||||
cohesion_strength,
|
cohesion_strength,
|
||||||
@ -37,14 +37,14 @@ func setup(
|
|||||||
i.proximity.agents = agents
|
i.proximity.agents = agents
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
for child in get_children():
|
for child in get_children():
|
||||||
child.agent.max_linear_speed = value
|
child.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
for child in get_children():
|
for child in get_children():
|
||||||
child.agent.max_linear_acceleration = value
|
child.agent.linear_acceleration_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_proximity_radius(value: float) -> void:
|
func set_proximity_radius(value: float) -> void:
|
||||||
|
@ -2,18 +2,18 @@ extends KinematicBody2D
|
|||||||
# Controls the player ship's movements based on player input.
|
# Controls the player ship's movements based on player input.
|
||||||
|
|
||||||
|
|
||||||
onready var agent := GSTSteeringAgent.new()
|
|
||||||
|
|
||||||
export var thruster_strength := 175.0
|
export var thruster_strength := 175.0
|
||||||
export var side_thruster_strength := 10.0
|
export var side_thruster_strength := 10.0
|
||||||
export var max_velocity := 300.0
|
export var velocity_max := 300.0
|
||||||
export var max_angular_velocity := 2.0
|
export var angular_velocity_max := 2.0
|
||||||
export var angular_drag := 0.025
|
export var angular_drag := 0.025
|
||||||
export var linear_drag := 0.025
|
export var linear_drag := 0.025
|
||||||
|
|
||||||
var _linear_velocity := Vector2()
|
var _linear_velocity := Vector2()
|
||||||
var _angular_velocity := 0.0
|
var _angular_velocity := 0.0
|
||||||
|
|
||||||
|
onready var agent := GSTSteeringAgent.new()
|
||||||
|
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
var movement := _get_movement()
|
var movement := _get_movement()
|
||||||
@ -21,7 +21,7 @@ func _physics_process(delta: float) -> void:
|
|||||||
movement.x,
|
movement.x,
|
||||||
_angular_velocity,
|
_angular_velocity,
|
||||||
side_thruster_strength,
|
side_thruster_strength,
|
||||||
max_angular_velocity,
|
angular_velocity_max,
|
||||||
angular_drag,
|
angular_drag,
|
||||||
delta
|
delta
|
||||||
)
|
)
|
||||||
@ -33,7 +33,7 @@ func _physics_process(delta: float) -> void:
|
|||||||
Vector2.UP.rotated(rotation),
|
Vector2.UP.rotated(rotation),
|
||||||
linear_drag,
|
linear_drag,
|
||||||
thruster_strength,
|
thruster_strength,
|
||||||
max_velocity,
|
velocity_max,
|
||||||
delta
|
delta
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,14 +45,14 @@ func _calculate_angular_velocity(
|
|||||||
horizontal_movement: float,
|
horizontal_movement: float,
|
||||||
current_velocity: float,
|
current_velocity: float,
|
||||||
thruster_strength: float,
|
thruster_strength: float,
|
||||||
max_velocity: float,
|
velocity_max: float,
|
||||||
ship_drag: float,
|
ship_drag: float,
|
||||||
delta: float
|
delta: float
|
||||||
) -> float:
|
) -> float:
|
||||||
var velocity := clamp(
|
var velocity := clamp(
|
||||||
current_velocity + thruster_strength * horizontal_movement * delta,
|
current_velocity + thruster_strength * horizontal_movement * delta,
|
||||||
-max_velocity,
|
-velocity_max,
|
||||||
max_velocity
|
velocity_max
|
||||||
)
|
)
|
||||||
|
|
||||||
velocity = lerp(velocity, 0, ship_drag)
|
velocity = lerp(velocity, 0, ship_drag)
|
||||||
@ -66,7 +66,7 @@ func _calculate_linear_velocity(
|
|||||||
facing_direction: Vector2,
|
facing_direction: Vector2,
|
||||||
ship_drag_coefficient: float,
|
ship_drag_coefficient: float,
|
||||||
strength: float,
|
strength: float,
|
||||||
max_speed: float,
|
speed_max: float,
|
||||||
delta: float
|
delta: float
|
||||||
) -> Vector2:
|
) -> Vector2:
|
||||||
var actual_strength := 0.0
|
var actual_strength := 0.0
|
||||||
@ -78,7 +78,7 @@ func _calculate_linear_velocity(
|
|||||||
var velocity := current_velocity + facing_direction * actual_strength * delta
|
var velocity := current_velocity + facing_direction * actual_strength * delta
|
||||||
velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient)
|
velocity = velocity.linear_interpolate(Vector2.ZERO, ship_drag_coefficient)
|
||||||
|
|
||||||
return velocity.clamped(max_speed)
|
return velocity.clamped(speed_max)
|
||||||
|
|
||||||
|
|
||||||
func _get_movement() -> Vector2:
|
func _get_movement() -> Vector2:
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
|
|
||||||
export(float, 0, 2000, 40) var max_linear_speed := 120.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40) var linear_speed_max := 120.0 setget set_linear_speed_max
|
||||||
export(float, 0, 200, 2) var max_linear_accel := 10.0 setget set_max_linear_accel
|
export(float, 0, 200, 2) 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
|
export(float, 0, 5, 0.1) var predict_time := 1.0 setget set_predict_time
|
||||||
|
|
||||||
onready var pursuer := $BoundaryManager/Pursuer
|
onready var pursuer := $BoundaryManager/Pursuer
|
||||||
@ -10,26 +10,26 @@ onready var seeker := $BoundaryManager/Seeker
|
|||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
pursuer.setup(predict_time, max_linear_speed, max_linear_accel)
|
pursuer.setup(predict_time, linear_speed_max, linear_accel_max)
|
||||||
seeker.setup(predict_time, max_linear_speed, max_linear_accel)
|
seeker.setup(predict_time, linear_speed_max, linear_accel_max)
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
pursuer.agent.max_linear_speed = value
|
pursuer.agent.linear_speed_max = value
|
||||||
seeker.agent.max_linear_speed = value
|
seeker.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
max_linear_accel = value
|
linear_accel_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
pursuer.agent.max_linear_acceleration = value
|
pursuer.agent.linear_acceleration_max = value
|
||||||
seeker.agent.max_linear_acceleration = value
|
seeker.agent.linear_acceleration_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_predict_time(value: float) -> void:
|
func set_predict_time(value: float) -> void:
|
||||||
@ -37,4 +37,4 @@ func set_predict_time(value: float) -> void:
|
|||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
pursuer._behavior.max_predict_time = value
|
pursuer._behavior.predict_time_max = value
|
||||||
|
@ -12,7 +12,8 @@ script = ExtResource( 4 )
|
|||||||
__meta__ = {
|
__meta__ = {
|
||||||
"_editor_description_": "Toy demo to demonstrate the use of the Pursue contrasted to the more naive Seek steering behavior."
|
"_editor_description_": "Toy demo to demonstrate the use of the Pursue contrasted to the more naive Seek steering behavior."
|
||||||
}
|
}
|
||||||
max_linear_speed = 200.0
|
linear_speed_max = 240.0
|
||||||
|
linear_accel_max = 40.0
|
||||||
|
|
||||||
[node name="BoundaryManager" type="Node2D" parent="."]
|
[node name="BoundaryManager" type="Node2D" parent="."]
|
||||||
script = ExtResource( 3 )
|
script = ExtResource( 3 )
|
||||||
@ -24,7 +25,6 @@ collision_mask = 2
|
|||||||
script = ExtResource( 2 )
|
script = ExtResource( 2 )
|
||||||
thruster_strength = 600.0
|
thruster_strength = 600.0
|
||||||
side_thruster_strength = 20.0
|
side_thruster_strength = 20.0
|
||||||
max_velocity = 900.0
|
|
||||||
|
|
||||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"]
|
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Player"]
|
||||||
polygon = PoolVector2Array( 0, -32, -24, 32, 24, 32 )
|
polygon = PoolVector2Array( 0, -32, -24, 32, 24, 32 )
|
||||||
|
@ -2,10 +2,6 @@ extends KinematicBody2D
|
|||||||
# Represents a ship that chases after the player.
|
# Represents a ship that chases after the player.
|
||||||
|
|
||||||
|
|
||||||
onready var agent := GSTSteeringAgent.new()
|
|
||||||
onready var accel := GSTTargetAcceleration.new()
|
|
||||||
onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent
|
|
||||||
|
|
||||||
export var use_seek: bool = false
|
export var use_seek: bool = false
|
||||||
|
|
||||||
var _orient_behavior: GSTSteeringBehavior
|
var _orient_behavior: GSTSteeringBehavior
|
||||||
@ -16,6 +12,10 @@ var _linear_drag_coefficient := 0.025
|
|||||||
var _angular_velocity := 0.0
|
var _angular_velocity := 0.0
|
||||||
var _angular_drag := 0.1
|
var _angular_drag := 0.1
|
||||||
|
|
||||||
|
onready var agent := GSTSteeringAgent.new()
|
||||||
|
onready var accel := GSTTargetAcceleration.new()
|
||||||
|
onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
set_physics_process(false)
|
set_physics_process(false)
|
||||||
@ -29,20 +29,20 @@ func _physics_process(delta: float) -> void:
|
|||||||
|
|
||||||
_angular_velocity = clamp(
|
_angular_velocity = clamp(
|
||||||
lerp(_angular_velocity, 0, _angular_drag),
|
lerp(_angular_velocity, 0, _angular_drag),
|
||||||
-agent.max_angular_speed,
|
-agent.angular_speed_max,
|
||||||
agent.max_angular_speed
|
agent.angular_speed_max
|
||||||
)
|
)
|
||||||
|
|
||||||
rotation += _angular_velocity * delta
|
rotation += _angular_velocity * delta
|
||||||
|
|
||||||
accel = _behavior.calculate_steering(accel)
|
accel = _behavior.calculate_steering(accel)
|
||||||
_linear_velocity += Vector2(accel.linear.x, accel.linear.y)
|
_linear_velocity += Vector2(accel.linear.x, accel.linear.y)
|
||||||
|
_linear_velocity = _linear_velocity.clamped(agent.linear_speed_max)
|
||||||
_linear_velocity = _linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
|
_linear_velocity = _linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
|
||||||
_linear_velocity = _linear_velocity.clamped(agent.max_linear_speed)
|
|
||||||
_linear_velocity = move_and_slide(_linear_velocity)
|
_linear_velocity = move_and_slide(_linear_velocity)
|
||||||
|
|
||||||
|
|
||||||
func setup(predict_time: float, max_linear_speed: float, max_linear_accel: float) -> void:
|
func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float) -> void:
|
||||||
if use_seek:
|
if use_seek:
|
||||||
_behavior = GSTSeek.new(agent, player_agent)
|
_behavior = GSTSeek.new(agent, player_agent)
|
||||||
else:
|
else:
|
||||||
@ -52,10 +52,10 @@ func setup(predict_time: float, max_linear_speed: float, max_linear_accel: float
|
|||||||
_orient_behavior.alignment_tolerance = 0.001
|
_orient_behavior.alignment_tolerance = 0.001
|
||||||
_orient_behavior.deceleration_radius = PI/2
|
_orient_behavior.deceleration_radius = PI/2
|
||||||
|
|
||||||
agent.max_angular_acceleration = 2
|
agent.angular_acceleration_max = deg2rad(10)
|
||||||
agent.max_angular_speed = 5
|
agent.angular_speed_max = deg2rad(45)
|
||||||
agent.max_linear_acceleration = max_linear_accel
|
agent.linear_acceleration_max = linear_accel_max
|
||||||
agent.max_linear_speed = max_linear_speed
|
agent.linear_speed_max = linear_speed_max
|
||||||
|
|
||||||
_update_agent()
|
_update_agent()
|
||||||
set_physics_process(true)
|
set_physics_process(true)
|
||||||
|
@ -5,8 +5,8 @@ extends Node2D
|
|||||||
enum Mode { FLEE, SEEK }
|
enum Mode { FLEE, SEEK }
|
||||||
|
|
||||||
export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
|
export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
|
||||||
export(float, 0, 2000, 40) var max_linear_speed := 200.0 setget set_max_linear_speed
|
export(float, 0, 2000, 40) var linear_speed_max := 200.0 setget set_linear_speed_max
|
||||||
export(float, 0, 500, 0.5) var max_linear_accel := 10.0 setget set_max_linear_accel
|
export(float, 0, 500, 0.5) var linear_accel_max := 10.0 setget set_linear_accel_max
|
||||||
export(float) var player_speed := 600.0 setget set_player_speed
|
export(float) var player_speed := 600.0 setget set_player_speed
|
||||||
|
|
||||||
var camera_boundaries: Rect2
|
var camera_boundaries: Rect2
|
||||||
@ -37,8 +37,8 @@ func _ready() -> void:
|
|||||||
var entity: KinematicBody2D = spawner.Entity.instance()
|
var entity: KinematicBody2D = spawner.Entity.instance()
|
||||||
entity.global_position = new_pos
|
entity.global_position = new_pos
|
||||||
entity.player_agent = player.agent
|
entity.player_agent = player.agent
|
||||||
entity.start_speed = max_linear_speed
|
entity.start_speed = linear_speed_max
|
||||||
entity.start_accel = max_linear_accel
|
entity.start_accel = linear_accel_max
|
||||||
spawner.add_child(entity)
|
spawner.add_child(entity)
|
||||||
|
|
||||||
|
|
||||||
@ -56,22 +56,22 @@ func set_behavior_mode(mode: int) -> void:
|
|||||||
child.use_seek = false
|
child.use_seek = false
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_speed(value: float) -> void:
|
func set_linear_speed_max(value: float) -> void:
|
||||||
max_linear_speed = value
|
linear_speed_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
for child in spawner.get_children():
|
for child in spawner.get_children():
|
||||||
child.agent.max_linear_speed = value
|
child.agent.linear_speed_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_max_linear_accel(value: float) -> void:
|
func set_linear_accel_max(value: float) -> void:
|
||||||
max_linear_accel = value
|
linear_accel_max = value
|
||||||
if not is_inside_tree():
|
if not is_inside_tree():
|
||||||
return
|
return
|
||||||
|
|
||||||
for child in spawner.get_children():
|
for child in spawner.get_children():
|
||||||
child.agent.max_linear_acceleration = value
|
child.agent.linear_acceleration_max = value
|
||||||
|
|
||||||
|
|
||||||
func set_player_speed(value: float) -> void:
|
func set_player_speed(value: float) -> void:
|
||||||
|
@ -2,21 +2,21 @@ extends KinematicBody2D
|
|||||||
# AI agent that uses the Seek behavior to hone in on the player's location as directly as possible.
|
# AI agent that uses the Seek behavior to hone in on the player's location as directly as possible.
|
||||||
|
|
||||||
|
|
||||||
onready var agent := GSTSteeringAgent.new()
|
|
||||||
onready var accel := GSTTargetAcceleration.new()
|
|
||||||
onready var seek := GSTSeek.new(agent, player_agent)
|
|
||||||
onready var flee := GSTFlee.new(agent, player_agent)
|
|
||||||
|
|
||||||
var player_agent: GSTAgentLocation
|
var player_agent: GSTAgentLocation
|
||||||
var velocity := Vector2.ZERO
|
var velocity := Vector2.ZERO
|
||||||
var start_speed: float
|
var start_speed: float
|
||||||
var start_accel: float
|
var start_accel: float
|
||||||
var use_seek := true
|
var use_seek := true
|
||||||
|
|
||||||
|
onready var agent := GSTSteeringAgent.new()
|
||||||
|
onready var accel := GSTTargetAcceleration.new()
|
||||||
|
onready var seek := GSTSeek.new(agent, player_agent)
|
||||||
|
onready var flee := GSTFlee.new(agent, player_agent)
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
agent.max_linear_acceleration = start_accel
|
agent.linear_acceleration_max = start_accel
|
||||||
agent.max_linear_speed = start_speed
|
agent.linear_speed_max = start_speed
|
||||||
|
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
@ -29,7 +29,7 @@ func _physics_process(delta: float) -> void:
|
|||||||
else:
|
else:
|
||||||
accel = flee.calculate_steering(accel)
|
accel = flee.calculate_steering(accel)
|
||||||
|
|
||||||
velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.max_linear_speed)
|
velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.linear_speed_max)
|
||||||
velocity = move_and_slide(velocity)
|
velocity = move_and_slide(velocity)
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G
|
|||||||
if distance <= arrival_tolerance:
|
if distance <= arrival_tolerance:
|
||||||
acceleration.set_zero()
|
acceleration.set_zero()
|
||||||
else:
|
else:
|
||||||
var desired_speed := agent.max_linear_speed
|
var desired_speed := agent.linear_speed_max
|
||||||
|
|
||||||
if distance <= deceleration_radius:
|
if distance <= deceleration_radius:
|
||||||
desired_speed *= distance / deceleration_radius
|
desired_speed *= distance / deceleration_radius
|
||||||
@ -30,7 +30,7 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G
|
|||||||
|
|
||||||
desired_velocity = (desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach
|
desired_velocity = (desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach
|
||||||
|
|
||||||
acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.max_linear_acceleration)
|
acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
|
||||||
acceleration.angular = 0
|
acceleration.angular = 0
|
||||||
|
|
||||||
return acceleration
|
return acceleration
|
||||||
|
@ -33,7 +33,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
|
|||||||
else:
|
else:
|
||||||
acceleration.linear = first_relative_position + (first_relative_velocity * shortest_time)
|
acceleration.linear = first_relative_position + (first_relative_velocity * shortest_time)
|
||||||
|
|
||||||
acceleration.linear = acceleration.linear.normalized() * -agent.max_linear_acceleration
|
acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max
|
||||||
acceleration.angular = 0
|
acceleration.angular = 0
|
||||||
|
|
||||||
return acceleration
|
return acceleration
|
||||||
|
@ -38,7 +38,11 @@ func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAccel
|
|||||||
|
|
||||||
blended_accel.add_scaled_accel(_accel, bw.weight)
|
blended_accel.add_scaled_accel(_accel, bw.weight)
|
||||||
|
|
||||||
blended_accel.linear = GSTUtils.clampedv3(blended_accel.linear, agent.max_linear_acceleration)
|
blended_accel.linear = GSTUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max)
|
||||||
blended_accel.angular = min(blended_accel.angular, agent.max_angular_acceleration)
|
blended_accel.angular = clamp(
|
||||||
|
blended_accel.angular,
|
||||||
|
-agent.angular_acceleration_max,
|
||||||
|
agent.angular_acceleration_max
|
||||||
|
)
|
||||||
|
|
||||||
return blended_accel
|
return blended_accel
|
||||||
|
@ -17,7 +17,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
|
|||||||
var neighbor_count = proximity.find_neighbors(_callback)
|
var neighbor_count = proximity.find_neighbors(_callback)
|
||||||
if neighbor_count > 0:
|
if neighbor_count > 0:
|
||||||
center_of_mass *= 1.0 / neighbor_count
|
center_of_mass *= 1.0 / neighbor_count
|
||||||
acceleration.linear = (center_of_mass - agent.position).normalized() * agent.max_linear_acceleration
|
acceleration.linear = (center_of_mass - agent.position).normalized() * agent.linear_acceleration_max
|
||||||
return acceleration
|
return acceleration
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,15 +2,15 @@ class_name GSTEvade
|
|||||||
extends GSTPursue
|
extends GSTPursue
|
||||||
# Calculates acceleration to take an agent away from where a target agent will be.
|
# Calculates acceleration to take an agent away from where a target agent will be.
|
||||||
|
|
||||||
# # The `max_predict_time` variable represents how far ahead to calculate the intersection point.
|
# # The `predict_time_max` variable represents how far ahead to calculate the intersection point.
|
||||||
|
|
||||||
|
|
||||||
func _init(
|
func _init(
|
||||||
agent: GSTSteeringAgent,
|
agent: GSTSteeringAgent,
|
||||||
target: GSTSteeringAgent,
|
target: GSTSteeringAgent,
|
||||||
max_predict_time := 1.0).(agent, target, max_predict_time):
|
predict_time_max := 1.0).(agent, target, predict_time_max):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func _get_modified_acceleration() -> float:
|
func _get_modified_acceleration() -> float:
|
||||||
return -agent.max_linear_acceleration
|
return -agent.linear_acceleration_max
|
||||||
|
@ -9,7 +9,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) ->
|
|||||||
|
|
||||||
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
||||||
acceleration.linear = (
|
acceleration.linear = (
|
||||||
(agent.position - target.position).normalized() * agent.max_linear_acceleration)
|
(agent.position - target.position).normalized() * agent.linear_acceleration_max)
|
||||||
acceleration.angular = 0
|
acceleration.angular = 0
|
||||||
|
|
||||||
return acceleration
|
return acceleration
|
||||||
|
@ -41,7 +41,7 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
|
|||||||
return _arrive(acceleration, target_position)
|
return _arrive(acceleration, target_position)
|
||||||
|
|
||||||
acceleration.linear = (target_position - agent.position).normalized()
|
acceleration.linear = (target_position - agent.position).normalized()
|
||||||
acceleration.linear *= agent.max_linear_acceleration
|
acceleration.linear *= agent.linear_acceleration_max
|
||||||
acceleration.angular = 0
|
acceleration.angular = 0
|
||||||
|
|
||||||
return acceleration
|
return acceleration
|
||||||
|
@ -22,7 +22,7 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation
|
|||||||
if rotation_size <= alignment_tolerance:
|
if rotation_size <= alignment_tolerance:
|
||||||
acceleration.set_zero()
|
acceleration.set_zero()
|
||||||
else:
|
else:
|
||||||
var desired_rotation := agent.max_angular_speed
|
var desired_rotation := agent.angular_speed_max
|
||||||
|
|
||||||
if rotation_size <= deceleration_radius:
|
if rotation_size <= deceleration_radius:
|
||||||
desired_rotation *= rotation_size / deceleration_radius
|
desired_rotation *= rotation_size / deceleration_radius
|
||||||
@ -32,8 +32,8 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation
|
|||||||
acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach
|
acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach
|
||||||
|
|
||||||
var limited_acceleration := abs(acceleration.angular)
|
var limited_acceleration := abs(acceleration.angular)
|
||||||
if limited_acceleration > agent.max_angular_acceleration:
|
if limited_acceleration > agent.angular_acceleration_max:
|
||||||
acceleration.angular *= agent.max_angular_acceleration / limited_acceleration
|
acceleration.angular *= agent.angular_acceleration_max / limited_acceleration
|
||||||
|
|
||||||
acceleration.linear = Vector3.ZERO
|
acceleration.linear = Vector3.ZERO
|
||||||
|
|
||||||
|
@ -7,11 +7,11 @@ extends GSTSteeringBehavior
|
|||||||
var _behaviors := []
|
var _behaviors := []
|
||||||
|
|
||||||
var last_selected_index: int
|
var last_selected_index: int
|
||||||
var threshold_for_zero: float
|
var zero_threshold: float
|
||||||
|
|
||||||
|
|
||||||
func _init(agent: GSTSteeringAgent, threshold_for_zero := 0.001).(agent) -> void:
|
func _init(agent: GSTSteeringAgent, zero_threshold := 0.001).(agent) -> void:
|
||||||
self.threshold_for_zero = threshold_for_zero
|
self.zero_threshold = zero_threshold
|
||||||
|
|
||||||
|
|
||||||
func add(behavior: GSTSteeringBehavior) -> void:
|
func add(behavior: GSTSteeringBehavior) -> void:
|
||||||
@ -26,7 +26,7 @@ func get_behavior_at(index: int) -> GSTSteeringBehavior:
|
|||||||
|
|
||||||
|
|
||||||
func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
||||||
var threshold_squared := threshold_for_zero * threshold_for_zero
|
var threshold_squared := zero_threshold * zero_threshold
|
||||||
|
|
||||||
last_selected_index = -1
|
last_selected_index = -1
|
||||||
|
|
||||||
|
@ -2,19 +2,19 @@ class_name GSTPursue
|
|||||||
extends GSTSteeringBehavior
|
extends GSTSteeringBehavior
|
||||||
# Calculates acceleration to take an agent to intersect with where a target agent will be.
|
# Calculates acceleration to take an agent to intersect with where a target agent will be.
|
||||||
|
|
||||||
# # The `max_predict_time` variable represents how far ahead to calculate the intersection point.
|
# # The `predict_time_max` variable represents how far ahead to calculate the intersection point.
|
||||||
|
|
||||||
|
|
||||||
var target: GSTSteeringAgent
|
var target: GSTSteeringAgent
|
||||||
var max_predict_time: float
|
var predict_time_max: float
|
||||||
|
|
||||||
|
|
||||||
func _init(
|
func _init(
|
||||||
agent: GSTSteeringAgent,
|
agent: GSTSteeringAgent,
|
||||||
target: GSTSteeringAgent,
|
target: GSTSteeringAgent,
|
||||||
max_predict_time := 1.0).(agent) -> void:
|
predict_time_max := 1.0).(agent) -> void:
|
||||||
self.target = target
|
self.target = target
|
||||||
self.max_predict_time = max_predict_time
|
self.predict_time_max = predict_time_max
|
||||||
|
|
||||||
|
|
||||||
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
||||||
@ -22,11 +22,11 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
|
|||||||
var distance_squared := (target_position - agent.position).length_squared()
|
var distance_squared := (target_position - agent.position).length_squared()
|
||||||
|
|
||||||
var speed_squared := agent.linear_velocity.length_squared()
|
var speed_squared := agent.linear_velocity.length_squared()
|
||||||
var predict_time := max_predict_time
|
var predict_time := predict_time_max
|
||||||
|
|
||||||
if speed_squared > 0:
|
if speed_squared > 0:
|
||||||
var predict_time_squared := distance_squared / speed_squared
|
var predict_time_squared := distance_squared / speed_squared
|
||||||
if predict_time_squared < max_predict_time * max_predict_time:
|
if predict_time_squared < predict_time_max * predict_time_max:
|
||||||
predict_time = sqrt(predict_time_squared)
|
predict_time = sqrt(predict_time_squared)
|
||||||
|
|
||||||
acceleration.linear = ((
|
acceleration.linear = ((
|
||||||
@ -39,4 +39,4 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
|
|||||||
|
|
||||||
|
|
||||||
func _get_modified_acceleration() -> float:
|
func _get_modified_acceleration() -> float:
|
||||||
return agent.max_linear_acceleration
|
return agent.linear_acceleration_max
|
||||||
|
@ -12,7 +12,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
|
|||||||
|
|
||||||
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
||||||
acceleration.linear = (
|
acceleration.linear = (
|
||||||
(target.position - agent.position).normalized() * agent.max_linear_acceleration)
|
(target.position - agent.position).normalized() * agent.linear_acceleration_max)
|
||||||
acceleration.angular = 0
|
acceleration.angular = 0
|
||||||
|
|
||||||
return acceleration
|
return acceleration
|
||||||
|
@ -28,11 +28,11 @@ func report_neighbor(neighbor: GSTSteeringAgent) -> bool:
|
|||||||
var to_agent := agent.position - neighbor.position
|
var to_agent := agent.position - neighbor.position
|
||||||
|
|
||||||
var distance_squared := to_agent.length_squared()
|
var distance_squared := to_agent.length_squared()
|
||||||
var max_acceleration := agent.max_linear_acceleration
|
var acceleration_max := agent.linear_acceleration_max
|
||||||
|
|
||||||
var strength := decay_coefficient / distance_squared
|
var strength := decay_coefficient / distance_squared
|
||||||
if strength > max_acceleration:
|
if strength > acceleration_max:
|
||||||
strength = max_acceleration
|
strength = acceleration_max
|
||||||
|
|
||||||
acceleration.linear += to_agent * (strength / sqrt(distance_squared))
|
acceleration.linear += to_agent * (strength / sqrt(distance_squared))
|
||||||
|
|
||||||
|
@ -4,10 +4,10 @@ class_name GSTSteeringAgent
|
|||||||
|
|
||||||
|
|
||||||
var zero_linear_speed_threshold := 0.01
|
var zero_linear_speed_threshold := 0.01
|
||||||
var max_linear_speed := 0.0
|
var linear_speed_max := 0.0
|
||||||
var max_linear_acceleration := 0.0
|
var linear_acceleration_max := 0.0
|
||||||
var max_angular_speed := 0.0
|
var angular_speed_max := 0.0
|
||||||
var max_angular_acceleration := 0.0
|
var angular_acceleration_max := 0.0
|
||||||
var linear_velocity := Vector3.ZERO
|
var linear_velocity := Vector3.ZERO
|
||||||
var angular_velocity := 0.0
|
var angular_velocity := 0.0
|
||||||
var bounding_radius := 0.0
|
var bounding_radius := 0.0
|
||||||
|
Loading…
Reference in New Issue
Block a user