Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUCapsuleObstacle.h
1 //
2 // CUCapsuleObstacle.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This class implements a capsule physics object. A capsule is a box with semicircular
6 // ends along the major axis. They are a popular physics objects, particularly for
7 // character avatars. The rounded ends means they are less likely to snag, and they
8 // naturally fall off platforms when they go too far.
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_CAPSULE_OBSTACLE_H__
16 #define __CU_CAPSULE_OBSTACLE_H__
17 
18 #include <Box2D/Collision/Shapes/b2PolygonShape.h>
19 #include <Box2D/Collision/Shapes/b2CircleShape.h>
20 #include <Box2D/Collision/b2Collision.h>
21 #include "CUSimpleObstacle.h"
22 
23 
24 NS_CC_BEGIN
25 
26 #pragma mark -
27 #pragma mark Capsule Obstacle
28 
41 class CC_DLL CapsuleObstacle : public SimpleObstacle {
42 private:
44  CC_DISALLOW_COPY_AND_ASSIGN(CapsuleObstacle);
45 
46 public:
48  enum class Orientation {
50  TOP,
52  VERTICAL,
54  BOTTOM,
56  LEFT,
58  HORIZONTAL,
60  RIGHT
61  };
62 
63 protected:
65  b2PolygonShape _shape;
67  b2CircleShape _ends;
69  b2AABB _center;
70 
72  b2Fixture* _core;
74  b2Fixture* _cap1;
76  b2Fixture* _cap2;
78  Size _dimension;
81 
83  float _seamEpsilon;
84 
85 
86 #pragma mark -
87 #pragma mark Scene Graph Management
88 
95  bool resize(const Size& size);
96 
104  virtual void resetDebugNode() override;
105 
106 
107 public:
108 #pragma mark -
109 #pragma mark Static Constructors
110 
115  static CapsuleObstacle* create();
116 
129  static CapsuleObstacle* create(const Vec2& pos);
130 
148  static CapsuleObstacle* create(const Vec2& pos, const Size& size);
149 
168  static CapsuleObstacle* create(const Vec2& pos, const Size& size, Orientation orient);
169 
170 
171 #pragma mark -
172 #pragma mark Dimensions
173 
178  const Size& getDimension() const { return _dimension; }
179 
185  void setDimension(const Size& value) { resize(value); markDirty(true); }
186 
193  void setDimension(float width, float height) { setDimension(Size(width,height)); }
194 
200  float getWidth() const { return _dimension.width; }
201 
207  void setWidth(float value) { setDimension(value,_dimension.height); }
208 
214  float getHeight() const { return _dimension.height; }
215 
221  void setHeight(float value) { setDimension(_dimension.width,value); }
222 
228  const Orientation& getOrientation() const { return _orient; }
229 
240  bool setOrientation(Orientation value);
241 
242 
243 #pragma mark -
244 #pragma mark Physics Methods
245 
255  void setSeamOffset(float value) { _seamEpsilon = value; markDirty(true); }
256 
267  float getSeamOffset() const { return _seamEpsilon; }
268 
278  virtual void setDensity(float value) override;
279 
285  virtual void createFixtures() override;
286 
292  virtual void releaseFixtures() override;
293 
294 
295 #pragma mark -
296 #pragma mark Initializers
297 CC_CONSTRUCTOR_ACCESS:
298  /*
299  * Creates a new capsule object at the origin.
300  */
301  CapsuleObstacle(void) : SimpleObstacle(), _core(nullptr), _cap1(nullptr), _cap2(nullptr), _seamEpsilon(0.0f) { }
302 
308  virtual bool init() override { return init(Vec2::ZERO,Size::ZERO); }
309 
322  virtual bool init(const Vec2& pos) override { return init(pos,Size::ZERO); }
323 
341  virtual bool init(const Vec2& pos, const Size& size);
342 
360  virtual bool init(const Vec2& pos, const Size& size, Orientation orient);
361 
362 };
363 
364 NS_CC_END
365 #endif /* defined(__CU_CAPSULE_OBSTACLE_H__) */
void setDimension(float width, float height)
Definition: CUCapsuleObstacle.h:193
b2CircleShape _ends
Definition: CUCapsuleObstacle.h:67
float _seamEpsilon
Definition: CUCapsuleObstacle.h:83
const Orientation & getOrientation() const
Definition: CUCapsuleObstacle.h:228
b2Fixture * _cap1
Definition: CUCapsuleObstacle.h:74
const Size & getDimension() const
Definition: CUCapsuleObstacle.h:178
b2PolygonShape _shape
Definition: CUCapsuleObstacle.h:65
b2AABB _center
Definition: CUCapsuleObstacle.h:69
void setHeight(float value)
Definition: CUCapsuleObstacle.h:221
void setSeamOffset(float value)
Definition: CUCapsuleObstacle.h:255
void setDimension(const Size &value)
Definition: CUCapsuleObstacle.h:185
virtual bool init(const Vec2 &pos) override
Definition: CUCapsuleObstacle.h:322
float getWidth() const
Definition: CUCapsuleObstacle.h:200
void setWidth(float value)
Definition: CUCapsuleObstacle.h:207
virtual void createFixtures()
Definition: CUSimpleObstacle.h:792
b2Fixture * _core
Definition: CUCapsuleObstacle.h:72
float getHeight() const
Definition: CUCapsuleObstacle.h:214
virtual void releaseFixtures()
Definition: CUSimpleObstacle.h:799
Definition: CUSimpleObstacle.h:36
virtual void resetDebugNode()
Definition: CUObstacle.h:106
virtual bool init() override
Definition: CUCapsuleObstacle.h:308
virtual bool init()
Definition: CUObstacle.h:982
Size _dimension
Definition: CUCapsuleObstacle.h:78
void markDirty(bool value)
Definition: CUObstacle.h:758
Orientation
Definition: CUCapsuleObstacle.h:48
virtual void setDensity(float value) override
Definition: CUSimpleObstacle.cpp:33
float getSeamOffset() const
Definition: CUCapsuleObstacle.h:267
Orientation _orient
Definition: CUCapsuleObstacle.h:80
Definition: CUCapsuleObstacle.h:41
b2Fixture * _cap2
Definition: CUCapsuleObstacle.h:76