CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs 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
6 // semicircular ends along the major axis. They are a popular physics objects,
7 // particularly for character avatars. The rounded ends means they are less
8 // likely to snag, and they naturally fall off platforms when they go too far.
9 //
10 // This class uses our standard shared-pointer architecture.
11 //
12 // 1. The constructor does not perform any initialization; it just sets all
13 // attributes to their defaults.
14 //
15 // 2. All initialization takes place via init methods, which can fail if an
16 // object is initialized more than once.
17 //
18 // 3. All allocation takes place via static constructors which return a shared
19 // pointer.
20 //
21 // CUGL zlib License:
22 // This software is provided 'as-is', without any express or implied
23 // warranty. In no event will the authors be held liable for any damages
24 // arising from the use of this software.
25 //
26 // Permission is granted to anyone to use this software for any purpose,
27 // including commercial applications, and to alter it and redistribute it
28 // freely, subject to the following restrictions:
29 //
30 // 1. The origin of this software must not be misrepresented; you must not
31 // claim that you wrote the original software. If you use this software
32 // in a product, an acknowledgment in the product documentation would be
33 // appreciated but is not required.
34 //
35 // 2. Altered source versions must be plainly marked as such, and must not
36 // be misrepresented as being the original software.
37 //
38 // 3. This notice may not be removed or altered from any source distribution.
39 //
40 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
41 //
42 // Author: Walker White
43 // Version: 11/6/16
44 //
45 #ifndef __CU_CAPSULE_OBSTACLE_H__
46 #define __CU_CAPSULE_OBSTACLE_H__
47 
48 #include <Box2D/Collision/Shapes/b2PolygonShape.h>
49 #include <Box2D/Collision/Shapes/b2CircleShape.h>
50 #include <Box2D/Collision/b2Collision.h>
51 #include "CUSimpleObstacle.h"
52 
53 namespace cugl {
54 
55 #pragma mark -
56 #pragma mark Capsule Obstacle
57 
73 public:
75  enum class Orientation {
77  TOP,
79  VERTICAL,
81  BOTTOM,
83  LEFT,
85  HORIZONTAL,
87  RIGHT
88  };
89 
90 protected:
92  b2PolygonShape _shape;
94  b2CircleShape _ends;
96  b2AABB _center;
97 
99  b2Fixture* _core;
101  b2Fixture* _cap1;
103  b2Fixture* _cap2;
108 
111 
112 
113 #pragma mark -
114 #pragma mark Scene Graph Methods
115 
123  void resize(const Size& size);
124 
132  virtual void resetDebug() override;
133 
134 
135 #pragma mark -
136 #pragma mark Constructors
137 public:
145  _core(nullptr), _cap1(nullptr), _cap2(nullptr), _seamEpsilon(0.0f) { }
146 
156  virtual ~CapsuleObstacle() {
157  CUAssertLog(_core == nullptr, "You must deactive physics before deleting an object");
158  }
159 
165  virtual bool init() override { return init(Vec2::ZERO,Size::ZERO); }
166 
179  virtual bool init(const Vec2& pos) override { return init(pos,Size::ZERO); }
180 
198  virtual bool init(const Vec2& pos, const Size& size);
199 
217  virtual bool init(const Vec2& pos, const Size& size, Orientation orient);
218 
219 #pragma mark -
220 #pragma mark Static Constructors
221 
226  static std::shared_ptr<CapsuleObstacle> alloc() {
227  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
228  return (result->init() ? result : nullptr);
229  }
230 
243  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos) {
244  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
245  return (result->init(pos) ? result : nullptr);
246  }
247 
265  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos, const Size& size) {
266  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
267  return (result->init(pos,size) ? result : nullptr);
268  }
269 
288  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos, const Size& size, Orientation orient) {
289  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
290  return (result->init(pos,size,orient) ? result : nullptr);
291  }
292 
293 
294 #pragma mark -
295 #pragma mark Dimensions
296 
301  const Size& getDimension() const { return _dimension; }
302 
308  void setDimension(const Size& value) { resize(value); markDirty(true); }
309 
316  void setDimension(float width, float height) { setDimension(Size(width,height)); }
317 
323  float getWidth() const { return _dimension.width; }
324 
330  void setWidth(float value) { setDimension(value,_dimension.height); }
331 
337  float getHeight() const { return _dimension.height; }
338 
344  void setHeight(float value) { setDimension(_dimension.width,value); }
345 
351  const Orientation& getOrientation() const { return _orient; }
352 
363  bool setOrientation(Orientation value);
364 
372  static bool isHorizontal(Orientation value) {
373  return (value == Orientation::LEFT || value == Orientation::RIGHT || value == Orientation::HORIZONTAL);
374  }
375 
376 #pragma mark -
377 #pragma mark Physics Methods
378 
388  void setSeamOffset(float value);
389 
400  float getSeamOffset() const { return _seamEpsilon; }
401 
411  virtual void setDensity(float value) override;
412 
418  virtual void createFixtures() override;
419 
425  virtual void releaseFixtures() override;
426 
427 };
428 
429 }
430 #endif /* __CU_CAPSULE_OBSTACLE_H__ */
Definition: CUSize.h:57
Orientation _orient
Definition: CUCapsuleObstacle.h:107
virtual void releaseFixtures() override
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos, const Size &size, Orientation orient)
Definition: CUCapsuleObstacle.h:288
virtual void resetDebug() override
Definition: CUVec2.h:61
virtual ~CapsuleObstacle()
Definition: CUCapsuleObstacle.h:156
void markDirty(bool value)
Definition: CUObstacle.h:811
virtual void createFixtures() override
float getSeamOffset() const
Definition: CUCapsuleObstacle.h:400
b2Fixture * _core
Definition: CUCapsuleObstacle.h:99
b2CircleShape _ends
Definition: CUCapsuleObstacle.h:94
static bool isHorizontal(Orientation value)
Definition: CUCapsuleObstacle.h:372
CapsuleObstacle(void)
Definition: CUCapsuleObstacle.h:144
Orientation
Definition: CUCapsuleObstacle.h:75
b2PolygonShape _shape
Definition: CUCapsuleObstacle.h:92
Definition: CUCapsuleObstacle.h:72
static const Size ZERO
Definition: CUSize.h:66
void setSeamOffset(float value)
virtual bool init(const Vec2 &pos) override
Definition: CUCapsuleObstacle.h:179
Definition: CUSimpleObstacle.h:63
float width
Definition: CUSize.h:61
b2Fixture * _cap1
Definition: CUCapsuleObstacle.h:101
void setWidth(float value)
Definition: CUCapsuleObstacle.h:330
void resize(const Size &size)
void setDimension(const Size &value)
Definition: CUCapsuleObstacle.h:308
b2Fixture * _cap2
Definition: CUCapsuleObstacle.h:103
float height
Definition: CUSize.h:63
virtual void setDensity(float value) override
b2AABB _center
Definition: CUCapsuleObstacle.h:96
void setHeight(float value)
Definition: CUCapsuleObstacle.h:344
float getWidth() const
Definition: CUCapsuleObstacle.h:323
float getHeight() const
Definition: CUCapsuleObstacle.h:337
void setDimension(float width, float height)
Definition: CUCapsuleObstacle.h:316
const Size & getDimension() const
Definition: CUCapsuleObstacle.h:301
virtual bool init() override
Definition: CUCapsuleObstacle.h:165
bool setOrientation(Orientation value)
Definition: CUAnimationNode.h:52
static const Vec2 ZERO
Definition: CUVec2.h:71
float _seamEpsilon
Definition: CUCapsuleObstacle.h:110
Size _dimension
Definition: CUCapsuleObstacle.h:105
static std::shared_ptr< CapsuleObstacle > alloc()
Definition: CUCapsuleObstacle.h:226
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos, const Size &size)
Definition: CUCapsuleObstacle.h:265
const Orientation & getOrientation() const
Definition: CUCapsuleObstacle.h:351
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos)
Definition: CUCapsuleObstacle.h:243