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
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
#include "../gsai_steering_agent.h"
|
|
|
|
#include "../gsai_target_acceleration.h"
|
|
|
|
#include "../proximities/gsai_proximity.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-14 01:43:20 +01:00
|
|
|
Ref<GSAITargetAcceleration> GSAISeparation::get_acceleration() {
|
|
|
|
return acceleration;
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
void GSAISeparation::set_acceleration(const Ref<GSAITargetAcceleration> &val) {
|
|
|
|
acceleration = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
void GSAISeparation::_calculate_steering(Ref<GSAITargetAcceleration> p_acceleration) {
|
|
|
|
acceleration = p_acceleration;
|
|
|
|
|
|
|
|
acceleration->set_zero();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!proximity.is_valid());
|
|
|
|
|
|
|
|
proximity->find_neighbors(_callback);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
bool GSAISeparation::_report_neighbor(Ref<GSAISteeringAgent> p_neighbor) {
|
|
|
|
ERR_FAIL_COND_V(!agent.is_valid(), false);
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
Vector3 to_agent = agent->get_position() - p_neighbor->get_position();
|
2023-01-13 21:35:07 +01:00
|
|
|
float distance_squared = to_agent.length_squared();
|
2023-01-14 01:43:20 +01:00
|
|
|
float acceleration_max = agent->get_linear_acceleration_max();
|
|
|
|
float 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-14 01:43:20 +01:00
|
|
|
acceleration->set_linear(acceleration->get_linear() + to_agent * (strength / sqrt(distance_squared)));
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
return true;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAISeparation::GSAISeparation() {
|
|
|
|
decay_coefficient = 1.0;
|
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-14 01:43:20 +01:00
|
|
|
void GSAISeparation::_bind_methods() {
|
2023-01-13 21:35:07 +01:00
|
|
|
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-14 01:43:20 +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:35:07 +01:00
|
|
|
}
|