CUGL 1.3
Cornell University Game Library
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 MIT 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 
218  virtual bool init(const Vec2& pos, const Size& size, Orientation orient);
219 
220 #pragma mark -
221 #pragma mark Static Constructors
222 
227  static std::shared_ptr<CapsuleObstacle> alloc() {
228  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
229  return (result->init() ? result : nullptr);
230  }
231 
244  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos) {
245  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
246  return (result->init(pos) ? result : nullptr);
247  }
248 
266  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos, const Size& size) {
267  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
268  return (result->init(pos,size) ? result : nullptr);
269  }
270 
289  static std::shared_ptr<CapsuleObstacle> alloc(const Vec2& pos, const Size& size, Orientation orient) {
290  std::shared_ptr<CapsuleObstacle> result = std::make_shared<CapsuleObstacle>();
291  return (result->init(pos,size,orient) ? result : nullptr);
292  }
293 
294 
295 #pragma mark -
296 #pragma mark Dimensions
297 
302  const Size& getDimension() const { return _dimension; }
303 
309  void setDimension(const Size& value) { resize(value); markDirty(true); }
310 
317  void setDimension(float width, float height) { setDimension(Size(width,height)); }
318 
324  float getWidth() const { return _dimension.width; }
325 
331  void setWidth(float value) { setDimension(value,_dimension.height); }
332 
338  float getHeight() const { return _dimension.height; }
339 
345  void setHeight(float value) { setDimension(_dimension.width,value); }
346 
352  const Orientation& getOrientation() const { return _orient; }
353 
364  bool setOrientation(Orientation value);
365 
373  static bool isHorizontal(Orientation value) {
374  return (value == Orientation::LEFT || value == Orientation::RIGHT || value == Orientation::HORIZONTAL);
375  }
376 
377 #pragma mark -
378 #pragma mark Physics Methods
379 
389  void setSeamOffset(float value);
390 
401  float getSeamOffset() const { return _seamEpsilon; }
402 
412  virtual void setDensity(float value) override;
413 
419  virtual void createFixtures() override;
420 
426  virtual void releaseFixtures() override;
427 
428 };
429 
430 }
431 #endif /* __CU_CAPSULE_OBSTACLE_H__ */
cugl::CapsuleObstacle::_shape
b2PolygonShape _shape
Definition: CUCapsuleObstacle.h:92
cugl::CapsuleObstacle::Orientation::HORIZONTAL
cugl::CapsuleObstacle::getWidth
float getWidth() const
Definition: CUCapsuleObstacle.h:324
cugl::CapsuleObstacle::Orientation::VERTICAL
cugl::CapsuleObstacle::_ends
b2CircleShape _ends
Definition: CUCapsuleObstacle.h:94
cugl::CapsuleObstacle::Orientation::RIGHT
cugl::CapsuleObstacle::init
virtual bool init() override
Definition: CUCapsuleObstacle.h:165
cugl::CapsuleObstacle::alloc
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos)
Definition: CUCapsuleObstacle.h:244
cugl::CapsuleObstacle::alloc
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos, const Size &size)
Definition: CUCapsuleObstacle.h:266
cugl::Obstacle::markDirty
void markDirty(bool value)
Definition: CUObstacle.h:811
cugl::CapsuleObstacle::_cap1
b2Fixture * _cap1
Definition: CUCapsuleObstacle.h:101
cugl::Vec2::ZERO
static const Vec2 ZERO
Definition: CUVec2.h:71
cugl::CapsuleObstacle::Orientation::BOTTOM
cugl::CapsuleObstacle::getDimension
const Size & getDimension() const
Definition: CUCapsuleObstacle.h:302
cugl::Size
Definition: CUSize.h:57
cugl::CapsuleObstacle::_orient
Orientation _orient
Definition: CUCapsuleObstacle.h:107
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::CapsuleObstacle::_seamEpsilon
float _seamEpsilon
Definition: CUCapsuleObstacle.h:110
cugl::CapsuleObstacle::getOrientation
const Orientation & getOrientation() const
Definition: CUCapsuleObstacle.h:352
cugl::CapsuleObstacle::setDimension
void setDimension(float width, float height)
Definition: CUCapsuleObstacle.h:317
cugl::CapsuleObstacle::~CapsuleObstacle
virtual ~CapsuleObstacle()
Definition: CUCapsuleObstacle.h:156
cugl::CapsuleObstacle::setWidth
void setWidth(float value)
Definition: CUCapsuleObstacle.h:331
cugl::CapsuleObstacle::_dimension
Size _dimension
Definition: CUCapsuleObstacle.h:105
cugl::CapsuleObstacle::isHorizontal
static bool isHorizontal(Orientation value)
Definition: CUCapsuleObstacle.h:373
cugl::CapsuleObstacle::getHeight
float getHeight() const
Definition: CUCapsuleObstacle.h:338
cugl::CapsuleObstacle::releaseFixtures
virtual void releaseFixtures() override
cugl::CapsuleObstacle::setHeight
void setHeight(float value)
Definition: CUCapsuleObstacle.h:345
cugl::CapsuleObstacle::Orientation::TOP
cugl::Vec2
Definition: CUVec2.h:61
cugl::CapsuleObstacle::alloc
static std::shared_ptr< CapsuleObstacle > alloc(const Vec2 &pos, const Size &size, Orientation orient)
Definition: CUCapsuleObstacle.h:289
cugl::CapsuleObstacle::resize
void resize(const Size &size)
cugl::CapsuleObstacle::_center
b2AABB _center
Definition: CUCapsuleObstacle.h:96
cugl::CapsuleObstacle::Orientation::LEFT
cugl::CapsuleObstacle::getSeamOffset
float getSeamOffset() const
Definition: CUCapsuleObstacle.h:401
cugl::CapsuleObstacle::init
virtual bool init(const Vec2 &pos) override
Definition: CUCapsuleObstacle.h:179
cugl::CapsuleObstacle::alloc
static std::shared_ptr< CapsuleObstacle > alloc()
Definition: CUCapsuleObstacle.h:227
cugl::SimpleObstacle
Definition: CUSimpleObstacle.h:63
cugl::CapsuleObstacle::setSeamOffset
void setSeamOffset(float value)
cugl::Size::ZERO
static const Size ZERO
Definition: CUSize.h:66
cugl::CapsuleObstacle::setDensity
virtual void setDensity(float value) override
cugl::CapsuleObstacle::_core
b2Fixture * _core
Definition: CUCapsuleObstacle.h:99
cugl::CapsuleObstacle::resetDebug
virtual void resetDebug() override
cugl::CapsuleObstacle::CapsuleObstacle
CapsuleObstacle(void)
Definition: CUCapsuleObstacle.h:144
cugl::CapsuleObstacle::createFixtures
virtual void createFixtures() override
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::CapsuleObstacle::_cap2
b2Fixture * _cap2
Definition: CUCapsuleObstacle.h:103
cugl::CapsuleObstacle
Definition: CUCapsuleObstacle.h:72
cugl::CapsuleObstacle::Orientation
Orientation
Definition: CUCapsuleObstacle.h:75
cugl::CapsuleObstacle::setOrientation
bool setOrientation(Orientation value)
cugl::CapsuleObstacle::setDimension
void setDimension(const Size &value)
Definition: CUCapsuleObstacle.h:309