2020-01-29 17:04:04 +01:00
|
|
|
# Calculates an acceleration to make an agent intercept another based on the
|
|
|
|
# target agent's movement.
|
2020-01-29 05:56:10 +01:00
|
|
|
class_name GSTPursue
|
|
|
|
extends GSTSteeringBehavior
|
2019-12-16 17:22:03 +01:00
|
|
|
|
|
|
|
|
2020-01-29 17:04:04 +01:00
|
|
|
# The target agent that the behavior is trying to intercept.
|
2019-12-19 20:04:08 +01:00
|
|
|
var target: GSTSteeringAgent
|
2020-01-29 17:04:04 +01:00
|
|
|
# The maximum amount of time in the future the behavior predicts the target's
|
|
|
|
# location.
|
2020-01-22 17:55:49 +01:00
|
|
|
var predict_time_max: float
|
2019-12-16 17:22:03 +01:00
|
|
|
|
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
func _init(
|
|
|
|
agent: GSTSteeringAgent,
|
|
|
|
target: GSTSteeringAgent,
|
2020-01-22 17:55:49 +01:00
|
|
|
predict_time_max := 1.0).(agent) -> void:
|
2019-12-16 17:22:03 +01:00
|
|
|
self.target = target
|
2020-01-22 17:55:49 +01:00
|
|
|
self.predict_time_max = predict_time_max
|
2019-12-16 17:22:03 +01:00
|
|
|
|
|
|
|
|
2020-02-06 20:46:21 +01:00
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
|
2020-01-16 09:44:44 +01:00
|
|
|
var target_position := target.position
|
|
|
|
var distance_squared := (target_position - agent.position).length_squared()
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var speed_squared := agent.linear_velocity.length_squared()
|
2020-01-22 17:55:49 +01:00
|
|
|
var predict_time := predict_time_max
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-16 17:22:03 +01:00
|
|
|
if speed_squared > 0:
|
2020-01-16 09:44:44 +01:00
|
|
|
var predict_time_squared := distance_squared / speed_squared
|
2020-01-22 17:55:49 +01:00
|
|
|
if predict_time_squared < predict_time_max * predict_time_max:
|
2019-12-16 17:22:03 +01:00
|
|
|
predict_time = sqrt(predict_time_squared)
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
acceleration.linear = ((
|
|
|
|
target_position + (target.linear_velocity * predict_time))-agent.position).normalized()
|
|
|
|
acceleration.linear *= _get_modified_acceleration()
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-16 17:22:03 +01:00
|
|
|
acceleration.angular = 0
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
func _get_modified_acceleration() -> float:
|
2020-01-22 17:55:49 +01:00
|
|
|
return agent.linear_acceleration_max
|