2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_separation.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
|
|
|
float GSAISeparation::get_decay_coefficient() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return decay_coefficient;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAISeparation::set_decay_coefficient(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
decay_coefficient = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAITargetAcceleration GSAISeparation::get_ *acceleration() {
|
|
|
|
return *acceleration;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAISeparation::set_ *acceleration(const GSAITargetAcceleration &val) {
|
|
|
|
*acceleration = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
// Calculates an acceleration that repels the agent from its neighbors in the;
|
|
|
|
// given `GSAIProximity`.;
|
|
|
|
//;
|
|
|
|
// 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.;
|
|
|
|
// @category - Group behaviors;
|
|
|
|
// The coefficient to calculate how fast the separation strength decays with distance.;
|
|
|
|
float decay_coefficient = 1.0;
|
|
|
|
GSAITargetAcceleration *acceleration;
|
|
|
|
|
|
|
|
void GSAISeparation::_calculate_steering(const GSAITargetAcceleration &_acceleration) {
|
|
|
|
self.acceleration = _acceleration;
|
|
|
|
acceleration.set_zero();
|
|
|
|
// warning-ignore:return_value_discarded;
|
|
|
|
proximity.find_neighbors(_callback);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +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.;
|
|
|
|
// @tags - virtual;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
bool GSAISeparation::_report_neighbor(const GSAISteeringAgent &neighbor) {
|
|
|
|
Vector3 to_agent = agent.position - neighbor.position;
|
|
|
|
float distance_squared = to_agent.length_squared();
|
|
|
|
float acceleration_max = agent.linear_acceleration_max;
|
|
|
|
Variant strength = decay_coefficient / distance_squared;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (strength > acceleration_max) {
|
|
|
|
strength = acceleration_max;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
acceleration.linear += to_agent * (strength / sqrt(distance_squared));
|
|
|
|
return true;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAISeparation::GSAISeparation() {
|
|
|
|
decay_coefficient = 1.0;
|
|
|
|
*acceleration;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAISeparation::~GSAISeparation() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
static void GSAISeparation::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_decay_coefficient"), &GSAISeparation::get_decay_coefficient);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_decay_coefficient", "value"), &GSAISeparation::set_decay_coefficient);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "decay_coefficient"), "set_decay_coefficient", "get_decay_coefficient");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_*acceleration"), &GSAISeparation::get_ * acceleration);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_*acceleration", "value"), &GSAISeparation::set_ * acceleration);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "*acceleration", PROPERTY_HINT_RESOURCE_TYPE, "GSAITargetAcceleration"), "set_*acceleration", "get_*acceleration");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_calculate_steering", "_acceleration"), &GSAISeparation::_calculate_steering);
|
|
|
|
ClassDB::bind_method(D_METHOD("_report_neighbor", "neighbor"), &GSAISeparation::_report_neighbor);
|
|
|
|
}
|