2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_face.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
// Calculates angular acceleration to rotate a target to face its target's;
|
|
|
|
// position. The behavior attemps to arrive with zero remaining angular velocity.;
|
|
|
|
// @category - Individual behaviors;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIFace::face(const GSAITargetAcceleration &acceleration, const Vector3 &target_position) {
|
|
|
|
call("_face", acceleration, target_position);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIFace::_face(const GSAITargetAcceleration &acceleration, const Vector3 &target_position) {
|
|
|
|
Vector3 to_target = target_position - agent.position;
|
|
|
|
float distance_squared = to_target.length_squared();
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (distance_squared < agent.zero_linear_speed_threshold) {
|
|
|
|
acceleration.set_zero();
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
float orientation = ;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (use_z) {
|
|
|
|
orientation = GSAIUtils.vector3_to_angle(to_target);
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
orientation = GSAIUtils.vector2_to_angle(GSAIUtils.to_vector2(to_target));
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
match_orientation(acceleration, orientation);
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIFace::_calculate_steering(const GSAITargetAcceleration &acceleration) {
|
|
|
|
face(acceleration, target.position);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIFace::GSAIFace() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIFace::~GSAIFace() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
static void GSAIFace::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("face", "acceleration", "target_position"), &GSAIFace::face);
|
|
|
|
ClassDB::bind_method(D_METHOD("_face", "acceleration", "target_position"), &GSAIFace::_face);
|
|
|
|
ClassDB::bind_method(D_METHOD("_calculate_steering", "acceleration"), &GSAIFace::_calculate_steering);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|