CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUObstacleWorld.h
1 //
2 // CUObstacleWorld.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a wrapper to Box2d that for use with CUGL obstacle
6 // heirarchy. Obstacles provide a simple and direct way to create physics
7 // objects that does not require the multi-step approach of Box2D. It also
8 // supports shared pointers for simply memory management.
9 //
10 // However, this class is not as flexible as Box2D. Therefore, it may be
11 // necessary to access Box2D directly at times.
12 //
13 // This class uses our standard shared-pointer architecture.
14 //
15 // 1. The constructor does not perform any initialization; it just sets all
16 // attributes to their defaults.
17 //
18 // 2. All initialization takes place via init methods, which can fail if an
19 // object is initialized more than once.
20 //
21 // 3. All allocation takes place via static constructors which return a shared
22 // pointer.
23 //
24 // CUGL zlib License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // Author: Walker White
44 // Version: 11/1/16
45 #ifndef __CU_PHYSICS_WORLD_H__
46 #define __CU_PHYSICS_WORLD_H__
47 
48 #include <vector>
49 #include <Box2D/Dynamics/b2WorldCallbacks.h>
50 #include <cugl/math/cu_math.h>
51 class b2World;
52 
53 namespace cugl {
54 
55 // Forward declaration of the Obstacle class
56 class Obstacle;
57 
59 #define DEFAULT_WORLD_STEP 1/60.0f
60 
61 #define DEFAULT_WORLD_VELOC 6
62 
63 #define DEFAULT_WORLD_POSIT 2
64 
65 
66 #pragma mark -
67 #pragma mark World Controller
68 
80 class ObstacleWorld : public b2ContactListener, b2DestructionListener, b2ContactFilter {
81 protected:
83  b2World* _world;
85  bool _lockstep;
87  float _stepssize;
94 
96  std::vector<std::shared_ptr<Obstacle>> _objects;
97 
100 
102  bool _collide;
104  bool _filters;
106  bool _destroy;
107 
108 
109 #pragma mark -
110 #pragma mark Constructors
111 public:
120  ObstacleWorld();
121 
126 
134  void dispose();
135 
149  bool init(const Rect& bounds);
150 
163  bool init(const Rect& bounds, const Vec2& gravity);
164 
165 
166 #pragma mark -
167 #pragma mark Static Constructors
168 
181  static std::shared_ptr<ObstacleWorld> alloc(const Rect& bounds) {
182  std::shared_ptr<ObstacleWorld> result = std::make_shared<ObstacleWorld>();
183  return (result->init(bounds) ? result : nullptr);
184  }
185 
198  static std::shared_ptr<ObstacleWorld> alloc(const Rect& bounds, const Vec2& gravity) {
199  std::shared_ptr<ObstacleWorld> result = std::make_shared<ObstacleWorld>();
200  return (result->init(bounds,gravity) ? result : nullptr);
201  }
202 
203 
204 #pragma mark -
205 #pragma mark Physics Handling
206 
219  b2World* getWorld() { return _world; }
220 
228  bool isLockStep() const { return _lockstep; }
229 
238  void setLockStep(bool flag) { _lockstep = flag; }
239 
247  float getStepsize() const { return _stepssize; }
248 
257  void setStepsize(float step) { _stepssize = step; }
258 
264  int getVelocityIterations() const { return _itvelocity; }
265 
273  void setVelocityIterations(int velocity) { _itvelocity = velocity; }
274 
280  int getPositionIterations() const { return _itposition; }
281 
289  void setPositionIterations(int position) { _itposition = position; }
290 
296  const Vec2& getGravity() const { return _gravity; }
297 
305  void setGravity(const Vec2& gravity);
306 
321  void update(float dt);
322 
328  const Rect& getBounds() const { return _bounds; }
329 
339  bool inBounds(Obstacle* obj);
340 
341 
342 #pragma mark -
343 #pragma mark Object Management
344 
349  const std::vector<std::shared_ptr<Obstacle>>& getObstacles() { return _objects; }
350 
364  void addObstacle(const std::shared_ptr<Obstacle>& obj);
365 
381  void removeObstacle(Obstacle* obj);
382 
395  void garbageCollect();
396 
403  void clear();
404 
405 
406 #pragma mark -
407 #pragma mark Collision Callback Functions
408 
416  void activateCollisionCallbacks(bool flag);
417 
426  bool enabledCollisionCallbacks() const { return _collide; }
427 
436  std::function<void(b2Contact* contact)> onBeginContact;
437 
446  std::function<void(b2Contact* contact)> onEndContact;
447 
469  std::function<void(b2Contact* contact, const b2Manifold* oldManifold)> beforeSolve;
470 
488  std::function<void(b2Contact* contact, const b2ContactImpulse* impulse)> afterSolve;
489 
498  void BeginContact(b2Contact* contact) override {
499  if (onBeginContact != nullptr) {
500  onBeginContact(contact);
501  }
502  }
503 
512  void EndContact(b2Contact* contact) override {
513  if (onEndContact != nullptr) {
514  onEndContact(contact);
515  }
516  }
517 
539  void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override {
540  if (beforeSolve != nullptr) {
541  beforeSolve(contact,oldManifold);
542  }
543  }
544 
562  void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) override {
563  if (afterSolve != nullptr) {
564  afterSolve(contact,impulse);
565  }
566  }
567 
568 
569 #pragma mark -
570 #pragma mark Filter Callback Functions
571 
580  void activateFilterCallbacks(bool flag);
581 
591  bool enabledFilterCallbacks() const { return _filters; }
592 
603  std::function<bool(b2Fixture* fixtureA, b2Fixture* fixtureB)> shouldCollide;
604 
615  bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB) override {
616  if (shouldCollide != nullptr) {
617  return shouldCollide(fixtureA,fixtureB);
618  }
619  return false;
620  }
621 
622 
623 #pragma mark -
624 #pragma mark Destruction Callback Functions
625 
633  void activateDestructionCallbacks(bool flag);
634 
644  bool enabledDestructionCallbacks() const { return _destroy; }
645 
654  std::function<void(b2Fixture* fixture)> destroyFixture;
655 
664  std::function<void(b2Joint* joint)> destroyJoint;
665 
674  void SayGoodbye(b2Joint* joint) override {
675  if (destroyJoint != nullptr) {
676  destroyJoint(joint);
677  }
678  }
679 
688  void SayGoodbye(b2Fixture* fixture) override {
689  if (destroyFixture != nullptr) {
690  destroyFixture(fixture);
691  }
692  }
693 
694 
695 #pragma mark -
696 #pragma mark Query Functions
697 
705  void queryAABB(std::function<bool(b2Fixture* fixture)> callback, const Rect& aabb) const;
706 
717  void rayCast(std::function<float(b2Fixture* fixture, const Vec2& point,
718  const Vec2& normal, float fraction)> callback,
719  const Vec2& point1, const Vec2& point2) const;
720 
721 };
722 
723 }
724 #endif /* __CU_PHYSICS_WORLD_H__ */
void setPositionIterations(int position)
Definition: CUObstacleWorld.h:289
std::function< void(b2Fixture *fixture)> destroyFixture
Definition: CUObstacleWorld.h:654
bool ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB) override
Definition: CUObstacleWorld.h:615
b2World * getWorld()
Definition: CUObstacleWorld.h:219
void removeObstacle(Obstacle *obj)
const std::vector< std::shared_ptr< Obstacle > > & getObstacles()
Definition: CUObstacleWorld.h:349
bool enabledFilterCallbacks() const
Definition: CUObstacleWorld.h:591
std::function< void(b2Contact *contact, const b2ContactImpulse *impulse)> afterSolve
Definition: CUObstacleWorld.h:488
Definition: CUVec2.h:61
void activateDestructionCallbacks(bool flag)
const Rect & getBounds() const
Definition: CUObstacleWorld.h:328
float getStepsize() const
Definition: CUObstacleWorld.h:247
float _stepssize
Definition: CUObstacleWorld.h:87
bool _filters
Definition: CUObstacleWorld.h:104
bool isLockStep() const
Definition: CUObstacleWorld.h:228
bool _lockstep
Definition: CUObstacleWorld.h:85
int getVelocityIterations() const
Definition: CUObstacleWorld.h:264
std::function< bool(b2Fixture *fixtureA, b2Fixture *fixtureB)> shouldCollide
Definition: CUObstacleWorld.h:603
void EndContact(b2Contact *contact) override
Definition: CUObstacleWorld.h:512
void rayCast(std::function< float(b2Fixture *fixture, const Vec2 &point, const Vec2 &normal, float fraction)> callback, const Vec2 &point1, const Vec2 &point2) const
std::function< void(b2Contact *contact, const b2Manifold *oldManifold)> beforeSolve
Definition: CUObstacleWorld.h:469
std::function< void(b2Joint *joint)> destroyJoint
Definition: CUObstacleWorld.h:664
Rect _bounds
Definition: CUObstacleWorld.h:99
bool inBounds(Obstacle *obj)
static std::shared_ptr< ObstacleWorld > alloc(const Rect &bounds, const Vec2 &gravity)
Definition: CUObstacleWorld.h:198
void addObstacle(const std::shared_ptr< Obstacle > &obj)
void setLockStep(bool flag)
Definition: CUObstacleWorld.h:238
~ObstacleWorld()
Definition: CUObstacleWorld.h:125
int getPositionIterations() const
Definition: CUObstacleWorld.h:280
void activateFilterCallbacks(bool flag)
Vec2 _gravity
Definition: CUObstacleWorld.h:93
b2World * _world
Definition: CUObstacleWorld.h:83
std::function< void(b2Contact *contact)> onEndContact
Definition: CUObstacleWorld.h:446
void setVelocityIterations(int velocity)
Definition: CUObstacleWorld.h:273
void setGravity(const Vec2 &gravity)
void setStepsize(float step)
Definition: CUObstacleWorld.h:257
int _itvelocity
Definition: CUObstacleWorld.h:89
bool enabledDestructionCallbacks() const
Definition: CUObstacleWorld.h:644
void update(float dt)
Definition: CURect.h:45
bool init(const Rect &bounds)
void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition: CUObstacleWorld.h:562
void activateCollisionCallbacks(bool flag)
std::function< void(b2Contact *contact)> onBeginContact
Definition: CUObstacleWorld.h:436
void SayGoodbye(b2Fixture *fixture) override
Definition: CUObstacleWorld.h:688
Definition: CUObstacle.h:76
void queryAABB(std::function< bool(b2Fixture *fixture)> callback, const Rect &aabb) const
static std::shared_ptr< ObstacleWorld > alloc(const Rect &bounds)
Definition: CUObstacleWorld.h:181
bool _collide
Definition: CUObstacleWorld.h:102
const Vec2 & getGravity() const
Definition: CUObstacleWorld.h:296
int _itposition
Definition: CUObstacleWorld.h:91
bool enabledCollisionCallbacks() const
Definition: CUObstacleWorld.h:426
Definition: CUAnimationNode.h:52
std::vector< std::shared_ptr< Obstacle > > _objects
Definition: CUObstacleWorld.h:96
bool _destroy
Definition: CUObstacleWorld.h:106
void SayGoodbye(b2Joint *joint) override
Definition: CUObstacleWorld.h:674
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: CUObstacleWorld.h:539
Definition: CUObstacleWorld.h:80
void BeginContact(b2Contact *contact) override
Definition: CUObstacleWorld.h:498