Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUWorldController.h
1 //
2 // CUWorldController.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a Cocos2d wrapper to Box2d that is superior to the one built into
6 // Cocos2d. The existing implementation is coupled with the scene graph, which is bad, bad
7 // bad. This is a way to handle Box2d with proper reference counting and garbage collection
8 // that does not require the scene graph.
9 //
10 // Author: Walker White
11 // Version: 12/5/15
12 //
13 #ifndef __CU_WORLD_CONTROLLER_H__
14 #define __CU_WORLD_CONTROLLER_H__
15 
16 #include <cocos2d.h>
17 #include <vector>
18 #include <Box2D/Dynamics/b2WorldCallbacks.h>
19 class b2World;
20 
21 NS_CC_BEGIN
22 
23 // We need a lot of forward references to the classes used by this controller
24 class Obstacle;
25 
27 #define DEFAULT_WORLD_STEP 1/60.0f
28 
29 #define DEFAULT_WORLD_VELOC 6
30 
31 #define DEFAULT_WORLD_POSIT 2
32 
33 
34 #pragma mark -
35 #pragma mark World Controller
36 
48 class CC_DLL WorldController : public b2ContactListener, b2DestructionListener, b2ContactFilter, public Ref {
49 protected:
51  b2World* _world;
53  bool _lockstep;
55  float _stepssize;
61  Vec2 _gravity;
62 
64  std::vector<Obstacle*> _objects;
65 
67  Rect _bounds;
68 
70  bool _collide;
72  bool _filters;
74  bool _destroy;
75 
76 
77 public:
78 #pragma mark Static Constructors
79 
93  static WorldController* create(const Rect& bounds);
94 
107  static WorldController* create(const Rect& bounds, const Vec2& gravity);
108 
109 
110 #pragma mark -
111 #pragma mark Physics Handling
112 
123  b2World* getWorld() { return _world; }
124 
132  bool isLockStep() const { return _lockstep; }
133 
142  void setLockStep(bool flag) { _lockstep = flag; }
143 
151  float getStepsize() const { return _stepssize; }
152 
161  void setStepsize(float step) { _stepssize = step; }
162 
168  int getVelocityIterations() const { return _itvelocity; }
169 
177  void setVelocityIterations(int velocity) { _itvelocity = velocity; }
178 
184  int getPositionIterations() const { return _itposition; }
185 
193  void setPositionIterations(int position) { _itposition = position; }
194 
200  const Vec2& getGravity() const { return _gravity; }
201 
209  void setGravity(const Vec2& gravity);
210 
225  void update(float dt);
226 
232  const Rect& getBounds() const { return _bounds; }
233 
243  bool inBounds(Obstacle* obj);
244 
245 
246 #pragma mark -
247 #pragma mark Object Management
248 
253  const std::vector<Obstacle*>& getObstacles() { return _objects; }
254 
265  void addObstacle(Obstacle* obj);
266 
281  void removeObstacle(Obstacle* obj);
282 
291  void garbageCollect();
292 
299  void clear();
300 
301 
302 #pragma mark -
303 #pragma mark Collision Callback Functions
304 
313  void activateCollisionCallbacks(bool flag);
314 
323  bool enabledCollisionCallbacks() const { return _collide; }
324 
333  std::function<void(b2Contact* contact)> onBeginContact;
334 
343  std::function<void(b2Contact* contact)> onEndContact;
344 
365  std::function<void(b2Contact* contact, const b2Manifold* oldManifold)> beforeSolve;
366 
384  std::function<void(b2Contact* contact, const b2ContactImpulse* impulse)> afterSolve;
385 
394  void BeginContact(b2Contact* contact) override {
395  if (onBeginContact != nullptr) {
396  onBeginContact(contact);
397  }
398  }
399 
408  void EndContact(b2Contact* contact) override {
409  if (onEndContact != nullptr) {
410  onEndContact(contact);
411  }
412  }
413 
434  void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override {
435  if (beforeSolve != nullptr) {
436  beforeSolve(contact,oldManifold);
437  }
438  }
439 
457  void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) override {
458  if (afterSolve != nullptr) {
459  afterSolve(contact,impulse);
460  }
461  }
462 
463 
464 #pragma mark -
465 #pragma mark Filter Callback Functions
466 
476  void activateFilterCallbacks(bool flag);
477 
487  bool enabledFilterCallbacks() const { return _filters; }
488 
499  std::function<bool(b2Fixture* fixtureA, b2Fixture* fixtureB)> shouldCollide;
500 
511  bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB) override {
512  if (shouldCollide != nullptr) {
513  return shouldCollide(fixtureA,fixtureB);
514  }
515  return false;
516  }
517 
518 
519 #pragma mark -
520 #pragma mark Destruction Callback Functions
521 
530  void activateDestructionCallbacks(bool flag);
531 
541  bool enabledDestructionCallbacks() const { return _destroy; }
542 
551  std::function<void(b2Fixture* fixture)> destroyFixture;
552 
561  std::function<void(b2Joint* joint)> destroyJoint;
562 
571  void SayGoodbye(b2Joint* joint) override {
572  if (destroyJoint != nullptr) {
573  destroyJoint(joint);
574  }
575  }
576 
585  void SayGoodbye(b2Fixture* fixture) override {
586  if (destroyFixture != nullptr) {
587  destroyFixture(fixture);
588  }
589  }
590 
591 
592 #pragma mark -
593 #pragma mark Query Functions
594 
603  void queryAABB(std::function<bool(b2Fixture* fixture)> callback, const Rect& aabb) const;
604 
614  void rayCast(std::function<float(b2Fixture* fixture, const Vec2& point, const Vec2& normal, float fraction)> callback,
615  const Vec2& point1, const Vec2& point2) const;
616 
617 
618 
619 CC_CONSTRUCTOR_ACCESS:
620 #pragma mark -
621 #pragma mark Initializers
622 
627  WorldController();
628 
632  ~WorldController();
633 
648  virtual bool init(const Rect& bounds);
649 
662  virtual bool init(const Rect& bounds, const Vec2& gravity);
663 
664 };
665 
666 NS_CC_END
667 #endif /* defined(__CU_WORLD_CONTROLLER_H__) */
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: CUWorldController.h:434
bool enabledCollisionCallbacks() const
Definition: CUWorldController.h:323
bool enabledDestructionCallbacks() const
Definition: CUWorldController.h:541
b2World * getWorld()
Definition: CUWorldController.h:123
bool _filters
Definition: CUWorldController.h:72
void EndContact(b2Contact *contact) override
Definition: CUWorldController.h:408
float _stepssize
Definition: CUWorldController.h:55
void BeginContact(b2Contact *contact) override
Definition: CUWorldController.h:394
int _itvelocity
Definition: CUWorldController.h:57
std::function< void(b2Contact *contact, const b2Manifold *oldManifold)> beforeSolve
Definition: CUWorldController.h:365
std::function< void(b2Fixture *fixture)> destroyFixture
Definition: CUWorldController.h:551
std::function< bool(b2Fixture *fixtureA, b2Fixture *fixtureB)> shouldCollide
Definition: CUWorldController.h:499
const Vec2 & getGravity() const
Definition: CUWorldController.h:200
std::function< void(b2Contact *contact, const b2ContactImpulse *impulse)> afterSolve
Definition: CUWorldController.h:384
int getPositionIterations() const
Definition: CUWorldController.h:184
void setStepsize(float step)
Definition: CUWorldController.h:161
const std::vector< Obstacle * > & getObstacles()
Definition: CUWorldController.h:253
void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition: CUWorldController.h:457
Rect _bounds
Definition: CUWorldController.h:67
std::function< void(b2Contact *contact)> onBeginContact
Definition: CUWorldController.h:333
bool _collide
Definition: CUWorldController.h:70
bool ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB) override
Definition: CUWorldController.h:511
void SayGoodbye(b2Fixture *fixture) override
Definition: CUWorldController.h:585
Definition: CUWorldController.h:48
bool _destroy
Definition: CUWorldController.h:74
int _itposition
Definition: CUWorldController.h:59
void SayGoodbye(b2Joint *joint) override
Definition: CUWorldController.h:571
float getStepsize() const
Definition: CUWorldController.h:151
void setLockStep(bool flag)
Definition: CUWorldController.h:142
Vec2 _gravity
Definition: CUWorldController.h:61
void setPositionIterations(int position)
Definition: CUWorldController.h:193
b2World * _world
Definition: CUWorldController.h:51
const Rect & getBounds() const
Definition: CUWorldController.h:232
Definition: CUObstacle.h:46
std::function< void(b2Contact *contact)> onEndContact
Definition: CUWorldController.h:343
void setVelocityIterations(int velocity)
Definition: CUWorldController.h:177
bool isLockStep() const
Definition: CUWorldController.h:132
std::function< void(b2Joint *joint)> destroyJoint
Definition: CUWorldController.h:561
bool _lockstep
Definition: CUWorldController.h:53
std::vector< Obstacle * > _objects
Definition: CUWorldController.h:64
bool enabledFilterCallbacks() const
Definition: CUWorldController.h:487
int getVelocityIterations() const
Definition: CUWorldController.h:168