CUGL 1.3
Cornell University Game Library
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 MIT 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__ */
cugl::ObstacleWorld::enabledDestructionCallbacks
bool enabledDestructionCallbacks() const
Definition: CUObstacleWorld.h:644
cugl::ObstacleWorld::isLockStep
bool isLockStep() const
Definition: CUObstacleWorld.h:228
cugl::ObstacleWorld::dispose
void dispose()
cugl::ObstacleWorld::_gravity
Vec2 _gravity
Definition: CUObstacleWorld.h:93
cugl::ObstacleWorld::setVelocityIterations
void setVelocityIterations(int velocity)
Definition: CUObstacleWorld.h:273
cugl::ObstacleWorld::afterSolve
std::function< void(b2Contact *contact, const b2ContactImpulse *impulse)> afterSolve
Definition: CUObstacleWorld.h:488
cugl::ObstacleWorld::_lockstep
bool _lockstep
Definition: CUObstacleWorld.h:85
cugl::ObstacleWorld::onBeginContact
std::function< void(b2Contact *contact)> onBeginContact
Definition: CUObstacleWorld.h:436
cugl::ObstacleWorld::setGravity
void setGravity(const Vec2 &gravity)
cugl::ObstacleWorld::getWorld
b2World * getWorld()
Definition: CUObstacleWorld.h:219
cugl::ObstacleWorld::setLockStep
void setLockStep(bool flag)
Definition: CUObstacleWorld.h:238
cugl::ObstacleWorld::clear
void clear()
cugl::ObstacleWorld::PreSolve
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: CUObstacleWorld.h:539
cugl::ObstacleWorld::~ObstacleWorld
~ObstacleWorld()
Definition: CUObstacleWorld.h:125
cugl::ObstacleWorld::update
void update(float dt)
cugl::ObstacleWorld::getGravity
const Vec2 & getGravity() const
Definition: CUObstacleWorld.h:296
cugl::ObstacleWorld::alloc
static std::shared_ptr< ObstacleWorld > alloc(const Rect &bounds, const Vec2 &gravity)
Definition: CUObstacleWorld.h:198
cugl::ObstacleWorld::addObstacle
void addObstacle(const std::shared_ptr< Obstacle > &obj)
cugl::ObstacleWorld::SayGoodbye
void SayGoodbye(b2Fixture *fixture) override
Definition: CUObstacleWorld.h:688
cugl::ObstacleWorld::shouldCollide
std::function< bool(b2Fixture *fixtureA, b2Fixture *fixtureB)> shouldCollide
Definition: CUObstacleWorld.h:603
cugl::ObstacleWorld::enabledFilterCallbacks
bool enabledFilterCallbacks() const
Definition: CUObstacleWorld.h:591
cugl::ObstacleWorld::_objects
std::vector< std::shared_ptr< Obstacle > > _objects
Definition: CUObstacleWorld.h:96
cugl::ObstacleWorld::activateFilterCallbacks
void activateFilterCallbacks(bool flag)
cugl::ObstacleWorld::setPositionIterations
void setPositionIterations(int position)
Definition: CUObstacleWorld.h:289
cugl::ObstacleWorld::alloc
static std::shared_ptr< ObstacleWorld > alloc(const Rect &bounds)
Definition: CUObstacleWorld.h:181
cugl::ObstacleWorld::removeObstacle
void removeObstacle(Obstacle *obj)
cugl::ObstacleWorld::EndContact
void EndContact(b2Contact *contact) override
Definition: CUObstacleWorld.h:512
cugl::Rect
Definition: CURect.h:45
cugl::ObstacleWorld::SayGoodbye
void SayGoodbye(b2Joint *joint) override
Definition: CUObstacleWorld.h:674
cugl::ObstacleWorld::garbageCollect
void garbageCollect()
cugl::ObstacleWorld::onEndContact
std::function< void(b2Contact *contact)> onEndContact
Definition: CUObstacleWorld.h:446
cugl::ObstacleWorld
Definition: CUObstacleWorld.h:80
cugl::ObstacleWorld::_itposition
int _itposition
Definition: CUObstacleWorld.h:91
cugl::ObstacleWorld::enabledCollisionCallbacks
bool enabledCollisionCallbacks() const
Definition: CUObstacleWorld.h:426
cugl::ObstacleWorld::getBounds
const Rect & getBounds() const
Definition: CUObstacleWorld.h:328
cugl::ObstacleWorld::ObstacleWorld
ObstacleWorld()
cugl::ObstacleWorld::getPositionIterations
int getPositionIterations() const
Definition: CUObstacleWorld.h:280
cugl::ObstacleWorld::rayCast
void rayCast(std::function< float(b2Fixture *fixture, const Vec2 &point, const Vec2 &normal, float fraction)> callback, const Vec2 &point1, const Vec2 &point2) const
cugl::ObstacleWorld::_bounds
Rect _bounds
Definition: CUObstacleWorld.h:99
cugl::ObstacleWorld::_itvelocity
int _itvelocity
Definition: CUObstacleWorld.h:89
cugl::ObstacleWorld::inBounds
bool inBounds(Obstacle *obj)
cugl::Obstacle
Definition: CUObstacle.h:76
cugl::ObstacleWorld::destroyFixture
std::function< void(b2Fixture *fixture)> destroyFixture
Definition: CUObstacleWorld.h:654
cugl::Vec2
Definition: CUVec2.h:61
cugl::ObstacleWorld::getVelocityIterations
int getVelocityIterations() const
Definition: CUObstacleWorld.h:264
cugl::ObstacleWorld::init
bool init(const Rect &bounds)
cugl::ObstacleWorld::setStepsize
void setStepsize(float step)
Definition: CUObstacleWorld.h:257
cugl::ObstacleWorld::_world
b2World * _world
Definition: CUObstacleWorld.h:83
cugl::ObstacleWorld::getStepsize
float getStepsize() const
Definition: CUObstacleWorld.h:247
cugl::ObstacleWorld::activateCollisionCallbacks
void activateCollisionCallbacks(bool flag)
cugl::ObstacleWorld::queryAABB
void queryAABB(std::function< bool(b2Fixture *fixture)> callback, const Rect &aabb) const
cugl::ObstacleWorld::activateDestructionCallbacks
void activateDestructionCallbacks(bool flag)
cugl::ObstacleWorld::getObstacles
const std::vector< std::shared_ptr< Obstacle > > & getObstacles()
Definition: CUObstacleWorld.h:349
cugl::ObstacleWorld::ShouldCollide
bool ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB) override
Definition: CUObstacleWorld.h:615
cugl::ObstacleWorld::beforeSolve
std::function< void(b2Contact *contact, const b2Manifold *oldManifold)> beforeSolve
Definition: CUObstacleWorld.h:469
cugl::ObstacleWorld::_stepssize
float _stepssize
Definition: CUObstacleWorld.h:87
cugl::ObstacleWorld::destroyJoint
std::function< void(b2Joint *joint)> destroyJoint
Definition: CUObstacleWorld.h:664
cugl::ObstacleWorld::_destroy
bool _destroy
Definition: CUObstacleWorld.h:106
cugl::ObstacleWorld::PostSolve
void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition: CUObstacleWorld.h:562
cugl::ObstacleWorld::_collide
bool _collide
Definition: CUObstacleWorld.h:102
cugl::ObstacleWorld::BeginContact
void BeginContact(b2Contact *contact) override
Definition: CUObstacleWorld.h:498
cugl::ObstacleWorld::_filters
bool _filters
Definition: CUObstacleWorld.h:104