2019-12-19 20:04:08 +01:00
|
|
|
class_name GSTArrive
|
2020-01-16 23:14:50 +01:00
|
|
|
extends GSTSteeringBehavior
|
2020-01-02 23:42:41 +01:00
|
|
|
# Calculates acceleration to take an agent to its target's location.
|
|
|
|
# The calculation will attempt to arrive with zero remaining velocity.
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
2020-01-27 19:24:05 +01:00
|
|
|
# The target whose location the agent will be steered to arrive at
|
2019-12-19 20:04:08 +01:00
|
|
|
var target: GSTAgentLocation
|
2020-01-27 19:24:05 +01:00
|
|
|
# The distance from the target for the agent to be considered successfully arrived
|
2019-12-19 20:04:08 +01:00
|
|
|
var arrival_tolerance: float
|
2020-01-27 19:24:05 +01:00
|
|
|
# The distance from the target for the agent to begin slowing down
|
2019-12-19 20:04:08 +01:00
|
|
|
var deceleration_radius: float
|
2020-01-27 19:24:05 +01:00
|
|
|
# A constant that represents the time it takes to change acceleration
|
2020-01-16 09:44:44 +01:00
|
|
|
var time_to_reach := 0.1
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
2020-01-27 19:24:05 +01:00
|
|
|
# Initializes the behavior
|
2019-12-19 20:04:08 +01:00
|
|
|
func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
|
|
|
|
self.target = target
|
|
|
|
|
|
|
|
|
|
|
|
func _arrive(acceleration: GSTTargetAcceleration, target_position: Vector3) -> GSTTargetAcceleration:
|
2020-01-16 09:44:44 +01:00
|
|
|
var to_target := target_position - agent.position
|
|
|
|
var distance := to_target.length()
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
if distance <= arrival_tolerance:
|
|
|
|
acceleration.set_zero()
|
|
|
|
else:
|
2020-01-22 17:55:49 +01:00
|
|
|
var desired_speed := agent.linear_speed_max
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
if distance <= deceleration_radius:
|
|
|
|
desired_speed *= distance / deceleration_radius
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var desired_velocity := to_target * desired_speed/distance
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
desired_velocity = (desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
acceleration.linear = GSTUtils.clampedv3(desired_velocity, agent.linear_acceleration_max)
|
2019-12-19 20:04:08 +01:00
|
|
|
acceleration.angular = 0
|
|
|
|
|
|
|
|
return acceleration
|
|
|
|
|
|
|
|
|
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
|
|
|
return _arrive(acceleration, target.position)
|