Merge pull request #17 from GDQuest/features/special-agents

Introduce specialized smart agents
This commit is contained in:
Francois Belair 2020-02-07 09:56:33 -05:00 committed by GitHub
commit 50a141bbcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 566 additions and 2404 deletions

View File

@ -1,7 +1,7 @@
extends KinematicBody2D
var agent := GSTSteeringAgent.new()
var agent := GSTKinematicBody2DAgent.new(self)
var target := GSTAgentLocation.new()
var arrive := GSTArrive.new(agent, target)
var _accel := GSTTargetAcceleration.new()
@ -11,11 +11,8 @@ var _drag := 0.1
func _physics_process(delta: float) -> void:
_update_agent()
_accel = arrive.calculate_steering(_accel)
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag).clamped(agent.linear_speed_max)
_velocity = move_and_slide(_velocity)
arrive.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
func setup(
@ -26,12 +23,7 @@ func setup(
) -> void:
agent.linear_speed_max = linear_speed_max
agent.linear_acceleration_max = linear_acceleration_max
agent.position = Vector3(global_position.x, global_position.y, 0)
agent.linear_drag_percentage = _drag
arrive.deceleration_radius = deceleration_radius
arrive.arrival_tolerance = arrival_tolerance
target.position = agent.position
func _update_agent() -> void:
agent.position = Vector3(global_position.x, global_position.y, 0)
agent.linear_velocity = Vector3(_velocity.x, _velocity.y, 0)

View File

@ -13,7 +13,7 @@ var _drag := 0.1
var _color := Color(0.4, 1.0, 0.89, 0.3)
onready var collision := $CollisionShape2D
onready var agent := GSTSteeringAgent.new()
onready var agent := GSTKinematicBody2DAgent.new(self)
onready var proximity := GSTRadiusProximity.new(agent, [], 140)
onready var avoid := GSTAvoidCollisions.new(agent, proximity)
onready var target := GSTAgentLocation.new()
@ -27,12 +27,11 @@ func _draw() -> void:
func _physics_process(delta: float) -> void:
_update_agent()
_accel = priority.calculate_steering(_accel)
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
_velocity = _velocity.clamped(agent.linear_speed_max)
_velocity = move_and_slide(_velocity)
target.position.x = agent.position.x + _direction.x*_radius
target.position.y = agent.position.y + _direction.y*_radius
priority.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
func setup(
@ -46,15 +45,19 @@ func setup(
) -> void:
rng.randomize()
_direction = Vector2(rng.randf_range(-1, 1), rng.randf_range(-1, 1)).normalized()
_update_agent()
agent.linear_speed_max = linear_speed_max
agent.linear_acceleration_max = linear_accel_max
proximity.radius = proximity_radius
_boundary_bottom = boundary_bottom
_boundary_right = boundary_right
_radius = collision.shape.radius
agent.bounding_radius = _radius
agent.linear_drag_percentage = _drag
self.draw_proximity = draw_proximity
priority.add(avoid)
@ -84,12 +87,3 @@ func set_random_nonoverlapping_position(others: Array, distance_from_boundary_mi
done = false
if done:
break
func _update_agent() -> void:
agent.position.x = global_position.x
agent.position.y = global_position.y
agent.linear_velocity.x = _velocity.x
agent.linear_velocity.y = _velocity.y
target.position.x = agent.position.x + _direction.x*_radius
target.position.y = agent.position.y + _direction.y*_radius

View File

@ -2,7 +2,7 @@ extends KinematicBody2D
var face: GSTFace
var agent := GSTSteeringAgent.new()
var agent := GSTKinematicBody2DAgent.new(self)
var _accel := GSTTargetAcceleration.new()
var _angular_drag := 0.1
@ -19,15 +19,8 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
_accel = face.calculate_steering(_accel)
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.orientation += agent.angular_velocity * delta
rotation = agent.orientation
face.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
func _draw() -> void:
@ -48,5 +41,4 @@ func setup(
agent.angular_acceleration_max = angular_accel_max
agent.angular_speed_max = angular_speed_max
agent.position = Vector3(global_position.x, global_position.y, 0)
agent.angular_drag_percentage = _angular_drag

View File

@ -6,7 +6,7 @@ var _accel := GSTTargetAcceleration.new()
var _valid := false
var _drag := 0.1
onready var agent := GSTSteeringAgent.new()
onready var agent := GSTKinematicBody2DAgent.new(self)
onready var path := GSTPath.new([
Vector3(global_position.x, global_position.y, 0),
Vector3(global_position.x, global_position.y, 0)
@ -25,27 +25,18 @@ func setup(
owner.drawer.connect("path_established", self, "_on_Drawer_path_established")
follow.path_offset = path_offset
follow.prediction_time = predict_time
agent.linear_acceleration_max = accel_max
agent.linear_speed_max = speed_max
follow.deceleration_radius = decel_radius
follow.arrival_tolerance = arrival_tolerance
agent.linear_acceleration_max = accel_max
agent.linear_speed_max = speed_max
agent.linear_drag_percentage = _drag
func _physics_process(delta: float) -> void:
if _valid:
_update_agent()
_accel = follow.calculate_steering(_accel)
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
_velocity = _velocity.linear_interpolate(Vector2.ZERO, _drag)
_velocity = _velocity.clamped(agent.linear_speed_max)
_velocity = move_and_slide(_velocity)
func _update_agent() -> void:
agent.position.x = global_position.x
agent.position.y = global_position.y
agent.linear_velocity.x = _velocity.x
agent.linear_velocity.y = _velocity.y
follow.calculate_steering(_accel)
agent._apply_steering(_accel, delta)
func _on_Drawer_path_established(points: Array) -> void:

View File

@ -4,7 +4,7 @@ extends KinematicBody2D
var separation: GSTSeparation
var cohesion: GSTCohesion
var proximity: GSTRadiusProximity
var agent := GSTSteeringAgent.new()
var agent := GSTKinematicBody2DAgent.new(self)
var blend := GSTBlend.new(agent)
var acceleration := GSTTargetAcceleration.new()
var draw_proximity := false
@ -28,6 +28,7 @@ func setup(
agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max
agent.linear_drag_percentage = 0.1
proximity = GSTRadiusProximity.new(agent, [], proximity_radius)
separation = GSTSeparation.new(agent, proximity)
@ -43,14 +44,9 @@ func _draw() -> void:
func _physics_process(delta: float) -> void:
agent.position.x = global_position.x
agent.position.y = global_position.y
if blend:
acceleration = blend.calculate_steering(acceleration)
_velocity += Vector2(acceleration.linear.x, acceleration.linear.y)
_velocity = _velocity.linear_interpolate(Vector2.ZERO, 0.1)
_velocity = _velocity.clamped(agent.linear_speed_max)
move_and_slide(_velocity)
blend.calculate_steering(acceleration)
agent._apply_steering(acceleration, delta)
func set_neighbors(neighbor: Array) -> void:

View File

@ -4,71 +4,68 @@ extends KinematicBody2D
export var use_seek: bool = false
var _orient_behavior: GSTSteeringBehavior
var _behavior: GSTSteeringBehavior
var _blend: GSTBlend
var _linear_velocity := Vector2()
var _linear_drag_coefficient := 0.025
var _angular_velocity := 0.0
var _angular_drag := 0.1
var _direction_face := GSTAgentLocation.new()
onready var agent := GSTSteeringAgent.new()
onready var agent := GSTKinematicBody2DAgent.new(self)
onready var accel := GSTTargetAcceleration.new()
onready var player_agent: GSTSteeringAgent = owner.find_node("Player", true, false).agent
func _ready() -> void:
agent.calculate_velocities = false
set_physics_process(false)
func _physics_process(delta: float) -> void:
_update_agent()
accel = _behavior.calculate_steering(accel)
_direction_face.position = agent.position + accel.linear.normalized()
accel = _orient_behavior.calculate_steering(accel)
_angular_velocity += accel.angular
_angular_velocity = clamp(
lerp(_angular_velocity, 0, _angular_drag),
_blend.calculate_steering(accel)
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)
rotation += agent.angular_velocity * delta
rotation += _angular_velocity * delta
var linear_velocity := (
GSTUtils.to_vector2(agent.linear_velocity) +
(GSTUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max)
)
linear_velocity = linear_velocity.clamped(agent.linear_speed_max)
linear_velocity = linear_velocity.linear_interpolate(
Vector2.ZERO,
_linear_drag_coefficient
)
_linear_velocity += GSTUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max
_linear_velocity = _linear_velocity.clamped(agent.linear_speed_max)
_linear_velocity = _linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
_linear_velocity = move_and_slide(_linear_velocity)
linear_velocity = move_and_slide(linear_velocity)
agent.linear_velocity = GSTUtils.to_vector3(linear_velocity)
func setup(predict_time: float, linear_speed_max: float, linear_accel_max: float) -> void:
var behavior: GSTSteeringBehavior
if use_seek:
_behavior = GSTSeek.new(agent, player_agent)
behavior = GSTSeek.new(agent, player_agent)
else:
_behavior = GSTPursue.new(agent, player_agent, predict_time)
behavior = GSTPursue.new(agent, player_agent, predict_time)
_orient_behavior = GSTFace.new(agent, _direction_face)
_orient_behavior.alignment_tolerance = deg2rad(5)
_orient_behavior.deceleration_radius = deg2rad(5)
var orient_behavior := GSTFace.new(agent, _direction_face)
orient_behavior.alignment_tolerance = deg2rad(5)
orient_behavior.deceleration_radius = deg2rad(5)
_blend = GSTBlend.new(agent)
_blend.add(behavior, 1)
_blend.add(orient_behavior, 1)
agent.angular_acceleration_max = deg2rad(40)
agent.angular_speed_max = deg2rad(90)
agent.linear_acceleration_max = linear_accel_max
agent.linear_speed_max = linear_speed_max
_update_agent()
set_physics_process(true)
func _update_agent() -> void:
agent.position.x = global_position.x
agent.position.y = global_position.y
agent.orientation = rotation
agent.linear_velocity.x = _linear_velocity.x
agent.linear_velocity.y = _linear_velocity.y
agent.angular_velocity = _angular_velocity

View File

@ -1,5 +1,4 @@
extends KinematicBody2D
# AI agent that uses the Seek behavior to hone in on the player's location as directly as possible.
var player_agent: GSTAgentLocation
@ -8,7 +7,7 @@ var start_speed: float
var start_accel: float
var use_seek := true
onready var agent := GSTSteeringAgent.new()
onready var agent := GSTKinematicBody2DAgent.new(self)
onready var accel := GSTTargetAcceleration.new()
onready var seek := GSTSeek.new(agent, player_agent)
onready var flee := GSTFlee.new(agent, player_agent)
@ -23,18 +22,9 @@ func _physics_process(delta: float) -> void:
if not player_agent:
return
_update_agent()
if use_seek:
accel = seek.calculate_steering(accel)
seek.calculate_steering(accel)
else:
accel = flee.calculate_steering(accel)
flee.calculate_steering(accel)
velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.linear_speed_max)
velocity = move_and_slide(velocity)
func _update_agent() -> void:
agent.position.x = global_position.x
agent.position.y = global_position.y
agent.linear_velocity.x = velocity.x
agent.linear_velocity.y = velocity.y
agent._apply_steering(accel, delta)

View File

@ -64,6 +64,16 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Proximities/GSTInfiniteProximity.gd"
}, {
"base": "GSTSpecializedAgent",
"class": "GSTKinematicBody2DAgent",
"language": "GDScript",
"path": "res://src/Agents/GSTKinematicBody2DAgent.gd"
}, {
"base": "GSTSpecializedAgent",
"class": "GSTKinematicBodyAgent",
"language": "GDScript",
"path": "res://src/Agents/GSTKinematicBodyAgent.gd"
}, {
"base": "GSTMatchOrientation",
"class": "GSTLookWhereYouGo",
"language": "GDScript",
@ -99,6 +109,16 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Proximities/GSTRadiusProximity.gd"
}, {
"base": "GSTSpecializedAgent",
"class": "GSTRigidBody2DAgent",
"language": "GDScript",
"path": "res://src/Agents/GSTRigidBody2DAgent.gd"
}, {
"base": "GSTSpecializedAgent",
"class": "GSTRigidBodyAgent",
"language": "GDScript",
"path": "res://src/Agents/GSTRigidBodyAgent.gd"
}, {
"base": "GSTSteeringBehavior",
"class": "GSTSeek",
"language": "GDScript",
@ -109,6 +129,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Behaviors/GSTSeparation.gd"
}, {
"base": "GSTSteeringAgent",
"class": "GSTSpecializedAgent",
"language": "GDScript",
"path": "res://src/Agents/GSTSpecializedAgent.gd"
}, {
"base": "GSTAgentLocation",
"class": "GSTSteeringAgent",
"language": "GDScript",
@ -128,6 +153,11 @@ _global_script_classes=[ {
"class": "GSTUtils",
"language": "GDScript",
"path": "res://src/GSTUtils.gd"
}, {
"base": "EditorScript",
"class": "ReferenceCollector",
"language": "GDScript",
"path": "res://ReferenceCollector.gd"
} ]
_global_script_class_icons={
"GSTAgentLocation": "",
@ -141,6 +171,8 @@ _global_script_class_icons={
"GSTFollowPath": "",
"GSTGroupBehavior": "",
"GSTInfiniteProximity": "",
"GSTKinematicBody2DAgent": "",
"GSTKinematicBodyAgent": "",
"GSTLookWhereYouGo": "",
"GSTMatchOrientation": "",
"GSTPath": "",
@ -148,12 +180,16 @@ _global_script_class_icons={
"GSTProximity": "",
"GSTPursue": "",
"GSTRadiusProximity": "",
"GSTRigidBody2DAgent": "",
"GSTRigidBodyAgent": "",
"GSTSeek": "",
"GSTSeparation": "",
"GSTSpecializedAgent": "",
"GSTSteeringAgent": "",
"GSTSteeringBehavior": "",
"GSTTargetAcceleration": "",
"GSTUtils": ""
"GSTUtils": "",
"ReferenceCollector": ""
}
[application]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,134 @@
# A specialized steering agent that updates itself every frame so the user does
# not have to using a KinematicBody2D
extends GSTSpecializedAgent
class_name GSTKinematicBody2DAgent
# SLIDE uses `move_and_slide`
# COLLIDE uses `move_and_collide`
# POSITION changes the `global_position` directly
enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody2D to keep track of
var body: KinematicBody2D setget _set_body
# The type of movement the body executes
var movement_type: int
var _last_position: Vector2
func _init(body: KinematicBody2D, movement_type: int = MovementType.SLIDE) -> void:
if not body.is_inside_tree():
yield(body, "ready")
self.body = body
self.movement_type = movement_type
body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
match movement_type:
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_:
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
func _apply_sliding_steering(accel: Vector3) -> void:
var velocity := GSTUtils.to_vector2(linear_velocity + accel).clamped(linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector2.ZERO, linear_drag_percentage)
velocity = body.move_and_slide(velocity)
if calculate_velocities:
linear_velocity = GSTUtils.to_vector3(velocity)
func _apply_collide_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
body.move_and_collide(GSTUtils.to_vector2(velocity) * delta)
if calculate_velocities:
linear_velocity = velocity
func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
body.global_position += GSTUtils.to_vector2(velocity) * delta
if calculate_velocities:
linear_velocity = velocity
func _apply_orientation_steering(angular_acceleration: float, delta: float) -> void:
var velocity = angular_velocity + angular_acceleration
if apply_angular_drag:
velocity = lerp(velocity, 0, angular_drag_percentage)
body.rotation += velocity * delta
if calculate_velocities:
angular_velocity = velocity
func _set_body(value: KinematicBody2D) -> void:
body = value
_last_position = body.global_position
_last_orientation = body.rotation
position = GSTUtils.to_vector3(_last_position)
orientation = _last_orientation
func _on_SceneTree_physics_frame() -> void:
var current_position: Vector2 = body.global_position
var current_orientation: float = body.rotation
position = GSTUtils.to_vector3(current_position)
orientation = current_orientation
if calculate_velocities:
if _applied_steering:
_applied_steering = false
else:
linear_velocity = GSTUtils.clampedv3(
GSTUtils.to_vector3(_last_position - current_position),
linear_speed_max
)
if apply_linear_drag:
linear_velocity = linear_velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
angular_velocity = clamp(
_last_orientation - current_orientation,
-angular_speed_max,
angular_speed_max
)
if apply_angular_drag:
angular_velocity = lerp(
angular_velocity,
0,
angular_drag_percentage
)
_last_position = current_position
_last_orientation = current_orientation

View File

@ -0,0 +1,133 @@
# A specialized steering agent that updates itself every frame so the user does
# not have to using a KinematicBody
extends GSTSpecializedAgent
class_name GSTKinematicBodyAgent
# SLIDE uses `move_and_slide`
# COLLIDE uses `move_and_collide`
# POSITION changes the global_position directly
enum MovementType { SLIDE, COLLIDE, POSITION }
# The KinematicBody to keep track of
var body: KinematicBody setget _set_body
# The type of movement the body executes
var movement_type: int
var _last_position: Vector3
func _init(body: KinematicBody, movement_type: int = MovementType.SLIDE) -> void:
if not body.is_inside_tree():
yield(body, "ready")
self.body = body
self.movement_type = movement_type
body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
match movement_type:
MovementType.COLLIDE:
_apply_collide_steering(acceleration.linear, delta)
MovementType.SLIDE:
_apply_sliding_steering(acceleration.linear)
_:
_apply_position_steering(acceleration.linear, delta)
_apply_orientation_steering(acceleration.angular, delta)
func _apply_sliding_steering(accel: Vector3) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
velocity = body.move_and_slide(velocity)
if calculate_velocities:
linear_velocity = velocity
func _apply_collide_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
body.move_and_collide(velocity * delta)
if calculate_velocities:
linear_velocity = velocity
func _apply_position_steering(accel: Vector3, delta: float) -> void:
var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max)
if apply_linear_drag:
velocity = velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
body.global_position += velocity * delta
if calculate_velocities:
linear_velocity = velocity
func _apply_orientation_steering(angular_acceleration: float, delta: float) -> void:
var velocity = angular_velocity + angular_acceleration
if apply_angular_drag:
velocity = lerp(velocity, 0, angular_drag_percentage)
body.rotation.y += velocity * delta
if calculate_velocities:
angular_velocity = velocity
func _set_body(value: KinematicBody) -> void:
body = value
_last_position = body.global_position
_last_orientation = body.rotation.y
position = _last_position
orientation = _last_orientation
func _on_SceneTree_physics_frame() -> void:
var current_position: Vector3 = body.global_position
var current_orientation: float = body.rotation.y
position = current_position
orientation = current_orientation
if calculate_velocities:
if _applied_steering:
_applied_steering = false
else:
linear_velocity = GSTUtils.clampedv3(
_last_position - current_position,
linear_speed_max
)
if apply_linear_drag:
linear_velocity = linear_velocity.linear_interpolate(
Vector3.ZERO,
linear_drag_percentage
)
angular_velocity = clamp(
_last_orientation - current_orientation,
-angular_speed_max,
angular_speed_max
)
if apply_angular_drag:
angular_velocity = lerp(
angular_velocity,
0,
angular_drag_percentage
)
_last_position = current_position
_last_orientation = current_orientation

View File

@ -0,0 +1,58 @@
# A specialized steering agent that updates itself every frame so the user does
# not have to using a RigidBody2D
extends GSTSpecializedAgent
class_name GSTRigidBody2DAgent
# The RigidBody2D to keep track of
var body: RigidBody2D setget _set_body
var _last_position: Vector2
func _init(body: RigidBody2D) -> void:
if not body.is_inside_tree():
yield(body, "ready")
self.body = body
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
body.apply_central_impulse(GSTUtils.to_vector2(acceleration.linear))
body.apply_torque_impulse(acceleration.angular)
if calculate_velocities:
linear_velocity = GSTUtils.to_vector3(body.linear_velocity)
angular_velocity = body.angular_velocity
func _set_body(value: RigidBody2D) -> void:
body = value
_last_position = body.global_position
_last_orientation = body.rotation
position = GSTUtils.to_vector3(_last_position)
orientation = _last_orientation
func _on_body_ready() -> void:
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
_set_body(body)
func _on_SceneTree_frame() -> void:
var current_position: Vector2 = body.global_position
var current_orientation: float = body.rotation
position = GSTUtils.to_vector3(current_position)
orientation = current_orientation
if calculate_velocities:
if _applied_steering:
_applied_steering = false
else:
linear_velocity = GSTUtils.to_vector3(body.linear_velocity)
angular_velocity = body.angular_velocity

View File

@ -0,0 +1,60 @@
# A specialized steering agent that updates itself every frame so the user does
# not have to using a RigidBody
extends GSTSpecializedAgent
class_name GSTRigidBodyAgent
# The RigidBody to keep track of
var body: RigidBody setget _set_body
var _last_position: Vector3
func _init(body: RigidBody) -> void:
if not body.is_inside_tree():
yield(body, "ready")
self.body = body
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
# Moves the agent's `body` by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
_applied_steering = true
body.apply_central_impulse(acceleration.linear)
body.apply_torque_impulse(Vector3.UP * acceleration.angular)
if calculate_velocities:
linear_velocity = body.linear_velocity
angular_velocity = body.angular_velocity.y
func _set_body(value: RigidBody) -> void:
body = value
_last_position = body.global_position
_last_orientation = body.rotation.y
position = _last_position
orientation = _last_orientation
func _on_body_ready() -> void:
body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
_set_body(body)
func _on_SceneTree_frame() -> void:
var current_position: Vector3 = body.global_position
var current_orientation: float = body.rotation.y
position = current_position
orientation = current_orientation
if calculate_velocities:
if _applied_steering:
_applied_steering = false
else:
linear_velocity = body.linear_velocity
angular_velocity = body.angular_velocity.y

View File

@ -0,0 +1,40 @@
# A base class for a specialized steering agent that updates itself every frame
# so the user does not have to. All other specialized agents derive from this.
# tags: abstract
extends GSTSteeringAgent
class_name GSTSpecializedAgent
# If `true`, calculates linear and angular velocities based on the previous
# frame. When `false`, the user must keep those values updated.
var calculate_velocities := true
# If `true` and `calculate_velocities` is true, interpolates
# the current linear velocity towards 0 by the `linear_drag_percentage` value.
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
var apply_linear_drag := true
# If `true` and `calculate_velocities` is true, interpolates
# the current angular velocity towards 0 by the `angular_drag_percentage` value.
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
var apply_angular_drag := true
# The percentage between the current linear velocity and 0 to interpolate by if
# `calculate_velocities` and `apply_linear_drag` are true.
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
var linear_drag_percentage := 0.0
# The percentage between the current angular velocity and 0 to interpolate by if
# `calculate_velocities` and `apply_angular_drag` are true.
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
var angular_drag_percentage := 0.0
var _last_orientation: float
var _body_type: int
var _applied_steering := false
# Moves the agent's body by target `acceleration`.
# tags: virtual
func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void:
pass

View File

@ -19,7 +19,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
self.target = target
func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> GSTTargetAcceleration:
func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> void:
var to_target := target_position - agent.position
var distance := to_target.length()
@ -38,8 +38,6 @@ func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> G
acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
acceleration.angular = 0
return acceleration
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
return _arrive(acceleration, target.position)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
_arrive(acceleration, target.position)

View File

@ -16,7 +16,7 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
pass
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
_shortest_time = INF
_first_neighbor = null
_first_minimum_separation = 0
@ -37,8 +37,6 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
acceleration.linear = acceleration.linear.normalized() * -agent.linear_acceleration_max
acceleration.angular = 0
return acceleration
# Callback for the proximity to call when finding neighbors. Keeps track of every `neighbor`
# that was found but only keeps the one the owning agent will most likely collide with.

View File

@ -33,7 +33,7 @@ func get_behavior_at(index: int) -> Dictionary:
return {}
func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(blended_accel: GSTTargetAcceleration) -> void:
blended_accel.set_zero()
for i in range(_behaviors.size()):
@ -48,5 +48,3 @@ func _calculate_steering(blended_accel: GSTTargetAcceleration) -> GSTTargetAccel
-agent.angular_acceleration_max,
agent.angular_acceleration_max
)
return blended_accel

View File

@ -11,14 +11,13 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
pass
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
acceleration.set_zero()
_center_of_mass = Vector3.ZERO
var neighbor_count = proximity._find_neighbors(_callback)
if neighbor_count > 0:
_center_of_mass *= 1.0 / neighbor_count
acceleration.linear = (_center_of_mass - agent.position).normalized() * agent.linear_acceleration_max
return acceleration
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position

View File

@ -8,17 +8,16 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) ->
pass
func _face(acceleration: GSTTargetAcceleration, target_position: Vector3) -> GSTTargetAcceleration:
func _face(acceleration: GSTTargetAcceleration, target_position: Vector3) -> void:
var to_target := target_position - agent.position
var distance_squared := to_target.length_squared()
if distance_squared < agent.zero_linear_speed_threshold:
acceleration.set_zero()
return acceleration
else:
var orientation = GSTUtils.vector3_to_angle(to_target)
return _match_orientation(acceleration, orientation)
_match_orientation(acceleration, orientation)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
return _face(acceleration, target.position)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
_face(acceleration, target.position)

View File

@ -7,9 +7,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent, target) ->
pass
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
acceleration.linear = (
(agent.position - target.position).normalized() * agent.linear_acceleration_max)
acceleration.angular = 0
return acceleration

View File

@ -25,7 +25,7 @@ func _init(
self.prediction_time = prediction_time
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
var location := (
agent.position if prediction_time == 0
else agent.position + (agent.linear_velocity * prediction_time))
@ -38,13 +38,13 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
if is_arrive_enabled and path.is_open:
if path_offset >= 0:
if target_distance > path.length - deceleration_radius:
return _arrive(acceleration, target_position)
_arrive(acceleration, target_position)
return
else:
if target_distance < deceleration_radius:
return _arrive(acceleration, target_position)
_arrive(acceleration, target_position)
return
acceleration.linear = (target_position - agent.position).normalized()
acceleration.linear *= agent.linear_acceleration_max
acceleration.angular = 0
return acceleration

View File

@ -8,10 +8,9 @@ func _init(agent: GSTSteeringAgent).(agent, null) -> void:
pass
func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(accel: GSTTargetAcceleration) -> void:
if agent.linear_velocity.length_squared() < agent.zero_linear_speed_threshold:
accel.set_zero()
return accel
else:
var orientation := GSTUtils.vector3_to_angle(agent.linear_velocity)
return _match_orientation(accel, orientation)
_match_orientation(accel, orientation)

View File

@ -20,7 +20,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
self.target = target
func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> GSTTargetAcceleration:
func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> void:
var rotation := wrapf(desired_orientation - agent.orientation, -PI, PI)
var rotation_size := abs(rotation)
@ -43,8 +43,6 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation
acceleration.linear = Vector3.ZERO
return acceleration
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
return _match_orientation(acceleration, target.orientation)
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
_match_orientation(acceleration, target.orientation)

View File

@ -31,7 +31,7 @@ func get_behavior_at(index: int) -> GSTSteeringBehavior:
return null
func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(accel: GSTTargetAcceleration) -> void:
var threshold_squared := zero_threshold * zero_threshold
last_selected_index = -1
@ -48,5 +48,3 @@ func _calculate_steering(accel: GSTTargetAcceleration) -> GSTTargetAcceleration:
break
else:
accel.set_zero()
return accel

View File

@ -19,7 +19,7 @@ func _init(
self.predict_time_max = predict_time_max
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
var target_position := target.position
var distance_squared := (target_position - agent.position).length_squared()
@ -37,8 +37,6 @@ func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAccele
acceleration.angular = 0
return acceleration
func _get_modified_acceleration() -> float:
return agent.linear_acceleration_max

View File

@ -12,9 +12,7 @@ func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
self.target = target
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
acceleration.linear = (
(target.position - agent.position).normalized() * agent.linear_acceleration_max)
acceleration.angular = 0
return acceleration

View File

@ -18,11 +18,10 @@ func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity)
pass
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
acceleration.set_zero()
self._acceleration = acceleration
proximity._find_neighbors(_callback)
return acceleration
# Callback for the proximity to call when finding neighbors. Determines the amount of

View File

@ -18,16 +18,13 @@ func _init(agent: GSTSteeringAgent) -> void:
self.agent = agent
# Returns the `acceleration` modified with the behavior's desired amount of
# acceleration.
func calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
# Sets the `acceleration` with the behavior's desired amount of acceleration.
func calculate_steering(acceleration: GSTTargetAcceleration) -> void:
if is_enabled:
return _calculate_steering(acceleration)
_calculate_steering(acceleration)
else:
acceleration.set_zero()
return acceleration
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
acceleration.set_zero()
return acceleration

View File

@ -25,3 +25,13 @@ static func vector3_to_angle(vector: Vector3) -> float:
# rotate around the Y axis.
static func angle_to_vector2(angle: float) -> Vector2:
return Vector2(sin(-angle), cos(angle))
# Returns a vector2 with `vector`'s x and y components.
static func to_vector2(vector: Vector3) -> Vector2:
return Vector2(vector.x, vector.y)
# Returns a vector3 with `vector`'s x and y components and 0 in z.
static func to_vector3(vector: Vector2) -> Vector3:
return Vector3(vector.x, vector.y, 0)