pandemonium_engine/modules/steering_ai/behaviors/gsai_avoid_collisions.cpp

113 lines
4.7 KiB
C++
Raw Normal View History

2023-12-18 00:18:53 +01:00
/*************************************************************************/
/* gsai_avoid_collisions.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2023-01-13 21:29:17 +01:00
#include "gsai_avoid_collisions.h"
2023-01-14 02:31:42 +01:00
#include "../gsai_steering_agent.h"
#include "../gsai_target_acceleration.h"
#include "../proximities/gsai_proximity.h"
2023-01-14 02:31:42 +01:00
#include "core/math/math_defs.h"
#include "core/math/math_funcs.h"
2023-01-14 02:31:42 +01:00
void GSAIAvoidCollisions::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
ERR_FAIL_COND(!proximity.is_valid());
ERR_FAIL_COND(!agent.is_valid());
2023-01-14 02:31:42 +01:00
_shortest_time = Math_INF;
_first_neighbor.unref();
_first_minimum_separation = 0;
_first_distance = 0;
2023-01-14 02:31:42 +01:00
int neighbor_count = proximity->find_neighbors(_callback);
2023-01-14 02:31:42 +01:00
Vector3 linear = acceleration->get_linear();
2023-01-14 02:31:42 +01:00
if (neighbor_count == 0 || !_first_neighbor.is_valid()) {
acceleration->set_zero();
2023-01-14 15:33:52 +01:00
linear = Vector3();
2023-01-14 02:31:42 +01:00
} else {
if ((_first_minimum_separation <= 0 || _first_distance < agent->get_bounding_radius() + _first_neighbor->get_bounding_radius())) {
linear = _first_neighbor->get_position() - agent->get_position();
} else {
linear = (_first_relative_position + (_first_relative_velocity * _shortest_time));
}
}
2023-01-14 02:31:42 +01:00
linear = (linear.normalized() * -agent->get_linear_acceleration_max());
acceleration->set_linear(linear);
acceleration->set_angular(0);
}
2023-01-14 02:31:42 +01:00
bool GSAIAvoidCollisions::_report_neighbor(Ref<GSAISteeringAgent> neighbor) {
ERR_FAIL_COND_V(!agent.is_valid(), false);
2023-01-14 02:31:42 +01:00
Vector3 relative_position = neighbor->get_position() - agent->get_position();
Vector3 relative_velocity = neighbor->get_linear_velocity() - agent->get_linear_velocity();
float relative_speed_squared = relative_velocity.length_squared();
if (relative_speed_squared == 0) {
return false;
2023-01-14 03:14:27 +01:00
} else {
float time_to_collision = -relative_position.dot(relative_velocity) / relative_speed_squared;
if (time_to_collision <= 0 || time_to_collision >= _shortest_time) {
return false;
2023-01-14 02:31:42 +01:00
} else {
float distance = relative_position.length();
float minimum_separation = (distance - Math::sqrt(relative_speed_squared) * time_to_collision);
2023-01-14 02:31:42 +01:00
if (minimum_separation > agent->get_bounding_radius() + neighbor->get_bounding_radius()) {
return false;
2023-01-14 02:31:42 +01:00
} else {
_shortest_time = time_to_collision;
_first_neighbor = neighbor;
_first_minimum_separation = minimum_separation;
_first_distance = distance;
_first_relative_position = relative_position;
_first_relative_velocity = relative_velocity;
return true;
}
}
}
}
GSAIAvoidCollisions::GSAIAvoidCollisions() {
_shortest_time = 0.0;
_first_minimum_separation = 0.0;
_first_distance = 0.0;
}
GSAIAvoidCollisions::~GSAIAvoidCollisions() {
}
2023-01-14 02:31:42 +01:00
void GSAIAvoidCollisions::_bind_methods() {
}