2019-12-19 20:04:08 +01:00
|
|
|
class_name GSTPursue
|
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 intersect with where a target agent will be.
|
2019-12-17 05:12:18 +01:00
|
|
|
|
2020-01-02 23:42:41 +01:00
|
|
|
# # The `max_predict_time` variable represents how far ahead to calculate the intersection point.
|
2019-12-16 17:22:03 +01:00
|
|
|
|
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
var target: GSTSteeringAgent
|
2019-12-16 17:22:03 +01:00
|
|
|
var max_predict_time: float
|
|
|
|
|
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
func _init(
|
|
|
|
agent: GSTSteeringAgent,
|
|
|
|
target: GSTSteeringAgent,
|
2020-01-16 09:44:44 +01:00
|
|
|
max_predict_time := 1.0).(agent) -> void:
|
2019-12-16 17:22:03 +01:00
|
|
|
self.target = target
|
|
|
|
self.max_predict_time = max_predict_time
|
|
|
|
|
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
2020-01-16 09:44:44 +01:00
|
|
|
var target_position := target.position
|
|
|
|
var distance_squared := (target_position - agent.position).length_squared()
|
2019-12-16 17:22:03 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var speed_squared := agent.linear_velocity.length_squared()
|
|
|
|
var predict_time := max_predict_time
|
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
|
2019-12-16 17:22:03 +01:00
|
|
|
if predict_time_squared < max_predict_time * max_predict_time:
|
|
|
|
predict_time = sqrt(predict_time_squared)
|
|
|
|
|
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()
|
2019-12-16 17:22:03 +01:00
|
|
|
|
|
|
|
acceleration.angular = 0
|
|
|
|
|
|
|
|
return acceleration
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _get_modified_acceleration() -> float:
|
|
|
|
return agent.max_linear_acceleration
|