Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUObstacleSelector.h
1 //
2 // CUObstacleSelector.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This class implements a selection tool for dragging physics objects with a mouse.
6 // It is essentially an instance of b2MouseJoint, but with an API that makes it a lot
7 // easier to use. As with all instances of b2MouseJoint, there will be some lag in
8 // the drag (though this is true on touch devices in general). You can adjust the
9 // degree of this lag by adjusting the force. However, larger forces can cause artifacts
10 // when dragging an obstacle through other obstacles.
11 //
12 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
13 //
14 // Author: Walker White
15 // Version: 12/28/15
16 //
17 #ifndef __CU_OBSTACLE_SELECTOR_H__
18 #define __CU_OBSTACLE_SELECTOR_H__
19 
20 #include <base/CCRef.h>
21 #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
22 #include <Box2D/Dynamics/b2Fixture.h>
23 #include "CUWorldController.h"
24 
26 #define DEFAULT_MSIZE 0.2f
27 
28 #define DEFAULT_FREQUENCY 10.0f
29 
30 #define DEFAULT_DAMPING 0.7f
31 
32 #define DEFAULT_FORCE 1000.0f
33 
34 NS_CC_BEGIN
35 
36 #pragma mark -
37 #pragma mark Obstacle Selector
38 
51 class CC_DLL ObstacleSelector : public Ref {
52 private:
54  CC_DISALLOW_COPY_AND_ASSIGN(ObstacleSelector);
55 
56 protected:
60  b2Fixture* _selection;
62  b2Body* _ground;
63 
65  b2MouseJointDef _jointDef;
67  b2MouseJoint* _mouseJoint;
68 
70  Rect _pointer;
72  float _force;
73 
74 
75 public:
76 #pragma mark Static Constructors
77 
90  static ObstacleSelector* create(WorldController* controller);
91 
104  static ObstacleSelector* create(WorldController* controller, const Size& mouseSize);
105 
106 
107 #pragma mark Selection Methods
108 
113  bool isSelected() const { return _selection != nullptr; }
114 
124  Obstacle* getObstacle();
125 
136  bool select(const Vec2& pos);
137 
145  void moveTo(const Vec2& pos);
146 
152  void deselect();
153 
164  bool onQuery(b2Fixture* fixture);
165 
166 
167 #pragma mark Attribute Properties
168 
175  float getFrequency() const { return _jointDef.frequencyHz; }
176 
184  void setFrequency(float speed) { _jointDef.frequencyHz = speed; }
185 
193  float getDamping() const { return _jointDef.dampingRatio; }
194 
202  void setDamping(float ratio) { _jointDef.dampingRatio = ratio; }
203 
212  float getForce() const { return _force; }
213 
222  void setForce(float force) { _force = force; }
223 
233  const Size& getMouseSize() const { return _pointer.size; }
234 
244  void setMouseSize(const Size& size) { _pointer.size = size; }
245 
246 
247 #pragma mark Initializers
248 CC_CONSTRUCTOR_ACCESS:
255  ObstacleSelector(void) : _controller(nullptr), _selection(nullptr), _ground(nullptr), _mouseJoint(nullptr) {}
256 
260  ~ObstacleSelector(void);
261 
275  bool init(WorldController* controller) { return init(controller, Size(DEFAULT_MSIZE, DEFAULT_MSIZE)); }
276 
289  bool init(WorldController* controller, const Size& mouseSize);
290 
291 };
292 
293 
294 
295 NS_CC_END
296 
297 #endif /* __CU_OBSTACLE_SELECTOR_H__ */
bool isSelected() const
Definition: CUObstacleSelector.h:113
WorldController * _controller
Definition: CUObstacleSelector.h:58
b2MouseJoint * _mouseJoint
Definition: CUObstacleSelector.h:67
void setForce(float force)
Definition: CUObstacleSelector.h:222
float getDamping() const
Definition: CUObstacleSelector.h:193
void setMouseSize(const Size &size)
Definition: CUObstacleSelector.h:244
Definition: CUObstacleSelector.h:51
b2Body * _ground
Definition: CUObstacleSelector.h:62
Definition: CUWorldController.h:48
b2Fixture * _selection
Definition: CUObstacleSelector.h:60
float getForce() const
Definition: CUObstacleSelector.h:212
const Size & getMouseSize() const
Definition: CUObstacleSelector.h:233
float getFrequency() const
Definition: CUObstacleSelector.h:175
Rect _pointer
Definition: CUObstacleSelector.h:70
void setFrequency(float speed)
Definition: CUObstacleSelector.h:184
b2MouseJointDef _jointDef
Definition: CUObstacleSelector.h:65
float _force
Definition: CUObstacleSelector.h:72
Definition: CUObstacle.h:46
void setDamping(float ratio)
Definition: CUObstacleSelector.h:202
bool init(WorldController *controller)
Definition: CUObstacleSelector.h:275