aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
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
parent9c2cafd85ed7aa9a860ba25fbe2bd3ccc2439f29 (diff)
Corrected accumulate_force()
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/system/AISystem.cpp21
-rw-r--r--src/crepe/system/AISystem.h5
2 files changed, 18 insertions, 8 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) {
diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h
index 5e94ccb..18f1c61 100644
--- a/src/crepe/system/AISystem.h
+++ b/src/crepe/system/AISystem.h
@@ -15,9 +15,12 @@ public:
private:
vec2 calculate(AI & ai);
- bool accumulate_force(vec2 & running_total, vec2 force_to_add);
+ bool accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add);
vec2 seek(const AI & ai);
+ vec2 flee(const AI & ai);
+ vec2 arrive(const AI & ai);
+ vec2 path_follow(const AI & ai);
};
} // namespace crepe