CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUObstacle.h
1 //
2 // CUObstacle.h
3 // Cornell University Game Library (CUGL)
4 //
5 // Box2D is an excellent physics engine in how it decouples collision and
6 // geometry from rigid body dynamics. However, there are some times in which
7 // coupling is okay for convenience reasons (particularly when we have the
8 // option to uncouple). This module is such an example; it couples the
9 // bodies and fixtures from Box2d into a single class, making the physics
10 // easier to use (in most cases).
11 //
12 // This class uses our standard shared-pointer architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // CUGL zlib License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
43 //
44 // Author: Walker White
45 // Version: 11/6/16
46 //
47 #ifndef __CU_OBSTACLE_H__
48 #define __CU_OBSTACLE_H__
49 
50 #include <Box2D/Dynamics/b2Body.h>
51 #include <Box2D/Dynamics/b2Fixture.h>
52 #include <iostream>
53 #include <cugl/2d/CUWireNode.h>
54 
55 namespace cugl {
56 
57 #pragma mark -
58 #pragma mark Obstacle
59 
76 class Obstacle {
77 protected:
79  b2BodyDef _bodyinfo;
81  b2FixtureDef _fixture;
83  b2MassData _massdata;
86 
88  std::shared_ptr<Node> _scene;
90  std::shared_ptr<WireNode> _debug;
94  std::string _tag;
95 
97  std::function<void(Obstacle* obstacle)> _listener;
98 
99 #pragma mark -
100 #pragma mark Scene Graph Internals
101 
108  virtual void resetDebug() { }
109 
117  virtual void updateDebug();
118 
119 
120 private:
122 
123  bool _remove;
125  bool _dirty;
126 
127 
128 #pragma mark -
129 #pragma mark Constructors
130 public:
138  Obstacle(void);
139 
149  virtual ~Obstacle();
150 
156  virtual bool init() { return init(Vec2::ZERO); }
157 
165  virtual bool init(const Vec2& vec);
166 
167 
168 #pragma mark -
169 #pragma mark BodyDef Methods
170 
180  virtual b2BodyType getBodyType() const { return _bodyinfo.type; }
181 
192  virtual void setBodyType(b2BodyType value) { _bodyinfo.type = value; }
193 
203  virtual Vec2 getPosition() const { return Vec2(_bodyinfo.position.x,_bodyinfo.position.y); }
204 
213  virtual void setPosition(const Vec2& value) { setPosition(value.x,value.y); }
214 
221  virtual void setPosition(float x, float y) { _bodyinfo.position.Set(x,y); }
222 
228  virtual float getX() const { return _bodyinfo.position.x; }
229 
235  virtual void setX(float value) { _bodyinfo.position.x = value; }
236 
242  virtual float getY() const { return _bodyinfo.position.y; }
243 
249  virtual void setY(float value) { _bodyinfo.position.y = value; }
250 
258  virtual float getAngle() const { return _bodyinfo.angle; }
259 
265  virtual void setAngle(float value) { _bodyinfo.angle = value; }
266 
276  virtual Vec2 getLinearVelocity() const {
277  return Vec2(_bodyinfo.linearVelocity.x,_bodyinfo.linearVelocity.y);
278  }
279 
288  virtual void setLinearVelocity(const Vec2& value) { setLinearVelocity(value.x,value.y); }
289 
296  virtual void setLinearVelocity(float x, float y) { _bodyinfo.linearVelocity.Set(x,y); }
297 
303  virtual float getVX() const { return _bodyinfo.linearVelocity.x; }
304 
310  virtual void setVX(float value) { _bodyinfo.linearVelocity.x = value; }
311 
317  virtual float getVY() const { return _bodyinfo.linearVelocity.y; }
318 
324  virtual void setVY(float value) { _bodyinfo.linearVelocity.y = value; }
325 
333  virtual float getAngularVelocity() const { return _bodyinfo.angularVelocity; }
334 
340  virtual void setAngularVelocity(float value) { _bodyinfo.angularVelocity = value; }
341 
352  virtual bool isActive() const { return _bodyinfo.active; }
353 
364  virtual void setActive(bool value) { _bodyinfo.active = value; }
365 
377  virtual bool isAwake() const { return _bodyinfo.awake; }
378 
390  virtual void setAwake(bool value) { _bodyinfo.awake = value; }
391 
403  virtual bool isSleepingAllowed() const { return _bodyinfo.allowSleep; }
404 
416  virtual void setSleepingAllowed(bool value) { _bodyinfo.allowSleep = value; }
417 
434  virtual bool isBullet() const { return _bodyinfo.bullet; }
435 
452  virtual void setBullet(bool value) { _bodyinfo.bullet = value; }
453 
461  virtual bool isFixedRotation() const { return _bodyinfo.fixedRotation; }
462 
470  virtual void setFixedRotation(bool value) { _bodyinfo.fixedRotation = value; }
471 
480  virtual float getGravityScale() const { return _bodyinfo.gravityScale; }
481 
490  virtual void setGravityScale(float value) { _bodyinfo.gravityScale = value; }
491 
506  virtual float getLinearDamping() const { return _bodyinfo.linearDamping; }
507 
522  virtual void setLinearDamping(float value) { _bodyinfo.linearDamping = value; }
523 
538  virtual float getAngularDamping() const { return _bodyinfo.angularDamping; }
539 
554  virtual void setAngularDamping(float value) { _bodyinfo.angularDamping = value; }
555 
562  void setBodyState(const b2Body& body);
563 
564 
565 #pragma mark -
566 #pragma mark FixtureDef Methods
567 
576  float getDensity() const { return _fixture.density; }
577 
587  virtual void setDensity(float value) { _fixture.density = value; }
588 
600  float getFriction() const { return _fixture.friction; }
601 
613  virtual void setFriction(float value) { _fixture.friction = value; }
614 
626  float getRestitution() const { return _fixture.restitution; }
627 
639  virtual void setRestitution(float value) { _fixture.restitution = value; }
640 
650  bool isSensor() const { return _fixture.isSensor; }
651 
661  virtual void setSensor(bool value) { _fixture.isSensor = value; }
662 
675  b2Filter getFilterData() const { return _fixture.filter; }
676 
691  virtual void setFilterData(b2Filter value) { _fixture.filter = value; }
692 
693 
694 #pragma mark -
695 #pragma mark MassData Methods
696 
705  virtual Vec2 getCentroid() const { return Vec2(_massdata.center.x,_massdata.center.y); }
706 
715  virtual void setCentroid(const Vec2& value) { setCentroid(value.x,value.y); }
716 
723  virtual void setCentroid(float x, float y);
724 
733  virtual float getInertia() const { return _massdata.I; }
734 
743  virtual void setInertia(float value);
744 
752  virtual float getMass() const { return _massdata.mass; }
753 
761  virtual void setMass(float value);
762 
766  virtual void resetMass() { _masseffect = false; }
767 
768 
769 #pragma mark -
770 #pragma mark Garbage Collection
771 
779  bool isRemoved() const { return _remove; }
780 
789  void markRemoved(bool value) { _remove = value; }
790 
800  bool isDirty() const { return _dirty; }
801 
811  void markDirty(bool value) { _dirty = value; }
812 
813 #pragma mark -
814 #pragma mark Physics Methods
815 
824  virtual b2Body* getBody() { return nullptr; }
825 
836  virtual bool activatePhysics(b2World& world) { return false; }
837 
845  virtual void deactivatePhysics(b2World& world) {}
846 
847 
848 #pragma mark -
849 #pragma mark Update Methods
850 
863  virtual void update(float delta) {
864  if (_scene) { updateDebug(); }
865  if (_listener) { _listener(this); }
866  }
867 
879  const std::function<void(Obstacle* obstacle)>& getListener() const {
880  return _listener;
881  }
882 
894  void setListener(const std::function<void(Obstacle* obstacle)>& listener) {
895  _listener = listener;
896  }
897 
898 #pragma mark -
899 #pragma mark Debugging Methods
900 
908  std::string getName() const { return _tag; }
909 
918  void setName(std::string value) { _tag = value; }
919 
929  std::string toString() const;
930 
942  friend std::ostream& operator<<(std::ostream& os, const Obstacle& obj);
943 
944 
945 #pragma mark -
946 #pragma mark Scene Graph Methods
947 
955  Color4 getDebugColor() const { return _dcolor; }
956 
965  virtual void setDebugColor(Color4 color);
966 
987  Node* getDebugScene() const { return _scene.get(); }
988 
1008  WireNode* getDebugNode() const { return _debug.get(); }
1009 
1031  virtual void setDebugScene(const std::shared_ptr<Node>& node);
1032 
1041  bool hasDebug() { return _scene != nullptr; }
1042 };
1043 
1044 }
1045 
1046 #endif /* __CU_OBSTACLE_H__ */
Definition: CUWireNode.h:107
virtual void setFixedRotation(bool value)
Definition: CUObstacle.h:470
virtual ~Obstacle()
virtual float getY() const
Definition: CUObstacle.h:242
float x
Definition: CUVec2.h:66
virtual bool isSleepingAllowed() const
Definition: CUObstacle.h:403
float y
Definition: CUVec2.h:68
void setListener(const std::function< void(Obstacle *obstacle)> &listener)
Definition: CUObstacle.h:894
std::shared_ptr< Node > _scene
Definition: CUObstacle.h:88
virtual void setAngle(float value)
Definition: CUObstacle.h:265
void setName(std::string value)
Definition: CUObstacle.h:918
virtual void setAngularVelocity(float value)
Definition: CUObstacle.h:340
virtual b2Body * getBody()
Definition: CUObstacle.h:824
virtual float getMass() const
Definition: CUObstacle.h:752
virtual float getX() const
Definition: CUObstacle.h:228
virtual float getInertia() const
Definition: CUObstacle.h:733
Definition: CUVec2.h:61
virtual void setAngularDamping(float value)
Definition: CUObstacle.h:554
virtual void setBodyType(b2BodyType value)
Definition: CUObstacle.h:192
void markDirty(bool value)
Definition: CUObstacle.h:811
virtual float getGravityScale() const
Definition: CUObstacle.h:480
b2MassData _massdata
Definition: CUObstacle.h:83
virtual float getVY() const
Definition: CUObstacle.h:317
virtual bool activatePhysics(b2World &world)
Definition: CUObstacle.h:836
virtual bool isBullet() const
Definition: CUObstacle.h:434
virtual bool isActive() const
Definition: CUObstacle.h:352
virtual void setPosition(float x, float y)
Definition: CUObstacle.h:221
virtual float getVX() const
Definition: CUObstacle.h:303
Definition: CUNode.h:92
virtual void setVY(float value)
Definition: CUObstacle.h:324
virtual void setDebugScene(const std::shared_ptr< Node > &node)
virtual void setActive(bool value)
Definition: CUObstacle.h:364
virtual float getAngle() const
Definition: CUObstacle.h:258
Color4 getDebugColor() const
Definition: CUObstacle.h:955
virtual void setFilterData(b2Filter value)
Definition: CUObstacle.h:691
std::shared_ptr< WireNode > _debug
Definition: CUObstacle.h:90
virtual void setSleepingAllowed(bool value)
Definition: CUObstacle.h:416
virtual void setCentroid(const Vec2 &value)
Definition: CUObstacle.h:715
virtual void setDensity(float value)
Definition: CUObstacle.h:587
virtual void setSensor(bool value)
Definition: CUObstacle.h:661
virtual void updateDebug()
virtual void setDebugColor(Color4 color)
std::string _tag
Definition: CUObstacle.h:94
float getFriction() const
Definition: CUObstacle.h:600
b2Filter getFilterData() const
Definition: CUObstacle.h:675
Node * getDebugScene() const
Definition: CUObstacle.h:987
void markRemoved(bool value)
Definition: CUObstacle.h:789
virtual void setY(float value)
Definition: CUObstacle.h:249
friend std::ostream & operator<<(std::ostream &os, const Obstacle &obj)
const std::function< void(Obstacle *obstacle)> & getListener() const
Definition: CUObstacle.h:879
b2BodyDef _bodyinfo
Definition: CUObstacle.h:79
virtual void setFriction(float value)
Definition: CUObstacle.h:613
virtual void setMass(float value)
bool isDirty() const
Definition: CUObstacle.h:800
Color4 _dcolor
Definition: CUObstacle.h:92
bool hasDebug()
Definition: CUObstacle.h:1041
virtual void setRestitution(float value)
Definition: CUObstacle.h:639
void setBodyState(const b2Body &body)
float getRestitution() const
Definition: CUObstacle.h:626
float getDensity() const
Definition: CUObstacle.h:576
virtual void setBullet(bool value)
Definition: CUObstacle.h:452
bool _masseffect
Definition: CUObstacle.h:85
virtual void setGravityScale(float value)
Definition: CUObstacle.h:490
std::function< void(Obstacle *obstacle)> _listener
Definition: CUObstacle.h:97
bool isSensor() const
Definition: CUObstacle.h:650
b2FixtureDef _fixture
Definition: CUObstacle.h:81
virtual Vec2 getCentroid() const
Definition: CUObstacle.h:705
virtual void setVX(float value)
Definition: CUObstacle.h:310
virtual float getAngularDamping() const
Definition: CUObstacle.h:538
virtual void setLinearVelocity(float x, float y)
Definition: CUObstacle.h:296
bool isRemoved() const
Definition: CUObstacle.h:779
std::string getName() const
Definition: CUObstacle.h:908
WireNode * getDebugNode() const
Definition: CUObstacle.h:1008
virtual float getAngularVelocity() const
Definition: CUObstacle.h:333
Definition: CUObstacle.h:76
virtual void setLinearVelocity(const Vec2 &value)
Definition: CUObstacle.h:288
virtual void setPosition(const Vec2 &value)
Definition: CUObstacle.h:213
virtual bool isAwake() const
Definition: CUObstacle.h:377
virtual void setX(float value)
Definition: CUObstacle.h:235
virtual void resetDebug()
Definition: CUObstacle.h:108
virtual Vec2 getLinearVelocity() const
Definition: CUObstacle.h:276
virtual float getLinearDamping() const
Definition: CUObstacle.h:506
Definition: CUColor4.h:1104
virtual void setAwake(bool value)
Definition: CUObstacle.h:390
std::string toString() const
Definition: CUAnimationNode.h:52
static const Vec2 ZERO
Definition: CUVec2.h:71
virtual bool init()
Definition: CUObstacle.h:156
virtual bool isFixedRotation() const
Definition: CUObstacle.h:461
virtual Vec2 getPosition() const
Definition: CUObstacle.h:203
virtual void resetMass()
Definition: CUObstacle.h:766
virtual void setInertia(float value)
virtual b2BodyType getBodyType() const
Definition: CUObstacle.h:180
virtual void update(float delta)
Definition: CUObstacle.h:863
virtual void deactivatePhysics(b2World &world)
Definition: CUObstacle.h:845
virtual void setLinearDamping(float value)
Definition: CUObstacle.h:522