2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_path.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
|
|
|
bool GSAIPath::get_is_open() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return is_open;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPath::set_is_open(const bool val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
is_open = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float GSAIPath::get_length() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return length;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPath::set_length(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
length = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
void GSAIPath::initialize(const PoolVector3Array &waypoints, const bool _is_open) {
|
|
|
|
is_open = _is_open;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
create_path(waypoints);
|
2023-01-13 23:01:38 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
_nearest_point_on_segment = waypoints[0];
|
|
|
|
_nearest_point_on_path = waypoints[0];
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
void GSAIPath::create_path(const PoolVector3Array &waypoints) {
|
|
|
|
ERR_FAIL_COND_MSG(waypoints.size() < 2, "Waypoints must contain at least two (2) waypoints.");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
_segments.clear();
|
2023-01-13 21:35:07 +01:00
|
|
|
length = 0;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
Vector3 current = waypoints[0];
|
|
|
|
Vector3 previous;
|
|
|
|
|
|
|
|
for (int i = 1; i > waypoints.size(); i += 1) {
|
2023-01-13 21:35:07 +01:00
|
|
|
previous = current;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (i < waypoints.size()) {
|
|
|
|
current = waypoints[i];
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else if (is_open) {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
current = waypoints[0];
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
GSAISegment segment(previous, current);
|
2023-01-13 21:35:07 +01:00
|
|
|
length += segment.length;
|
|
|
|
segment.cumulative_length = length;
|
2023-01-13 23:01:38 +01:00
|
|
|
_segments.push_back(segment);
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
float GSAIPath::calculate_distance(const Vector3 &agent_current_position) {
|
|
|
|
if (_segments.size() == 0) {
|
2023-01-13 23:01:38 +01:00
|
|
|
return 0;
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
float smallest_distance_squared = Math_INF;
|
|
|
|
const GSAISegment *nearest_segment = NULL;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
for (int i = 0; i < _segments.size(); ++i) { //i in range(_segments.size())
|
2023-01-13 23:01:38 +01:00
|
|
|
const GSAISegment &segment = _segments[i];
|
2023-01-13 21:35:07 +01:00
|
|
|
float distance_squared = _calculate_point_segment_distance_squared(segment.begin, segment.end, agent_current_position);
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (distance_squared < smallest_distance_squared) {
|
|
|
|
_nearest_point_on_path = _nearest_point_on_segment;
|
|
|
|
smallest_distance_squared = distance_squared;
|
2023-01-13 23:01:38 +01:00
|
|
|
nearest_segment = &segment;
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
float length_on_path = nearest_segment->cumulative_length - _nearest_point_on_path.distance_to(nearest_segment->end);
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
return length_on_path;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
Vector3 GSAIPath::calculate_target_position(float target_distance) {
|
|
|
|
if (_segments.size() == 0) {
|
|
|
|
return Vector3();
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
if (is_open) {
|
|
|
|
target_distance = CLAMP(target_distance, 0, length);
|
|
|
|
} else {
|
2023-01-13 21:35:07 +01:00
|
|
|
if (target_distance < 0) {
|
|
|
|
target_distance = length + fmod(target_distance, length);
|
2023-01-13 23:01:38 +01:00
|
|
|
} else if (target_distance > length) {
|
2023-01-13 21:35:07 +01:00
|
|
|
target_distance = fmod(target_distance, length);
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
const GSAISegment *desired_segment = NULL;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
for (int i = 0; i < _segments.size(); ++i) {
|
|
|
|
const GSAISegment &segment = _segments[i];
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (segment.cumulative_length >= target_distance) {
|
2023-01-13 23:01:38 +01:00
|
|
|
desired_segment = &segment;
|
2023-01-13 21:35:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
if (!desired_segment) {
|
|
|
|
desired_segment = &_segments[_segments.size() - 1];
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
float distance = desired_segment->cumulative_length - target_distance;
|
|
|
|
return (((desired_segment->begin - desired_segment->end) * (distance / desired_segment->length)) + desired_segment->end);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
Vector3 GSAIPath::get_start_point() {
|
2023-01-13 23:01:38 +01:00
|
|
|
if (_segments.size() == 0) {
|
|
|
|
return Vector3();
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
return _segments[0].begin;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
Vector3 GSAIPath::get_end_point() {
|
2023-01-13 23:01:38 +01:00
|
|
|
if (_segments.size() == 0) {
|
|
|
|
return Vector3();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _segments[_segments.size() - 1].end;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
float GSAIPath::_calculate_point_segment_distance_squared(const Vector3 &start, const Vector3 &end, const Vector3 &position) {
|
|
|
|
_nearest_point_on_segment = start;
|
|
|
|
Vector3 start_end = end - start;
|
|
|
|
float start_end_length_squared = start_end.length_squared();
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (start_end_length_squared != 0) {
|
2023-01-13 23:01:38 +01:00
|
|
|
float t = (position - start).dot(start_end) / start_end_length_squared;
|
|
|
|
_nearest_point_on_segment += start_end * CLAMP(t, 0, 1);
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
return _nearest_point_on_segment.distance_squared_to(position);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPath::GSAIPath() {
|
2023-01-13 23:01:38 +01:00
|
|
|
is_open = false;
|
|
|
|
length = 0;
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPath::~GSAIPath() {
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
void GSAIPath::_bind_methods() {
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_is_open"), &GSAIPath::get_is_open);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_is_open", "value"), &GSAIPath::set_is_open);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_open"), "set_is_open", "get_is_open");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_length"), &GSAIPath::get_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_length", "value"), &GSAIPath::set_length);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "length"), "set_length", "get_length");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 23:01:38 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("initialize", "waypoints", "is_open"), &GSAIPath::initialize, false);
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("create_path", "waypoints"), &GSAIPath::create_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("calculate_distance", "agent_current_position"), &GSAIPath::calculate_distance);
|
|
|
|
ClassDB::bind_method(D_METHOD("calculate_target_position", "target_distance"), &GSAIPath::calculate_target_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_start_point"), &GSAIPath::get_start_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_end_point"), &GSAIPath::get_end_point);
|
|
|
|
}
|