2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_arrive.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
#include "../gsai_steering_agent.h"
|
|
|
|
#include "../gsai_target_acceleration.h"
|
|
|
|
#include "../gsai_utils.h"
|
|
|
|
|
|
|
|
Ref<GSAIAgentLocation> GSAIArrive::get_target() {
|
|
|
|
return target;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
void GSAIArrive::set_target(const Ref<GSAIAgentLocation> &val) {
|
|
|
|
target = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIArrive::get_arrival_tolerance() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return arrival_tolerance;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIArrive::set_arrival_tolerance(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
arrival_tolerance = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIArrive::get_deceleration_radius() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return deceleration_radius;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIArrive::set_deceleration_radius(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
deceleration_radius = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIArrive::get_time_to_reach() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return time_to_reach;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIArrive::set_time_to_reach(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
time_to_reach = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
void GSAIArrive::arrive(const Ref<GSAITargetAcceleration> &acceleration, const Vector3 &target_position) {
|
2023-01-13 21:35:07 +01:00
|
|
|
call("_arrive", acceleration, target_position);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
void GSAIArrive::_arrive(Ref<GSAITargetAcceleration> acceleration, Vector3 target_position) {
|
|
|
|
ERR_FAIL_COND(!agent.is_valid());
|
|
|
|
|
|
|
|
Vector3 to_target = target_position - agent->get_position();
|
2023-01-13 21:35:07 +01:00
|
|
|
float distance = to_target.length();
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (distance <= arrival_tolerance) {
|
2023-01-14 02:47:55 +01:00
|
|
|
acceleration->set_zero();
|
|
|
|
} else {
|
|
|
|
float desired_speed = agent->get_linear_speed_max();
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (distance <= deceleration_radius) {
|
|
|
|
desired_speed *= distance / deceleration_radius;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
Vector3 desired_velocity = to_target * desired_speed / distance;
|
2023-01-14 02:47:55 +01:00
|
|
|
desired_velocity = ((desired_velocity - agent->get_linear_velocity()) * 1.0 / time_to_reach);
|
|
|
|
acceleration->set_linear(GSAIUtils::clampedv3(desired_velocity, agent->get_linear_acceleration_max()));
|
|
|
|
acceleration->set_angular(0);
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
void GSAIArrive::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
|
|
|
|
ERR_FAIL_COND(!target.is_valid());
|
|
|
|
|
|
|
|
arrive(acceleration, target->get_position());
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIArrive::GSAIArrive() {
|
|
|
|
arrival_tolerance = 0.0;
|
|
|
|
deceleration_radius = 0.0;
|
|
|
|
time_to_reach = 0.1;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIArrive::~GSAIArrive() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
void GSAIArrive::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_target"), &GSAIArrive::get_target);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_target", "value"), &GSAIArrive::set_target);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "target", PROPERTY_HINT_RESOURCE_TYPE, "GSAIAgentLocation"), "set_target", "get_target");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_arrival_tolerance"), &GSAIArrive::get_arrival_tolerance);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_arrival_tolerance", "value"), &GSAIArrive::set_arrival_tolerance);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "arrival_tolerance"), "set_arrival_tolerance", "get_arrival_tolerance");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_deceleration_radius"), &GSAIArrive::get_deceleration_radius);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_deceleration_radius", "value"), &GSAIArrive::set_deceleration_radius);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "deceleration_radius"), "set_deceleration_radius", "get_deceleration_radius");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_time_to_reach"), &GSAIArrive::get_time_to_reach);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_time_to_reach", "value"), &GSAIArrive::set_time_to_reach);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_to_reach"), "set_time_to_reach", "get_time_to_reach");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 02:47:55 +01:00
|
|
|
BIND_VMETHOD(MethodInfo("_arrive", PropertyInfo(Variant::OBJECT, "acceleration", PROPERTY_HINT_RESOURCE_TYPE, "GSAITargetAcceleration"), PropertyInfo(Variant::VECTOR3, "target_position")));
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("arrive", "acceleration", "target_position"), &GSAIArrive::arrive);
|
|
|
|
ClassDB::bind_method(D_METHOD("_arrive", "acceleration", "target_position"), &GSAIArrive::_arrive);
|
|
|
|
}
|