Behaviors cleanup part3.

This commit is contained in:
Relintai 2023-01-14 02:47:55 +01:00
parent 6f27129fe5
commit 693ec871e5
7 changed files with 92 additions and 100 deletions

View File

@ -1,12 +1,16 @@
#include "gsai_arrive.h"
GSAIAgentLocation GSAIArrive::get_ *target() {
return *target;
#include "../gsai_steering_agent.h"
#include "../gsai_target_acceleration.h"
#include "../gsai_utils.h"
Ref<GSAIAgentLocation> GSAIArrive::get_target() {
return target;
}
void GSAIArrive::set_ *target(const GSAIAgentLocation &val) {
*target = val;
void GSAIArrive::set_target(const Ref<GSAIAgentLocation> &val) {
target = val;
}
float GSAIArrive::get_arrival_tolerance() const {
@ -33,52 +37,39 @@ void GSAIArrive::set_time_to_reach(const float val) {
time_to_reach = val;
}
// Calculates acceleration to take an agent to its target's location. The;
// calculation attempts to arrive with zero remaining velocity.;
// @category - Individual behaviors;
// Target agent to arrive to.;
GSAIAgentLocation *target;
// Distance from the target for the agent to be considered successfully;
// arrived.;
float arrival_tolerance = 0.0;
// Distance from the target for the agent to begin slowing down.;
float deceleration_radius = 0.0;
// Represents the time it takes to change acceleration.;
float time_to_reach = 0.1;
void GSAIArrive::arrive(const GSAITargetAcceleration &acceleration, const Vector3 &target_position) {
void GSAIArrive::arrive(const Ref<GSAITargetAcceleration> &acceleration, const Vector3 &target_position) {
call("_arrive", acceleration, target_position);
}
void GSAIArrive::_arrive(const GSAITargetAcceleration &acceleration, const Vector3 &target_position) {
Vector3 to_target = target_position - agent.position;
void GSAIArrive::_arrive(Ref<GSAITargetAcceleration> acceleration, Vector3 target_position) {
ERR_FAIL_COND(!agent.is_valid());
Vector3 to_target = target_position - agent->get_position();
float distance = to_target.length();
if (distance <= arrival_tolerance) {
acceleration.set_zero();
}
else {
float desired_speed = agent.linear_speed_max;
acceleration->set_zero();
} else {
float desired_speed = agent->get_linear_speed_max();
if (distance <= deceleration_radius) {
desired_speed *= distance / deceleration_radius;
}
Vector3 desired_velocity = to_target * desired_speed / distance;
desired_velocity = ((desired_velocity - agent.linear_velocity) * 1.0 / time_to_reach);
acceleration.linear = GSAIUtils.clampedv3(desired_velocity, agent.linear_acceleration_max);
acceleration.angular = 0;
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);
}
}
void GSAIArrive::_calculate_steering(const GSAITargetAcceleration &acceleration) {
arrive(acceleration, target.position);
}
void GSAIArrive::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
ERR_FAIL_COND(!target.is_valid());
arrive(acceleration, target->get_position());
}
GSAIArrive::GSAIArrive() {
*target;
arrival_tolerance = 0.0;
deceleration_radius = 0.0;
time_to_reach = 0.1;
@ -87,10 +78,10 @@ GSAIArrive::GSAIArrive() {
GSAIArrive::~GSAIArrive() {
}
static 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");
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");
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);
@ -104,7 +95,7 @@ static void GSAIArrive::_bind_methods() {
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");
BIND_VMETHOD(MethodInfo("_arrive", PropertyInfo(Variant::OBJECT, "acceleration", PROPERTY_HINT_RESOURCE_TYPE, "GSAITargetAcceleration"), PropertyInfo(Variant::VECTOR3, "target_position")));
ClassDB::bind_method(D_METHOD("arrive", "acceleration", "target_position"), &GSAIArrive::arrive);
ClassDB::bind_method(D_METHOD("_arrive", "acceleration", "target_position"), &GSAIArrive::_arrive);
ClassDB::bind_method(D_METHOD("_calculate_steering", "acceleration"), &GSAIArrive::_calculate_steering);
}

View File

@ -1,12 +1,18 @@
#ifndef GSAI_ARRIVE_H
#define GSAI_ARRIVE_H
#include "core/object/reference.h"
#include "../gsai_steering_behavior.h"
class GSAIAgentLocation;
class GSAIArrive : public GSAISteeringBehavior {
GDCLASS(GSAIArrive, GSAISteeringBehavior);
public:
GSAIAgentLocation get_ *target();
void set_ *target(const GSAIAgentLocation &val);
Ref<GSAIAgentLocation> get_target();
void set_target(const Ref<GSAIAgentLocation> &val);
float get_arrival_tolerance() const;
void set_arrival_tolerance(const float val);
@ -17,10 +23,10 @@ public:
float get_time_to_reach() const;
void set_time_to_reach(const float val);
void arrive(const GSAITargetAcceleration &acceleration, const Vector3 &target_position);
void _arrive(const GSAITargetAcceleration &acceleration, const Vector3 &target_position);
void _calculate_steering(const GSAITargetAcceleration &acceleration);
void arrive(const Ref<GSAITargetAcceleration> &acceleration, const Vector3 &target_position);
virtual void _arrive(Ref<GSAITargetAcceleration> acceleration, Vector3 target_position);
void _calculate_steering(Ref<GSAITargetAcceleration> acceleration);
GSAIArrive();
~GSAIArrive();
@ -32,14 +38,14 @@ protected:
// calculation attempts to arrive with zero remaining velocity.
// @category - Individual behaviors
// Target agent to arrive to.
GSAIAgentLocation *target;
Ref<GSAIAgentLocation> target;
// Distance from the target for the agent to be considered successfully
// arrived.
float arrival_tolerance = 0.0;
float arrival_tolerance;
// Distance from the target for the agent to begin slowing down.
float deceleration_radius = 0.0;
float deceleration_radius;
// Represents the time it takes to change acceleration.
float time_to_reach = 0.1;
float time_to_reach;
};
#endif

View File

@ -12,7 +12,7 @@ class GSAIFace : public GSAIMatchOrientation {
public:
void face(const Ref<GSAITargetAcceleration> &acceleration, const Vector3 &target_position);
void _face(Ref<GSAITargetAcceleration> acceleration, Vector3 target_position);
virtual void _face(Ref<GSAITargetAcceleration> acceleration, Vector3 target_position);
void _calculate_steering(Ref<GSAITargetAcceleration> acceleration);

View File

@ -1,12 +1,16 @@
#include "gsai_follow_path.h"
GSAIPath GSAIFollowPath::get_ *path() {
return *path;
#include "../gsai_path.h"
#include "../gsai_steering_agent.h"
#include "../gsai_target_acceleration.h"
Ref<GSAIPath> GSAIFollowPath::get_path() {
return path;
}
void GSAIFollowPath::set_ *path(const GSAIPath &val) {
*path = val;
void GSAIFollowPath::set_path(const Ref<GSAIPath> &val) {
path = val;
}
float GSAIFollowPath::get_path_offset() const {
@ -33,50 +37,36 @@ void GSAIFollowPath::set_prediction_time(const float val) {
prediction_time = val;
}
// Produces a linear acceleration that moves the agent along the specified path.;
// @category - Individual behaviors;
// The path to follow and travel along.;
GSAIPath *path;
// The distance along the path to generate the next target position.;
float path_offset = 0.0;
// Whether to use `GSAIArrive` behavior on an open path.;
bool is_arrive_enabled = true;
// The amount of time in the future to predict the owning agent's position along;
// the path. Setting it to 0.0 will force non-predictive path following.;
float prediction_time = 0.0;
void GSAIFollowPath::_calculate_steering(Ref<GSAITargetAcceleration> acceleration) {
ERR_FAIL_COND(!agent.is_valid());
ERR_FAIL_COND(!path.is_valid());
void GSAIFollowPath::_calculate_steering(const GSAITargetAcceleration &acceleration) {
Vector3 location = ;
Vector3 location;
if (prediction_time == 0) {
location = agent.position;
location = agent->get_position();
} else {
location = agent->get_position() + (agent->get_linear_velocity() * prediction_time);
}
else {
location = agent.position + (agent.linear_velocity * prediction_time);
}
float distance = path.calculate_distance(location);
float distance = path->calculate_distance(location);
float target_distance = distance + path_offset;
if (prediction_time > 0 && path.is_open) {
if (target_distance < path.calculate_distance(agent.position)) {
target_distance = path.length;
if (prediction_time > 0 && path->get_is_open()) {
if (target_distance < path->calculate_distance(agent->get_position())) {
target_distance = path->get_length();
}
}
Vector3 target_position = path.calculate_target_position(target_distance);
Vector3 target_position = path->calculate_target_position(target_distance);
if (is_arrive_enabled && path.is_open) {
if (is_arrive_enabled && path->get_is_open()) {
if (path_offset >= 0) {
if (target_distance > path.length - deceleration_radius) {
if (target_distance > path->get_length() - deceleration_radius) {
arrive(acceleration, target_position);
return;
}
}
else {
} else {
if (target_distance < deceleration_radius) {
arrive(acceleration, target_position);
return;
@ -84,14 +74,14 @@ void GSAIFollowPath::_calculate_steering(const GSAITargetAcceleration &accelerat
}
}
acceleration.linear = (target_position - agent.position).normalized();
acceleration.linear *= agent.linear_acceleration_max;
acceleration.angular = 0;
}
Vector3 linear = (target_position - agent->get_position()).normalized();
linear *= agent->get_linear_acceleration_max();
acceleration->set_linear(linear);
acceleration->set_angular(0);
}
GSAIFollowPath::GSAIFollowPath() {
*path;
path_offset = 0.0;
is_arrive_enabled = true;
prediction_time = 0.0;
@ -100,10 +90,10 @@ GSAIFollowPath::GSAIFollowPath() {
GSAIFollowPath::~GSAIFollowPath() {
}
static void GSAIFollowPath::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_*path"), &GSAIFollowPath::get_ * path);
ClassDB::bind_method(D_METHOD("set_*path", "value"), &GSAIFollowPath::set_ * path);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "*path", PROPERTY_HINT_RESOURCE_TYPE, "GSAIPath"), "set_*path", "get_*path");
void GSAIFollowPath::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_path"), &GSAIFollowPath::get_path);
ClassDB::bind_method(D_METHOD("set_path", "value"), &GSAIFollowPath::set_path);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "path", PROPERTY_HINT_RESOURCE_TYPE, "GSAIPath"), "set_path", "get_path");
ClassDB::bind_method(D_METHOD("get_path_offset"), &GSAIFollowPath::get_path_offset);
ClassDB::bind_method(D_METHOD("set_path_offset", "value"), &GSAIFollowPath::set_path_offset);
@ -116,6 +106,4 @@ static void GSAIFollowPath::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_prediction_time"), &GSAIFollowPath::get_prediction_time);
ClassDB::bind_method(D_METHOD("set_prediction_time", "value"), &GSAIFollowPath::set_prediction_time);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "prediction_time"), "set_prediction_time", "get_prediction_time");
ClassDB::bind_method(D_METHOD("_calculate_steering", "acceleration"), &GSAIFollowPath::_calculate_steering);
}

View File

@ -1,12 +1,18 @@
#ifndef GSAI_FOLLOW_PATH_H
#define GSAI_FOLLOW_PATH_H
#include "core/object/reference.h"
#include "gsai_arrive.h"
class GSAIPath;
class GSAIFollowPath : public GSAIArrive {
GDCLASS(GSAIFollowPath, GSAIArrive);
public:
GSAIPath get_path();
void set_path(const GSAIPath &val);
Ref<GSAIPath> get_path();
void set_path(const Ref<GSAIPath> &val);
float get_path_offset() const;
void set_path_offset(const float val);
@ -17,7 +23,7 @@ public:
float get_prediction_time() const;
void set_prediction_time(const float val);
void _calculate_steering(const GSAITargetAcceleration &acceleration);
void _calculate_steering(Ref<GSAITargetAcceleration> acceleration);
GSAIFollowPath();
~GSAIFollowPath();
@ -28,14 +34,14 @@ protected:
// Produces a linear acceleration that moves the agent along the specified path.
// @category - Individual behaviors
// The path to follow and travel along.
GSAIPath *path;
Ref<GSAIPath> path;
// The distance along the path to generate the next target position.
float path_offset = 0.0;
float path_offset;
// Whether to use `GSAIArrive` behavior on an open path.
bool is_arrive_enabled = true;
bool is_arrive_enabled;
// The amount of time in the future to predict the owning agent's position along
// the path. Setting it to 0.0 will force non-predictive path following.
float prediction_time = 0.0;
float prediction_time;
};
#endif

View File

@ -28,7 +28,7 @@ public:
void set_use_z(const bool val);
void match_orientation(const Ref<GSAITargetAcceleration> &acceleration, const float desired_orientation);
void _match_orientation(Ref<GSAITargetAcceleration> acceleration, float desired_orientation);
virtual void _match_orientation(Ref<GSAITargetAcceleration> acceleration, float desired_orientation);
void _calculate_steering(Ref<GSAITargetAcceleration> acceleration);

View File

@ -19,6 +19,7 @@ public:
void set_predict_time_max(const float val);
void _calculate_steering(Ref<GSAITargetAcceleration> acceleration);
float get_modified_acceleration();
virtual float _get_modified_acceleration();