2020-01-29 17:04:04 +01:00
|
|
|
# Calculates acceleration to take an agent to its target's location. The
|
|
|
|
# calculation attempts to arrive with zero remaining velocity.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @category - Individual behaviors
|
2020-02-11 18:33:25 +01:00
|
|
|
class_name GSAIArrive
|
|
|
|
extends GSAISteeringBehavior
|
2019-12-19 20:04:08 +01:00
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# Target agent to arrive to.
|
2023-01-13 13:09:18 +01:00
|
|
|
var target : GSAIAgentLocation
|
2020-01-29 17:04:04 +01:00
|
|
|
# Distance from the target for the agent to be considered successfully
|
|
|
|
# arrived.
|
2023-01-13 13:09:18 +01:00
|
|
|
var arrival_tolerance : float = 0.0
|
2020-01-29 17:04:04 +01:00
|
|
|
# Distance from the target for the agent to begin slowing down.
|
2023-01-13 13:09:18 +01:00
|
|
|
var deceleration_radius : float = 0.0
|
2020-01-29 17:04:04 +01:00
|
|
|
# Represents the time it takes to change acceleration.
|
2023-01-13 13:09:18 +01:00
|
|
|
var time_to_reach : float = 0.1
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
func _init(agent : GSAISteeringAgent, _target : GSAIAgentLocation).(agent) -> void:
|
2020-02-11 20:36:06 +01:00
|
|
|
self.target = _target
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
func _arrive(acceleration : GSAITargetAcceleration, target_position : Vector3) -> void:
|
|
|
|
var to_target : Vector3 = target_position - agent.position
|
|
|
|
var distance : float = to_target.length()
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
if distance <= arrival_tolerance:
|
|
|
|
acceleration.set_zero()
|
|
|
|
else:
|
2023-01-13 13:09:18 +01:00
|
|
|
var desired_speed : float = agent.linear_speed_max
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
if distance <= deceleration_radius:
|
|
|
|
desired_speed *= distance / deceleration_radius
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2023-01-13 13:09:18 +01:00
|
|
|
var desired_velocity : Vector3 = to_target * desired_speed / distance
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2020-02-14 17:35:18 +01:00
|
|
|
desired_velocity = ((desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach)
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
|
2019-12-19 20:04:08 +01:00
|
|
|
acceleration.angular = 0
|
2020-01-28 00:31:10 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
|
2020-02-11 18:33:25 +01:00
|
|
|
func _calculate_steering(acceleration: GSAITargetAcceleration) -> void:
|
2020-02-06 20:46:21 +01:00
|
|
|
_arrive(acceleration, target.position)
|