Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUPolygonObstacle.h
1 //
2 // CUPolygonObstacle.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This class implements a polygonal Physics object. This is different from PolygonNode,
6 // which is used for drawing. This class is substantially more complex than the other
7 // physics objects, but it will allow you to draw arbitrary shapes. Be careful modifying
8 // this file as there are a lot of subtleties here.
9 //
10 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
11 //
12 // Author: Walker White
13 // Version: 11/24/15
14 //
15 #ifndef __CU_POLYGON_OBSTACLE_H__
16 #define __CU_POLYGON_OBSTACLE_H__
17 
18 #include "CUSimpleObstacle.h"
19 #include "CUPoly2.h"
20 
21 NS_CC_BEGIN
22 
23 #pragma mark -
24 #pragma mark Polygon Obstacle
25 
32 class CC_DLL PolygonObstacle : public SimpleObstacle {
33 private:
35  CC_DISALLOW_COPY_AND_ASSIGN(PolygonObstacle);
36 
37 protected:
41  b2PolygonShape* _shapes;
43  b2Fixture** _geoms;
45  Vec2 _anchor;
47  int _fixCount;
48 
49 
50 #pragma mark -
51 #pragma mark Scene Graph Methods
52 
59  void resize(const Size& size);
60 
67  virtual void resetSceneNode() override;
68 
76  virtual void resetDebugNode() override;
77 
83  void resetShapes();
84 
85 
86 public:
87 #pragma mark -
88 #pragma mark Static Constructors
89 
99  static PolygonObstacle* create(const Poly2& poly);
100 
114  static PolygonObstacle* create(const Poly2& poly, const Vec2& anchor);
115 
116 
117 #pragma mark -
118 #pragma mark Dimensions
119 
124  const Size& getSize() const { return _polygon.getBounds().size; }
125 
135  void setSize(const Size& value) { resize(value); markDirty(true); }
136 
143  void setSize(float width, float height) { setSize(Size(width, height)); }
144 
150  float getWidth() const { return _polygon.getBounds().size.width; }
151 
161  void setWidth(float value) { setSize(value,getHeight()); }
162 
168  float getHeight() const { return _polygon.getBounds().size.height; }
169 
179  void setHeight(float value) { setSize(getWidth(),value); }
180 
191  const Vec2& getAnchor() const { return _anchor; }
192 
203  void setAnchor(const Vec2& value) { setAnchor(value.x, value.y); }
204 
216  void setAnchor(float x, float y);
217 
223  const Poly2& getPolygon() const { return _polygon; }
224 
233  void setPolygon(const Poly2&);
234 
235 
236 #pragma mark -
237 #pragma mark Physics Methods
238 
244  virtual void createFixtures() override;
245 
251  virtual void releaseFixtures() override;
252 
253 
254 #pragma mark -
255 #pragma mark Initializers
256 CC_CONSTRUCTOR_ACCESS:
262  PolygonObstacle(void) : SimpleObstacle(), _shapes(nullptr), _geoms(nullptr) { }
263 
270  virtual ~PolygonObstacle();
271 
272  // Turn off init warnings
273  using SimpleObstacle::init;
274 
285  virtual bool init(const Poly2& poly) { return init(poly,Vec2(0.5,0.5)); }
286 
300  virtual bool init(const Poly2& poly, const Vec2& anchor);
301 
302 };
303 
304 NS_CC_END
305 #endif /* defined(__CU_POLYGON_OBSTACLE_H__) */
b2PolygonShape * _shapes
Definition: CUPolygonObstacle.h:41
const Rect & getBounds() const
Definition: CUPoly2.h:574
virtual bool init(const Poly2 &poly)
Definition: CUPolygonObstacle.h:285
virtual void resetSceneNode()
Definition: CUObstacle.h:79
void setWidth(float value)
Definition: CUPolygonObstacle.h:161
Poly2 _polygon
Definition: CUPolygonObstacle.h:39
const Poly2 & getPolygon() const
Definition: CUPolygonObstacle.h:223
Vec2 _anchor
Definition: CUPolygonObstacle.h:45
void setSize(const Size &value)
Definition: CUPolygonObstacle.h:135
const Vec2 & getAnchor() const
Definition: CUPolygonObstacle.h:191
virtual void createFixtures()
Definition: CUSimpleObstacle.h:792
b2Fixture ** _geoms
Definition: CUPolygonObstacle.h:43
const Size & getSize() const
Definition: CUPolygonObstacle.h:124
void setHeight(float value)
Definition: CUPolygonObstacle.h:179
virtual void releaseFixtures()
Definition: CUSimpleObstacle.h:799
Definition: CUSimpleObstacle.h:36
virtual void resetDebugNode()
Definition: CUObstacle.h:106
virtual bool init()
Definition: CUObstacle.h:982
void markDirty(bool value)
Definition: CUObstacle.h:758
Definition: CUPoly2.h:53
float getHeight() const
Definition: CUPolygonObstacle.h:168
void setAnchor(const Vec2 &value)
Definition: CUPolygonObstacle.h:203
Definition: CUPolygonObstacle.h:32
float getWidth() const
Definition: CUPolygonObstacle.h:150
int _fixCount
Definition: CUPolygonObstacle.h:47
void setSize(float width, float height)
Definition: CUPolygonObstacle.h:143