mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
Format the code using gdformat
gdformat follows the official style guide, and handles line length and wrapping lines for us.
This commit is contained in:
parent
2940442b95
commit
68b85bb234
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max
|
||||
export (float, 0, 200, 2.0) var linear_acceleration_max := 80.0 setget set_linear_acceleration_max
|
||||
export (float, 0, 100, 0.1) var arrival_tolerance := 25.0 setget set_arrival_tolerance
|
||||
@ -11,12 +10,7 @@ onready var target_drawer := $TargetDrawer
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
arriver.setup(
|
||||
linear_speed_max,
|
||||
linear_acceleration_max,
|
||||
arrival_tolerance,
|
||||
deceleration_radius
|
||||
)
|
||||
arriver.setup(linear_speed_max, linear_acceleration_max, arrival_tolerance, deceleration_radius)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var agent := GSAIKinematicBody2DAgent.new(self)
|
||||
var target := GSAIAgentLocation.new()
|
||||
var arrive := GSAIArrive.new(agent, target)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
const COLORS := {
|
||||
deceleration_radius = Color(1.0, 0.419, 0.592, 0.5),
|
||||
arrival_tolerance = Color(0.278, 0.231, 0.47, 0.3)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Camera
|
||||
|
||||
|
||||
var target: Spatial
|
||||
|
||||
onready var ray := $RayCast
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (float, 0, 50, 0.1) var linear_speed_max := 10.0 setget set_linear_speed_max
|
||||
export (float, 0, 50, 0.1) var linear_acceleration_max := 1.0 setget set_linear_acceleration_max
|
||||
export (float, 0, 50, 0.1) var arrival_tolerance := 0.5 setget set_arrival_tolerance
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody
|
||||
|
||||
|
||||
var target_node: Spatial
|
||||
|
||||
onready var agent := GSAIKinematicBody3DAgent.new(self)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (float, 0, 2000, 40) var linear_speed_max := 350.0 setget set_linear_speed_max
|
||||
export (float, 0, 100, 2) var linear_acceleration_max := 40.0 setget set_linear_accel_max
|
||||
export (float, 0, 500, 10) var proximity_radius := 140.0 setget set_proximity_radius
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var draw_proximity: bool
|
||||
|
||||
var _boundary_right: float
|
||||
@ -83,7 +82,10 @@ func set_random_nonoverlapping_position(others: Array, distance_from_boundary_mi
|
||||
var done := true
|
||||
for i in range(others.size()):
|
||||
var other: Node2D = others[i]
|
||||
if other.global_position.distance_to(position) <= _radius*2 + distance_from_boundary_min:
|
||||
if (
|
||||
other.global_position.distance_to(position)
|
||||
<= _radius * 2 + distance_from_boundary_min
|
||||
):
|
||||
done = false
|
||||
if done:
|
||||
break
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
export var avoider_template: PackedScene
|
||||
export var inner_color := Color()
|
||||
export var outer_color := Color()
|
||||
@ -11,8 +10,8 @@ var boundaries: Vector2
|
||||
|
||||
func _ready() -> void:
|
||||
boundaries = Vector2(
|
||||
ProjectSettings["display/window/size/width"],
|
||||
ProjectSettings["display/window/size/height"])
|
||||
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
|
||||
)
|
||||
var rng := RandomNumberGenerator.new()
|
||||
var avoiders := []
|
||||
var avoider_agents := []
|
||||
|
@ -1,7 +1,6 @@
|
||||
class_name DemoPickerUI
|
||||
extends Control
|
||||
|
||||
|
||||
# warning-ignore:unused_signal
|
||||
signal demo_requested
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
onready var demo_picker: DemoPickerUI = $DemoPickerUI
|
||||
onready var demo_player := $DemoPlayer
|
||||
onready var button_go_back: Button = $ButtonGoBack
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (int, 0, 359, 2) var angular_speed_max := 120 setget set_angular_speed_max
|
||||
export (int, 0, 359, 2) var angular_accel_max := 10 setget set_angular_accel_max
|
||||
export (int, 0, 180, 2) var align_tolerance := 5 setget set_align_tolerance
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var speed: float
|
||||
|
||||
onready var agent := GSAIAgentLocation.new()
|
||||
@ -15,4 +14,5 @@ func _physics_process(_delta: float) -> void:
|
||||
func _get_movement() -> Vector2:
|
||||
return Vector2(
|
||||
Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
|
||||
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up"))
|
||||
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")
|
||||
)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var face: GSAIFace
|
||||
var agent := GSAIKinematicBody2DAgent.new(self)
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
signal path_established(points)
|
||||
|
||||
|
||||
var active_points := []
|
||||
var is_drawing := false
|
||||
var distance_threshold := 100.0
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (float, 0, 2000, 40) var linear_speed_max := 600.0 setget set_linear_speed_max
|
||||
export (float, 0, 200, 10.0) var linear_acceleration_max := 40.0 setget set_linear_acceleration_max
|
||||
export (float, 0, 100, 0.1) var arrival_tolerance := 10.0 setget set_arrival_tolerance
|
||||
|
@ -1,17 +1,18 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var _velocity := Vector2.ZERO
|
||||
var _accel := GSAITargetAcceleration.new()
|
||||
var _valid := false
|
||||
var _drag := 0.1
|
||||
|
||||
|
||||
onready var agent := GSAIKinematicBody2DAgent.new(self)
|
||||
onready var path := GSAIPath.new([
|
||||
onready var path := GSAIPath.new(
|
||||
[
|
||||
Vector3(global_position.x, global_position.y, 0),
|
||||
Vector3(global_position.x, global_position.y, 0)
|
||||
], true)
|
||||
],
|
||||
true
|
||||
)
|
||||
onready var follow := GSAIFollowPath.new(agent, path, 0, 0)
|
||||
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
onready var spawner := $Spawner
|
||||
|
||||
export (float, 0, 2000, 40.0) var linear_speed_max := 600.0 setget set_linear_speed_max
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var separation: GSAISeparation
|
||||
var cohesion: GSAICohesion
|
||||
var proximity: GSAIRadiusProximity
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
export var member: PackedScene
|
||||
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends ItemList
|
||||
|
||||
|
||||
signal demo_selected(scene_path)
|
||||
|
||||
var file_paths := PoolStringArray()
|
||||
@ -34,7 +33,9 @@ func sentencify(line: String) -> String:
|
||||
return line
|
||||
|
||||
|
||||
func _find_files(dirpath := "", patterns := PoolStringArray(), is_recursive := false, do_skip_hidden := true) -> PoolStringArray:
|
||||
func _find_files(
|
||||
dirpath := "", patterns := PoolStringArray(), is_recursive := false, do_skip_hidden := true
|
||||
) -> PoolStringArray:
|
||||
var paths := PoolStringArray()
|
||||
var directory := Directory.new()
|
||||
|
||||
|
@ -1,14 +1,12 @@
|
||||
extends Node2D
|
||||
# Wraps the ships' positions around the world border.
|
||||
|
||||
|
||||
var _world_bounds: Vector2
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_world_bounds = Vector2(
|
||||
ProjectSettings["display/window/size/width"],
|
||||
ProjectSettings["display/window/size/height"]
|
||||
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
|
||||
)
|
||||
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
extends KinematicBody2D
|
||||
# Controls the player ship's movements based on player input.
|
||||
|
||||
|
||||
export var thruster_strength := 175.0
|
||||
export var side_thruster_strength := 10.0
|
||||
export var velocity_max := 300.0
|
||||
@ -82,8 +81,10 @@ func _calculate_linear_velocity(
|
||||
|
||||
|
||||
func _get_movement() -> Vector2:
|
||||
return Vector2( Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
|
||||
Input.get_action_strength("sf_up") - Input.get_action_strength("sf_down"))
|
||||
return Vector2(
|
||||
Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
|
||||
Input.get_action_strength("sf_up") - Input.get_action_strength("sf_down")
|
||||
)
|
||||
|
||||
|
||||
func _update_agent() -> void:
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node
|
||||
|
||||
|
||||
export (float, 0, 2000, 40) var linear_speed_max := 120.0 setget set_linear_speed_max
|
||||
export (float, 0, 200, 2) var linear_accel_max := 10.0 setget set_linear_accel_max
|
||||
export (float, 0, 5, 0.1) var predict_time := 1.0 setget set_predict_time
|
||||
|
@ -1,7 +1,6 @@
|
||||
extends KinematicBody2D
|
||||
# Represents a ship that chases after the player.
|
||||
|
||||
|
||||
export var use_seek: bool = false
|
||||
|
||||
var _blend: GSAIBlend
|
||||
@ -26,23 +25,18 @@ func _physics_process(delta: float) -> void:
|
||||
_blend.calculate_steering(accel)
|
||||
|
||||
agent.angular_velocity = clamp(
|
||||
agent.angular_velocity + accel.angular,
|
||||
-agent.angular_speed_max,
|
||||
agent.angular_speed_max
|
||||
agent.angular_velocity + accel.angular, -agent.angular_speed_max, agent.angular_speed_max
|
||||
)
|
||||
agent.angular_velocity = lerp(agent.angular_velocity, 0, _angular_drag)
|
||||
|
||||
rotation += agent.angular_velocity * delta
|
||||
|
||||
var linear_velocity := (
|
||||
GSAIUtils.to_vector2(agent.linear_velocity) +
|
||||
(GSAIUtils.angle_to_vector2(rotation) * -agent.linear_acceleration_max)
|
||||
GSAIUtils.to_vector2(agent.linear_velocity)
|
||||
+ (GSAIUtils.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 = linear_velocity.linear_interpolate(Vector2.ZERO, _linear_drag_coefficient)
|
||||
|
||||
linear_velocity = move_and_slide(linear_velocity)
|
||||
agent.linear_velocity = GSAIUtils.to_vector3(linear_velocity)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
# Maximum possible linear velocity
|
||||
export var speed_max := 450.0
|
||||
# Maximum change in linear velocity
|
||||
@ -119,9 +118,9 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
# We add the discovered acceleration to our linear velocity. The toolkit does not limit
|
||||
# velocity, just acceleration, so we clamp the result ourselves here.
|
||||
velocity = (velocity + Vector2(
|
||||
acceleration.linear.x, acceleration.linear.y)
|
||||
).clamped(agent.linear_speed_max)
|
||||
velocity = (velocity + Vector2(acceleration.linear.x, acceleration.linear.y)).clamped(
|
||||
agent.linear_speed_max
|
||||
)
|
||||
|
||||
# This applies drag on the agent's motion, helping it to slow down naturally.
|
||||
velocity = velocity.linear_interpolate(Vector2.ZERO, linear_drag)
|
||||
@ -132,9 +131,7 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
# We then do something similar to apply our agent's rotational speed.
|
||||
angular_velocity = clamp(
|
||||
angular_velocity + acceleration.angular,
|
||||
-agent.angular_speed_max,
|
||||
agent.angular_speed_max
|
||||
angular_velocity + acceleration.angular, -agent.angular_speed_max, agent.angular_speed_max
|
||||
)
|
||||
# This applies drag on the agent's rotation, helping it slow down naturally.
|
||||
angular_velocity = lerp(angular_velocity, 0, angular_drag)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
export var speed := 1500.0
|
||||
|
||||
var velocity := Vector2.ZERO
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
export var speed_max := 650.0
|
||||
export var acceleration_max := 70.0
|
||||
export var rotation_speed_max := 240
|
||||
@ -61,7 +60,10 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
elif event is InputEventMouseButton:
|
||||
if event.button_index == BUTTON_LEFT and event.pressed:
|
||||
var next_bullet := bullet.instance()
|
||||
next_bullet.global_position = global_position - direction * (agent.bounding_radius-5)
|
||||
next_bullet.global_position = (
|
||||
global_position
|
||||
- direction * (agent.bounding_radius - 5)
|
||||
)
|
||||
next_bullet.player = self
|
||||
next_bullet.start(-direction)
|
||||
bullets.add_child(next_bullet)
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
const COLOR := Color("8fde5d")
|
||||
|
||||
|
||||
@ -9,7 +8,6 @@ func _ready() -> void:
|
||||
_on_SceneTree_size_changed()
|
||||
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
for b in get_children():
|
||||
var extents: Vector2 = b.get_node("CollisionShape2D").shape.extents
|
||||
@ -18,8 +16,7 @@ func _draw() -> void:
|
||||
|
||||
func _on_SceneTree_size_changed() -> void:
|
||||
var size := Vector2(
|
||||
ProjectSettings["display/window/size/width"],
|
||||
ProjectSettings["display/window/size/height"]
|
||||
ProjectSettings["display/window/size/width"], ProjectSettings["display/window/size/height"]
|
||||
)
|
||||
for b in get_children():
|
||||
var boundary: String = b.name.rsplit("Boundary")[0]
|
||||
|
@ -1,7 +1,6 @@
|
||||
extends KinematicBody2D
|
||||
# Class to control the player in basic left/right up/down movement.
|
||||
|
||||
|
||||
var speed: float
|
||||
onready var agent := GSAIAgentLocation.new()
|
||||
|
||||
@ -23,4 +22,5 @@ func _physics_process(_delta: float) -> void:
|
||||
func _get_movement() -> Vector2:
|
||||
return Vector2(
|
||||
Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"),
|
||||
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up"))
|
||||
Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")
|
||||
)
|
||||
|
@ -1,7 +1,6 @@
|
||||
extends Node
|
||||
# Access helper class for children to access window boundaries.
|
||||
|
||||
|
||||
enum Mode { FLEE, SEEK }
|
||||
|
||||
export (Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
|
||||
|
@ -1,6 +1,5 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
var player_agent: GSAIAgentLocation
|
||||
var velocity := Vector2.ZERO
|
||||
var start_speed: float
|
||||
|
@ -1,7 +1,6 @@
|
||||
extends Node2D
|
||||
# Holds data to instantiate and configure a number of agent entities.
|
||||
|
||||
|
||||
export (PackedScene) var Entity: PackedScene
|
||||
export var entity_count := 10
|
||||
export var entity_color := Color.blue
|
||||
|
@ -1,7 +1,6 @@
|
||||
tool
|
||||
extends PanelContainer
|
||||
|
||||
|
||||
export (String, MULTILINE) var text_bbcode := "" setget set_text_bbcode
|
||||
|
||||
onready var rich_text_label: RichTextLabel = $MarginContainer/RichTextLabel
|
||||
|
@ -1,7 +1,6 @@
|
||||
tool
|
||||
extends Line2D
|
||||
|
||||
|
||||
export (Color) var inner_color := Color() setget set_inner_color
|
||||
|
||||
|
||||
|
@ -3,13 +3,11 @@
|
||||
extends GSAISpecializedAgent
|
||||
class_name GSAIKinematicBody2DAgent
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
@ -57,10 +55,7 @@ func _apply_sliding_steering(accel: Vector3) -> void:
|
||||
func _apply_collide_steering(accel: Vector3, delta: float) -> void:
|
||||
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
|
||||
if apply_linear_drag:
|
||||
velocity = velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
)
|
||||
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||
# warning-ignore:return_value_discarded
|
||||
body.move_and_collide(GSAIUtils.to_vector2(velocity) * delta)
|
||||
if calculate_velocities:
|
||||
@ -70,10 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
|
||||
func _apply_position_steering(accel: Vector3, delta: float) -> void:
|
||||
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
|
||||
if apply_linear_drag:
|
||||
velocity = velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
)
|
||||
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||
body.global_position += GSAIUtils.to_vector2(velocity) * delta
|
||||
if calculate_velocities:
|
||||
linear_velocity = velocity
|
||||
@ -110,27 +102,19 @@ func _on_SceneTree_physics_frame() -> void:
|
||||
_applied_steering = false
|
||||
else:
|
||||
linear_velocity = GSAIUtils.clampedv3(
|
||||
GSAIUtils.to_vector3(_last_position - current_position),
|
||||
linear_speed_max
|
||||
GSAIUtils.to_vector3(_last_position - current_position), linear_speed_max
|
||||
)
|
||||
if apply_linear_drag:
|
||||
linear_velocity = linear_velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
Vector3.ZERO, linear_drag_percentage
|
||||
)
|
||||
|
||||
angular_velocity = clamp(
|
||||
_last_orientation - current_orientation,
|
||||
-angular_speed_max,
|
||||
angular_speed_max
|
||||
_last_orientation - current_orientation, -angular_speed_max, angular_speed_max
|
||||
)
|
||||
|
||||
if apply_angular_drag:
|
||||
angular_velocity = lerp(
|
||||
angular_velocity,
|
||||
0,
|
||||
angular_drag_percentage
|
||||
)
|
||||
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
||||
|
||||
_last_position = current_position
|
||||
_last_orientation = current_orientation
|
||||
|
@ -8,7 +8,6 @@ class_name GSAIKinematicBody3DAgent
|
||||
# POSITION changes the global_position directly
|
||||
enum MovementType { SLIDE, COLLIDE, POSITION }
|
||||
|
||||
|
||||
# The KinematicBody to keep track of
|
||||
var body: KinematicBody setget _set_body
|
||||
|
||||
@ -56,10 +55,7 @@ func _apply_sliding_steering(accel: Vector3) -> void:
|
||||
func _apply_collide_steering(accel: Vector3, delta: float) -> void:
|
||||
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
|
||||
if apply_linear_drag:
|
||||
velocity = velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
)
|
||||
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||
# warning-ignore:return_value_discarded
|
||||
body.move_and_collide(velocity * delta)
|
||||
if calculate_velocities:
|
||||
@ -69,10 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
|
||||
func _apply_position_steering(accel: Vector3, delta: float) -> void:
|
||||
var velocity := GSAIUtils.clampedv3(linear_velocity + accel, linear_speed_max)
|
||||
if apply_linear_drag:
|
||||
velocity = velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
)
|
||||
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||
body.global_position += velocity * delta
|
||||
if calculate_velocities:
|
||||
linear_velocity = velocity
|
||||
@ -109,27 +102,19 @@ func _on_SceneTree_physics_frame() -> void:
|
||||
_applied_steering = false
|
||||
else:
|
||||
linear_velocity = GSAIUtils.clampedv3(
|
||||
_last_position - current_position,
|
||||
linear_speed_max
|
||||
_last_position - current_position, linear_speed_max
|
||||
)
|
||||
if apply_linear_drag:
|
||||
linear_velocity = linear_velocity.linear_interpolate(
|
||||
Vector3.ZERO,
|
||||
linear_drag_percentage
|
||||
Vector3.ZERO, linear_drag_percentage
|
||||
)
|
||||
|
||||
angular_velocity = clamp(
|
||||
_last_orientation - current_orientation,
|
||||
-angular_speed_max,
|
||||
angular_speed_max
|
||||
_last_orientation - current_orientation, -angular_speed_max, angular_speed_max
|
||||
)
|
||||
|
||||
if apply_angular_drag:
|
||||
angular_velocity = lerp(
|
||||
angular_velocity,
|
||||
0,
|
||||
angular_drag_percentage
|
||||
)
|
||||
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
||||
|
||||
_last_position = current_position
|
||||
_last_orientation = current_orientation
|
||||
|
@ -3,7 +3,6 @@
|
||||
extends GSAISpecializedAgent
|
||||
class_name GSAIRigidBody2DAgent
|
||||
|
||||
|
||||
# The RigidBody2D to keep track of
|
||||
var body: RigidBody2D setget _set_body
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
extends GSAISpecializedAgent
|
||||
class_name GSAIRigidBody3DAgent
|
||||
|
||||
|
||||
# The RigidBody to keep track of
|
||||
var body: RigidBody setget _set_body
|
||||
|
||||
@ -19,7 +18,6 @@ func _init(_body: RigidBody) -> void:
|
||||
self.body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame")
|
||||
|
||||
|
||||
|
||||
# Moves the agent's `body` by target `acceleration`.
|
||||
# tags: virtual
|
||||
func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> void:
|
||||
|
@ -4,7 +4,6 @@
|
||||
extends GSAISteeringAgent
|
||||
class_name GSAISpecializedAgent
|
||||
|
||||
|
||||
# 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
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAIArrive
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
# Target agent to arrive to.
|
||||
var target: GSAIAgentLocation
|
||||
# Distance from the target for the agent to be considered successfully
|
||||
@ -33,7 +32,7 @@ func _arrive(acceleration: GSAITargetAcceleration, target_position: Vector3) ->
|
||||
|
||||
var desired_velocity := to_target * desired_speed / distance
|
||||
|
||||
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 = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
|
||||
acceleration.angular = 0
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAIAvoidCollisions
|
||||
extends GSAIGroupBehavior
|
||||
|
||||
|
||||
var _first_neighbor: GSAISteeringAgent
|
||||
var _shortest_time: float
|
||||
var _first_minimum_separation: float
|
||||
@ -28,13 +27,17 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
acceleration.set_zero()
|
||||
else:
|
||||
if (
|
||||
_first_minimum_separation <= 0 or
|
||||
_first_distance < agent.bounding_radius + _first_neighbor.bounding_radius):
|
||||
_first_minimum_separation <= 0
|
||||
or _first_distance < agent.bounding_radius + _first_neighbor.bounding_radius
|
||||
):
|
||||
acceleration.linear = _first_neighbor.position - agent.position
|
||||
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.linear_acceleration_max
|
||||
acceleration.linear = (acceleration.linear.normalized() * -agent.linear_acceleration_max)
|
||||
acceleration.angular = 0
|
||||
|
||||
|
||||
@ -55,7 +58,10 @@ func _report_neighbor(neighbor: GSAISteeringAgent) -> bool:
|
||||
return false
|
||||
else:
|
||||
var distance = relative_position.length()
|
||||
var minimum_separation: float = distance - sqrt(relative_speed_squared) * time_to_collision
|
||||
var minimum_separation: float = (
|
||||
distance
|
||||
- sqrt(relative_speed_squared) * time_to_collision
|
||||
)
|
||||
if minimum_separation > agent.bounding_radius + neighbor.bounding_radius:
|
||||
return false
|
||||
else:
|
||||
|
@ -9,7 +9,6 @@
|
||||
class_name GSAIBlend
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
var _behaviors := []
|
||||
var _accel := GSAITargetAcceleration.new()
|
||||
|
||||
@ -44,7 +43,5 @@ func _calculate_steering(blended_accel: GSAITargetAcceleration) -> void:
|
||||
|
||||
blended_accel.linear = GSAIUtils.clampedv3(blended_accel.linear, agent.linear_acceleration_max)
|
||||
blended_accel.angular = clamp(
|
||||
blended_accel.angular,
|
||||
-agent.angular_acceleration_max,
|
||||
agent.angular_acceleration_max
|
||||
blended_accel.angular, -agent.angular_acceleration_max, agent.angular_acceleration_max
|
||||
)
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAICohesion
|
||||
extends GSAIGroupBehavior
|
||||
|
||||
|
||||
var _center_of_mass: Vector3
|
||||
|
||||
|
||||
@ -17,7 +16,10 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
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
|
||||
acceleration.linear = (
|
||||
(_center_of_mass - agent.position).normalized()
|
||||
* agent.linear_acceleration_max
|
||||
)
|
||||
|
||||
|
||||
# Callback for the proximity to call when finding neighbors. Adds `neighbor`'s position
|
||||
|
@ -4,10 +4,9 @@ class_name GSAIEvade
|
||||
extends GSAIPursue
|
||||
|
||||
|
||||
func _init(
|
||||
agent: GSAISteeringAgent,
|
||||
target: GSAISteeringAgent,
|
||||
predict_time_max := 1.0).(agent, target, predict_time_max):
|
||||
func _init(agent: GSAISteeringAgent, target: GSAISteeringAgent, predict_time_max := 1.0).(
|
||||
agent, target, predict_time_max
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
@ -4,7 +4,9 @@ class_name GSAIFace
|
||||
extends GSAIMatchOrientation
|
||||
|
||||
|
||||
func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation, use_z := false).(agent, target, use_z) -> void:
|
||||
func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation, use_z := false).(
|
||||
agent, target, use_z
|
||||
) -> void:
|
||||
pass
|
||||
|
||||
|
||||
@ -15,7 +17,8 @@ func _face(acceleration: GSAITargetAcceleration, target_position: Vector3) -> vo
|
||||
if distance_squared < agent.zero_linear_speed_threshold:
|
||||
acceleration.set_zero()
|
||||
else:
|
||||
var orientation = (GSAIUtils.vector3_to_angle(to_target)
|
||||
var orientation = (
|
||||
GSAIUtils.vector3_to_angle(to_target)
|
||||
if use_z
|
||||
else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target))
|
||||
)
|
||||
|
@ -9,5 +9,7 @@ func _init(agent: GSAISteeringAgent, target: GSAIAgentLocation).(agent, target)
|
||||
|
||||
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
acceleration.linear = (
|
||||
(agent.position - target.position).normalized() * agent.linear_acceleration_max)
|
||||
(agent.position - target.position).normalized()
|
||||
* agent.linear_acceleration_max
|
||||
)
|
||||
acceleration.angular = 0
|
||||
|
@ -2,7 +2,6 @@
|
||||
class_name GSAIFollowPath
|
||||
extends GSAIArrive
|
||||
|
||||
|
||||
# The path to follow and travel along.
|
||||
var path: GSAIPath
|
||||
# The distance along the path to generate the next target position.
|
||||
@ -15,11 +14,9 @@ var is_arrive_enabled := true
|
||||
var prediction_time := 0.0
|
||||
|
||||
|
||||
func _init(
|
||||
agent: GSAISteeringAgent,
|
||||
_path: GSAIPath,
|
||||
_path_offset := 0.0,
|
||||
_prediction_time := 0.0).(agent, null) -> void:
|
||||
func _init(agent: GSAISteeringAgent, _path: GSAIPath, _path_offset := 0.0, _prediction_time := 0.0).(
|
||||
agent, null
|
||||
) -> void:
|
||||
self.path = _path
|
||||
self.path_offset = _path_offset
|
||||
self.prediction_time = _prediction_time
|
||||
@ -27,8 +24,10 @@ func _init(
|
||||
|
||||
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
var location := (
|
||||
agent.position if prediction_time == 0
|
||||
else agent.position + (agent.linear_velocity * prediction_time))
|
||||
agent.position
|
||||
if prediction_time == 0
|
||||
else agent.position + (agent.linear_velocity * prediction_time)
|
||||
)
|
||||
|
||||
var distance := path.calculate_distance(location)
|
||||
var target_distance := distance + path_offset
|
||||
|
@ -12,7 +12,8 @@ func _calculate_steering(accel: GSAITargetAcceleration) -> void:
|
||||
if agent.linear_velocity.length_squared() < agent.zero_linear_speed_threshold:
|
||||
accel.set_zero()
|
||||
else:
|
||||
var orientation := (GSAIUtils.vector3_to_angle(agent.linear_velocity)
|
||||
var orientation := (
|
||||
GSAIUtils.vector3_to_angle(agent.linear_velocity)
|
||||
if use_z
|
||||
else GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(agent.linear_velocity))
|
||||
)
|
||||
|
@ -4,7 +4,6 @@
|
||||
class_name GSAIMatchOrientation
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
# The target orientation for the behavior to try and match rotations to.
|
||||
var target: GSAIAgentLocation
|
||||
# The amount of distance in radians for the behavior to consider itself close
|
||||
@ -39,11 +38,11 @@ func _match_orientation(acceleration: GSAITargetAcceleration, desired_orientatio
|
||||
|
||||
desired_rotation *= rotation / rotation_size
|
||||
|
||||
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)
|
||||
if limited_acceleration > agent.angular_acceleration_max:
|
||||
acceleration.angular *= agent.angular_acceleration_max / limited_acceleration
|
||||
acceleration.angular *= (agent.angular_acceleration_max / limited_acceleration)
|
||||
|
||||
acceleration.linear = Vector3.ZERO
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAIPriority
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
var _behaviors := []
|
||||
|
||||
# The index of the last behavior the container prioritized.
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAIPursue
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
# The target agent that the behavior is trying to intercept.
|
||||
var target: GSAISteeringAgent
|
||||
# The maximum amount of time in the future the behavior predicts the target's
|
||||
@ -11,10 +10,7 @@ var target: GSAISteeringAgent
|
||||
var predict_time_max: float
|
||||
|
||||
|
||||
func _init(
|
||||
agent: GSAISteeringAgent,
|
||||
_target: GSAISteeringAgent,
|
||||
_predict_time_max := 1.0).(agent) -> void:
|
||||
func _init(agent: GSAISteeringAgent, _target: GSAISteeringAgent, _predict_time_max := 1.0).(agent) -> void:
|
||||
self.target = _target
|
||||
self.predict_time_max = _predict_time_max
|
||||
|
||||
@ -31,8 +27,7 @@ func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
if predict_time_squared < predict_time_max * predict_time_max:
|
||||
predict_time = sqrt(predict_time_squared)
|
||||
|
||||
acceleration.linear = ((
|
||||
target_position + (target.linear_velocity * predict_time))-agent.position).normalized()
|
||||
acceleration.linear = ((target_position + (target.linear_velocity * predict_time)) - agent.position).normalized()
|
||||
acceleration.linear *= _get_modified_acceleration()
|
||||
|
||||
acceleration.angular = 0
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAISeek
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
# The target that the behavior aims to move the agent to.
|
||||
var target: GSAIAgentLocation
|
||||
|
||||
@ -14,5 +13,7 @@ func _init(agent: GSAISteeringAgent, _target: GSAIAgentLocation).(agent) -> void
|
||||
|
||||
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
||||
acceleration.linear = (
|
||||
(target.position - agent.position).normalized() * agent.linear_acceleration_max)
|
||||
(target.position - agent.position).normalized()
|
||||
* agent.linear_acceleration_max
|
||||
)
|
||||
acceleration.angular = 0
|
||||
|
@ -7,7 +7,6 @@
|
||||
class_name GSAISeparation
|
||||
extends GSAIGroupBehavior
|
||||
|
||||
|
||||
# The coefficient to calculate how fast the separation strength decays with distance.
|
||||
var decay_coefficient := 1.0
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Represents an agent with only a location and an orientation.
|
||||
class_name GSAIAgentLocation
|
||||
|
||||
|
||||
# The agent's position in space.
|
||||
var position := Vector3.ZERO
|
||||
# The agent's orientation on its Y axis rotation.
|
||||
|
@ -2,7 +2,6 @@
|
||||
class_name GSAIGroupBehavior
|
||||
extends GSAISteeringBehavior
|
||||
|
||||
|
||||
# Container to find neighbors of the agent and calculate group behavior.
|
||||
var proximity: GSAIProximity
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
class_name GSAIPath
|
||||
extends Reference
|
||||
|
||||
|
||||
# If `false`, the path loops.
|
||||
var is_open: bool
|
||||
# Total length of the path.
|
||||
@ -56,9 +55,7 @@ func calculate_distance(agent_current_position: Vector3) -> float:
|
||||
for i in range(_segments.size()):
|
||||
var segment: GSAISegment = _segments[i]
|
||||
var distance_squared := _calculate_point_segment_distance_squared(
|
||||
segment.begin,
|
||||
segment.end,
|
||||
agent_current_position
|
||||
segment.begin, segment.end, agent_current_position
|
||||
)
|
||||
|
||||
if distance_squared < smallest_distance_squared:
|
||||
@ -67,8 +64,9 @@ func calculate_distance(agent_current_position: Vector3) -> float:
|
||||
nearest_segment = segment
|
||||
|
||||
var length_on_path := (
|
||||
nearest_segment.cumulative_length -
|
||||
_nearest_point_on_path.distance_to(nearest_segment.end))
|
||||
nearest_segment.cumulative_length
|
||||
- _nearest_point_on_path.distance_to(nearest_segment.end)
|
||||
)
|
||||
|
||||
return length_on_path
|
||||
|
||||
@ -96,8 +94,9 @@ func calculate_target_position(target_distance: float) -> Vector3:
|
||||
var distance := desired_segment.cumulative_length - target_distance
|
||||
|
||||
return (
|
||||
(desired_segment.begin - desired_segment.end) *
|
||||
(distance / desired_segment.length) + desired_segment.end)
|
||||
((desired_segment.begin - desired_segment.end) * (distance / desired_segment.length))
|
||||
+ desired_segment.end
|
||||
)
|
||||
|
||||
|
||||
# Returns the position of the first point on the path.
|
||||
@ -127,7 +126,6 @@ class GSAISegment:
|
||||
var length: float
|
||||
var cumulative_length: float
|
||||
|
||||
|
||||
func _init(_begin: Vector3, _end: Vector3) -> void:
|
||||
self.begin = _begin
|
||||
self.end = _end
|
||||
|
@ -5,7 +5,6 @@
|
||||
extends GSAIAgentLocation
|
||||
class_name GSAISteeringAgent
|
||||
|
||||
|
||||
# The amount of velocity to be considered effectively not moving.
|
||||
var zero_linear_speed_threshold := 0.01
|
||||
# The maximum speed at which the agent can move.
|
||||
|
@ -7,7 +7,6 @@
|
||||
# Individual steering behaviors encapsulate the steering logic.
|
||||
class_name GSAISteeringBehavior
|
||||
|
||||
|
||||
# If `false`, all calculations return zero amounts of acceleration.
|
||||
var is_enabled := true
|
||||
# The AI agent on which the steering behavior bases its calculations.
|
||||
|
@ -2,7 +2,6 @@
|
||||
# system.
|
||||
class_name GSAITargetAcceleration
|
||||
|
||||
|
||||
# Linear acceleration
|
||||
var linear := Vector3.ZERO
|
||||
# Angular acceleration
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Math and vector utility functions.
|
||||
class_name GSAIUtils
|
||||
|
||||
|
||||
# Returns the `vector` with its length capped to `limit`.
|
||||
static func clampedv3(vector: Vector3, limit: float) -> Vector3:
|
||||
var length_squared := vector.length_squared()
|
||||
@ -10,7 +9,6 @@ static func clampedv3(vector: Vector3, limit: float) -> Vector3:
|
||||
vector *= sqrt(limit_squared / length_squared)
|
||||
return vector
|
||||
|
||||
|
||||
# Returns an angle in radians between the positive X axis and the `vector`.
|
||||
#
|
||||
# This assumes orientation for 3D agents that are upright and rotate
|
||||
@ -18,12 +16,10 @@ static func clampedv3(vector: Vector3, limit: float) -> Vector3:
|
||||
static func vector3_to_angle(vector: Vector3) -> float:
|
||||
return atan2(vector.x, vector.z)
|
||||
|
||||
|
||||
# Returns an angle in radians between the positive X axis and the `vector`.
|
||||
static func vector2_to_angle(vector: Vector2) -> float:
|
||||
return atan2(vector.x, -vector.y)
|
||||
|
||||
|
||||
# Returns a directional vector from the given orientation angle.
|
||||
#
|
||||
# This assumes orientation for 2D agents or 3D agents that are upright and
|
||||
@ -31,12 +27,10 @@ static func vector2_to_angle(vector: Vector2) -> float:
|
||||
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)
|
||||
|
@ -2,7 +2,6 @@
|
||||
extends Reference
|
||||
class_name GSAIProximity
|
||||
|
||||
|
||||
# The owning agent whose neighbors are found in the group
|
||||
var agent: GSAISteeringAgent
|
||||
# The agents who are part of this group and could be potential neighbors
|
||||
|
@ -3,7 +3,6 @@
|
||||
extends GSAIProximity
|
||||
class_name GSAIRadiusProximity
|
||||
|
||||
|
||||
# The radius around the owning agent to find neighbors in
|
||||
var radius := 0.0
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user