2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_pursue.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
|
|
|
|
#include "../gsai_steering_agent.h"
|
|
|
|
#include "../gsai_target_acceleration.h"
|
|
|
|
|
|
|
|
Ref<GSAISteeringAgent> GSAIPursue::get_target() {
|
|
|
|
return target;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
void GSAIPursue::set_target(const Ref<GSAISteeringAgent> &val) {
|
|
|
|
target = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIPursue::get_predict_time_max() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return predict_time_max;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPursue::set_predict_time_max(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
predict_time_max = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
void GSAIPursue::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
|
|
|
|
ERR_FAIL_COND(!target.is_valid());
|
|
|
|
ERR_FAIL_COND(!agent.is_valid());
|
|
|
|
|
|
|
|
Vector3 target_position = target->get_position();
|
|
|
|
float distance_squared = (target_position - agent->get_position()).length_squared();
|
|
|
|
float speed_squared = agent->get_linear_velocity().length_squared();
|
2023-01-13 21:35:07 +01:00
|
|
|
float predict_time = predict_time_max;
|
|
|
|
|
|
|
|
if (speed_squared > 0) {
|
2023-01-14 01:43:20 +01:00
|
|
|
float predict_time_squared = distance_squared / speed_squared;
|
2023-01-13 21:35:07 +01:00
|
|
|
|
|
|
|
if (predict_time_squared < predict_time_max * predict_time_max) {
|
2023-01-14 01:43:20 +01:00
|
|
|
predict_time = Math::sqrt(predict_time_squared);
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
Vector3 linear = ((target_position + (target->get_linear_velocity() * predict_time)) - agent->get_position()).normalized();
|
|
|
|
linear *= get_modified_acceleration();
|
|
|
|
|
|
|
|
acceleration->set_linear(linear);
|
|
|
|
acceleration->set_angular(0);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
float GSAIPursue::get_modified_acceleration() {
|
|
|
|
return call("_get_modified_acceleration");
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
float GSAIPursue::_get_modified_acceleration() {
|
2023-01-14 01:43:20 +01:00
|
|
|
ERR_FAIL_COND_V(!agent.is_valid(), 0);
|
|
|
|
|
|
|
|
return agent->get_linear_acceleration_max();
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPursue::GSAIPursue() {
|
|
|
|
predict_time_max = 1.0;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPursue::~GSAIPursue() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
void GSAIPursue::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_target"), &GSAIPursue::get_target);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_target", "value"), &GSAIPursue::set_target);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "target", PROPERTY_HINT_RESOURCE_TYPE, "GSAISteeringAgent"), "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_predict_time_max"), &GSAIPursue::get_predict_time_max);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_predict_time_max", "value"), &GSAIPursue::set_predict_time_max);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "predict_time_max"), "set_predict_time_max", "get_predict_time_max");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-14 01:43:20 +01:00
|
|
|
BIND_VMETHOD(MethodInfo(Variant::REAL, "_get_modified_acceleration"));
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_modified_acceleration"), &GSAIPursue::get_modified_acceleration);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_modified_acceleration"), &GSAIPursue::_get_modified_acceleration);
|
|
|
|
}
|