SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
WeightedSteering.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "Steering.h"
7 
8 namespace ai {
9 namespace movement {
10 
14 struct WeightedData {
15  SteeringPtr steering;
16  const float weight;
17 
18  WeightedData(const SteeringPtr& _steering, float _weight = 1.0f) :
19  steering(_steering), weight(_weight) {
20  ai_assert(weight > 0.0001f, "Weight is too small");
21  }
22 };
23 typedef std::vector<WeightedData> WeightedSteerings;
24 typedef WeightedSteerings::const_iterator WeightedSteeringsIter;
25 
30 private:
31  WeightedSteerings _steerings;
32 public:
33  explicit WeightedSteering(const WeightedSteerings& steerings) :
34  _steerings(steerings) {
35  }
36 
37  MoveVector execute (const AIPtr& ai, float speed) const {
38  float totalWeight = 0.0f;
39  glm::vec3 vecBlended(0.0f);
40  float angularBlended = 0.0f;
41  for (const WeightedData& wd : _steerings) {
42  const float weight = wd.weight;
43  const MoveVector& mv = wd.steering->execute(ai, speed);
44  if (isInfinite(mv.getVector())) {
45  continue;
46  }
47 
48  vecBlended += mv.getVector() * weight;
49  angularBlended += mv.getRotation() * weight;
50  totalWeight += weight;
51  }
52 
53  if (totalWeight <= 0.0000001f) {
54  return MoveVector(VEC3_INFINITE, 0.0f);
55  }
56 
57  const float scale = 1.0f / totalWeight;
58  return MoveVector(vecBlended * scale, fmodf(angularBlended * scale, glm::two_pi<float>()));
59  }
60 };
61 
62 }
63 }
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
Definition: MoveVector.h:10
Defines some basic movement algorithms like Wandering, Seeking and Fleeing.
This class allows you to weight several steering methods and get a blended MoveVector out of it...
Definition: WeightedSteering.h:29
Steering and weight as input for WeightedSteering.
Definition: WeightedSteering.h:14