aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AISystem.cpp
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-06 10:46:16 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-06 10:46:16 +0100
commit82863204048d65073bc5598dcb62f31e32b51430 (patch)
tree93a52ec686677d0a915db1d84aae7322be2321c2 /src/crepe/system/AISystem.cpp
parent9c2cafd85ed7aa9a860ba25fbe2bd3ccc2439f29 (diff)
Corrected accumulate_force()
Diffstat (limited to 'src/crepe/system/AISystem.cpp')
-rw-r--r--src/crepe/system/AISystem.cpp21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index d496e12..4858000 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -33,7 +33,7 @@ vec2 AISystem::calculate(AI & ai) {
if (ai.on(AI::BehaviorType::SEEK)) {
vec2 force_to_add = this->seek(ai);
- if (!this->accumulate_force(force, force_to_add)) {
+ if (!this->accumulate_force(ai, force, force_to_add)) {
return force;
}
}
@@ -50,16 +50,23 @@ vec2 AISystem::calculate(AI & ai) {
return force;
}
-bool AISystem::accumulate_force(vec2 & running_total, vec2 force_to_add) {
- double magnitude_remaining = running_total.length();
- double magnitude_to_add = force_to_add.length();
+bool AISystem::accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add) {
+ float magnitude = running_total.length();
+ float magnitude_remaining = ai.max_force - magnitude;
- if (magnitude_remaining + magnitude_to_add > 0) {
+ if (magnitude_remaining <= 0.0f) {
+ return false;
+ }
+
+ float magnitude_to_add = force_to_add.length();
+ if (magnitude_to_add < magnitude_remaining) {
running_total += force_to_add;
- return true;
+ } else {
+ force_to_add.normalize();
+ running_total += force_to_add * magnitude_remaining;
}
- return false;
+ return true;
}
vec2 AISystem::seek(const AI & ai) {