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-13 21:35:07 +01:00
|
|
|
GSAISteeringAgent GSAIPursue::get_ *target() {
|
|
|
|
return *target;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIPursue::set_ *target(const 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-13 21:35:07 +01:00
|
|
|
// Calculates an acceleration to make an agent intercept another based on the;
|
|
|
|
// target agent's movement.;
|
|
|
|
// @category - Individual behaviors;
|
|
|
|
// The target agent that the behavior is trying to intercept.;
|
|
|
|
GSAISteeringAgent *target;
|
|
|
|
// The maximum amount of time in the future the behavior predicts the target's;
|
|
|
|
// location.;
|
|
|
|
float predict_time_max = 1.0;
|
|
|
|
|
|
|
|
void GSAIPursue::_calculate_steering(const GSAITargetAcceleration &acceleration) {
|
|
|
|
Vector3 target_position = target.position;
|
|
|
|
float distance_squared = (target_position - agent.position).length_squared();
|
|
|
|
float speed_squared = agent.linear_velocity.length_squared();
|
|
|
|
float predict_time = predict_time_max;
|
|
|
|
|
|
|
|
if (speed_squared > 0) {
|
|
|
|
Variant predict_time_squared = distance_squared / speed_squared;
|
|
|
|
|
|
|
|
if (predict_time_squared < predict_time_max * predict_time_max) {
|
|
|
|
predict_time = sqrt(predict_time_squared);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acceleration.linear = ((target_position + (target.linear_velocity * predict_time)) - agent.position).normalized();
|
|
|
|
acceleration.linear *= get_modified_acceleration();
|
|
|
|
acceleration.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() {
|
|
|
|
return agent.linear_acceleration_max;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPursue::GSAIPursue() {
|
|
|
|
*target;
|
|
|
|
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-13 21:35:07 +01:00
|
|
|
static 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-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_calculate_steering", "acceleration"), &GSAIPursue::_calculate_steering);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_modified_acceleration"), &GSAIPursue::get_modified_acceleration);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_modified_acceleration"), &GSAIPursue::_get_modified_acceleration);
|
|
|
|
}
|