2020-01-29 17:04:04 +01:00
|
|
|
# Calculates an acceleration that repels the agent from its neighbors in the
|
|
|
|
# given `GSTProximity`.
|
|
|
|
#
|
|
|
|
# The acceleration is an average based on all neighbors, multiplied by a
|
|
|
|
# strength decreasing by the inverse square law in relation to distance, and it
|
|
|
|
# accumulates.
|
2020-01-29 05:56:10 +01:00
|
|
|
class_name GSTSeparation
|
|
|
|
extends GSTGroupBehavior
|
2020-01-08 18:46:42 +01:00
|
|
|
|
|
|
|
|
2020-01-29 05:56:10 +01:00
|
|
|
# The coefficient to calculate how fast the separation strength decays with distance.
|
2020-01-16 09:44:44 +01:00
|
|
|
var decay_coefficient := 1.0
|
2020-01-30 19:06:35 +01:00
|
|
|
|
|
|
|
var _acceleration: GSTTargetAcceleration
|
2020-01-08 18:46:42 +01:00
|
|
|
|
|
|
|
|
2020-01-10 18:15:50 +01:00
|
|
|
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent, proximity) -> void:
|
|
|
|
pass
|
2020-01-08 18:46:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
|
|
|
acceleration.set_zero()
|
2020-01-30 19:06:35 +01:00
|
|
|
self._acceleration = acceleration
|
2020-01-29 22:53:57 +01:00
|
|
|
proximity._find_neighbors(_callback)
|
2020-01-08 18:46:42 +01:00
|
|
|
return acceleration
|
|
|
|
|
|
|
|
|
2020-01-29 05:56:10 +01:00
|
|
|
# Callback for the proximity to call when finding neighbors. Determines the amount of
|
|
|
|
# acceleration that `neighbor` imposes based on its distance from the owner agent.
|
2020-01-30 19:06:35 +01:00
|
|
|
# virtual
|
2020-01-29 22:53:57 +01:00
|
|
|
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
|
2020-01-16 09:44:44 +01:00
|
|
|
var to_agent := agent.position - neighbor.position
|
2020-01-08 18:46:42 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var distance_squared := to_agent.length_squared()
|
2020-01-22 17:55:49 +01:00
|
|
|
var acceleration_max := agent.linear_acceleration_max
|
2020-01-08 18:46:42 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
var strength := decay_coefficient / distance_squared
|
2020-01-22 17:55:49 +01:00
|
|
|
if strength > acceleration_max:
|
|
|
|
strength = acceleration_max
|
2020-01-29 17:04:04 +01:00
|
|
|
|
2020-01-30 19:06:35 +01:00
|
|
|
_acceleration.linear += to_agent * (strength / sqrt(distance_squared))
|
2020-01-08 18:46:42 +01:00
|
|
|
|
|
|
|
return true
|