hx3d  1
2D/3D Simple Game Framework
attractor.cpp
1 #include "hx3d/physics/2d/attractor.hpp"
2 
3 namespace hx3d {
4 namespace physics2d {
5 
7  type(type),
8  priority(0)
9  {}
10 
12 
13 void Attractor::setCurrentAttractor(const Ptr<Collider>& collider, const Ptr<Attractor>& attractor, const float dt) {
14  collider->currentAttractor = attractor;
15  attractor->computeForce(collider, dt);
16 }
17 
18 void Attractor::applyForce(const Ptr<Collider>& collider, const Ptr<Attractor>& attractor, const float dt) {
19  if (attractor->overlaps(collider)) {
20  const Ptr<Attractor>& current = collider->currentAttractor;
21  if (!current) {
22  setCurrentAttractor(collider, attractor, dt);
23  return;
24  }
25 
26  if (current == attractor) {
27  attractor->computeForce(collider, dt);
28  return;
29  }
30 
31  unsigned int currentType = static_cast<unsigned int>(current->type);
32  unsigned int attractorType = static_cast<unsigned int>(attractor->type);
33 
34  if (attractorType > currentType) {
35  setCurrentAttractor(collider, attractor, dt);
36  }
37 
38  else if (attractorType == currentType) {
39  if (attractor->priority > current->priority) {
40  setCurrentAttractor(collider, attractor, dt);
41  }
42  }
43 
44  else {
45  if (!current->overlaps(collider)) {
46  setCurrentAttractor(collider, attractor, dt);
47  }
48  }
49  }
50 }
51 
52 } /* physics2d */
53 } /* hx3d */
Type
Attractor type.
Definition: attractor.hpp:39
Attractor(Type type)
Create an attractor.
Definition: attractor.cpp:6
static void applyForce(const Ptr< Collider > &collider, const Ptr< Attractor > &attractor, const float dt)
Apply an attractor force on a collider.
Definition: attractor.cpp:18
hx3d framework namespace
Definition: audio.hpp:26
static void setCurrentAttractor(const Ptr< Collider > &collider, const Ptr< Attractor > &attractor, const float dt)
Defines the current attractor on a collider.
Definition: attractor.cpp:13
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34