Cornell Cocos
Cornell Extensions to Cocos2d
CUComplexObstacle.h
1 //
2 // CUSimpleObstacle.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a physics object that supports mutliple Bodies.
6 // This is the base class for objects that are tied together with joints.
7 //
8 // This class does not provide Shape information, and cannot be instantiated
9 // directly. There are no default complex objects. You will need to create
10 // your own subclasses to use this class. It is very similar to Lab 4
11 // from CS 3152.
12 //
13 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
14 //
15 // Author: Walker White
16 // Version: 11/24/15
17 //
18 #ifndef __CC_COMPLEX_OBSTACLE_H__
19 #define __CC_COMPLEX_OBSTACLE_H__
20 
21 #include <Box2D/Dynamics/Joints/b2Joint.h>
22 #include "CUObstacle.h"
23 
24 
25 NS_CC_BEGIN
26 
27 #pragma mark -
28 #pragma mark Simple Obstacle
29 
30 
54 class CC_DLL ComplexObstacle : public Obstacle {
55 private:
57  CC_DISALLOW_COPY_AND_ASSIGN(ComplexObstacle);
58 
59 protected:
61  b2Body* _body;
63  vector<Obstacle*> _bodies;
65  vector<b2Joint*> _joints;
67  bool _tracking;
68 
69 public:
70 #pragma mark -
71 #pragma mark BodyDef Methods
72 
83  virtual b2BodyType getBodyType() const override {
84  return (_body != nullptr ? _body->GetType() : _bodyinfo.type);
85  }
86 
99  virtual void setBodyType(b2BodyType value) override {
100  if (_body != nullptr) {
101  _body->SetType(value);
102  } else {
103  _bodyinfo.type = value;
104  }
105  }
106 
118  virtual Vec2 getPosition() const override {
119  if (_body != nullptr) {
120  return Vec2(_body->GetPosition().x,_body->GetPosition().y);
121  } else {
122  return Vec2(_bodyinfo.position.x,_bodyinfo.position.y);
123  }
124  }
125 
134  virtual void setPosition(const Vec2& value) override { setPosition(value.x,value.y); }
135 
147  virtual void setPosition(float x, float y) override {
148  if (_body != nullptr) {
149  _body->SetTransform(b2Vec2(x,y),_body->GetAngle());
150  } else {
151  _bodyinfo.position.Set(x,y);
152  }
153  }
154 
162  virtual float getX() const override {
163  return (_body != nullptr ? _body->GetPosition().x : _bodyinfo.position.x);
164  }
165 
176  virtual void setX(float value) override {
177  if (_body != nullptr) {
178  _body->SetTransform(b2Vec2(value,_body->GetPosition().y),_body->GetAngle());
179  } else {
180  _bodyinfo.position.x = value;
181  }
182  }
183 
191  virtual float getY() const override {
192  return (_body != nullptr ? _body->GetPosition().y : _bodyinfo.position.y);
193  }
194 
205  virtual void setY(float value) override {
206  if (_body != nullptr) {
207  _body->SetTransform(b2Vec2(_body->GetPosition().x,value),_body->GetAngle());
208  } else {
209  _bodyinfo.position.y = value;
210  }
211  }
212 
221  virtual float getAngle() const override {
222  return (_body != nullptr ? _body->GetAngle() : _bodyinfo.angle);
223  }
224 
235  virtual void setAngle(float value) override {
236  if (_body != nullptr) {
237  _body->SetTransform(_body->GetPosition(),value);
238  } else {
239  _bodyinfo.angle = value;
240  }
241  }
242 
254  virtual Vec2 getLinearVelocity() const override {
255  if (_body != nullptr) {
256  return Vec2(_body->GetLinearVelocity().x,_body->GetLinearVelocity().y);
257  } else {
258  return Vec2(_bodyinfo.linearVelocity.x,_bodyinfo.linearVelocity.y);
259  }
260  }
261 
270  virtual void setLinearVelocity(const Vec2& value) override { setLinearVelocity(value.x,value.y); }
271 
283  virtual void setLinearVelocity(float x, float y) override {
284  if (_body != nullptr) {
285  _body->SetLinearVelocity(b2Vec2(x,y));
286  } else {
287  _bodyinfo.linearVelocity.Set(x,y);
288  }
289  }
290 
298  virtual float getVX() const override {
299  if (_body != nullptr) {
300  return _body->GetLinearVelocity().x;
301  } else {
302  return _bodyinfo.linearVelocity.x;
303  }
304  }
305 
316  virtual void setVX(float value) override {
317  if (_body != nullptr) {
318  _body->SetLinearVelocity(b2Vec2(value,_body->GetLinearVelocity().y));
319  } else {
320  _bodyinfo.linearVelocity.x = value;
321  }
322  }
323 
329  virtual float getVY() const override {
330  if (_body != nullptr) {
331  return _body->GetLinearVelocity().y;
332  } else {
333  return _bodyinfo.linearVelocity.y;
334  }
335  }
336 
347  virtual void setVY(float value) override {
348  if (_body != nullptr) {
349  _body->SetLinearVelocity(b2Vec2(value,_body->GetLinearVelocity().y));
350  } else {
351  _bodyinfo.linearVelocity.x = value;
352  }
353  }
354 
363  virtual float getAngularVelocity() const override {
364  return (_body != nullptr ? _body->GetAngularVelocity() : _bodyinfo.angularVelocity);
365  }
366 
377  virtual void setAngularVelocity(float value) override {
378  if (_body != nullptr) {
379  _body->SetAngularVelocity(value);
380  } else {
381  _bodyinfo.angularVelocity = value;
382  }
383  }
384 
399  virtual bool isActive() const override {
400  return (_body != nullptr ? _body->IsActive() : _bodyinfo.active);
401  }
402 
416  virtual void setActive(bool value) override {
417  if (_body != nullptr) {
418  _body->SetActive(value);
419  } else {
420  _bodyinfo.active = value;
421  }
422  }
423 
438  virtual bool isAwake() const override {
439  return (_body != nullptr ? _body->IsAwake() : _bodyinfo.awake);
440  }
441 
455  virtual void setAwake(bool value) override {
456  if (_body != nullptr) {
457  _body->SetAwake(value);
458  } else {
459  _bodyinfo.awake = value;
460  }
461  }
462 
477  virtual bool isSleepingAllowed() const override {
478  return (_body != nullptr ? _body->IsSleepingAllowed() : _bodyinfo.allowSleep);
479  }
480 
494  virtual void setSleepingAllowed(bool value) override {
495  if (_body != nullptr) {
496  _body->SetSleepingAllowed(value);
497  } else {
498  _bodyinfo.allowSleep = value;
499  }
500  }
501 
502 
523  virtual bool isBullet() const override {
524  return (_body != nullptr ? _body->IsBullet() : _bodyinfo.bullet);
525  }
526 
546  virtual void setBullet(bool value) override {
547  if (_body != nullptr) {
548  _body->SetBullet(value);
549  } else {
550  _bodyinfo.bullet = value;
551  }
552  }
553 
563  virtual bool isFixedRotation() const override {
564  return (_body != nullptr ? _body->IsFixedRotation() : _bodyinfo.fixedRotation);
565  }
566 
577  virtual void setFixedRotation(bool value) override {
578  if (_body != nullptr) {
579  _body->SetFixedRotation(value);
580  } else {
581  _bodyinfo.fixedRotation = value;
582  }
583  }
584 
597  virtual float getGravityScale() const override {
598  return (_body != nullptr ? _body->GetGravityScale() : _bodyinfo.gravityScale);
599  }
600 
612  virtual void setGravityScale(float value) override {
613  if (_body != nullptr) {
614  _body->SetGravityScale(value);
615  } else {
616  _bodyinfo.gravityScale = value;
617  }
618  }
619 
637  virtual float getLinearDamping() const override {
638  return (_body != nullptr ? _body->GetLinearDamping() : _bodyinfo.linearDamping);
639  }
640 
657  virtual void setLinearDamping(float value) override {
658  if (_body != nullptr) {
659  _body->SetLinearDamping(value);
660  } else {
661  _bodyinfo.linearDamping = value;
662  }
663  }
664 
682  virtual float getAngularDamping() const override {
683  return (_body != nullptr ? _body->GetAngularDamping() : _bodyinfo.angularDamping);
684  }
685 
702  virtual void setAngularDamping(float value) override {
703  if (_body != nullptr) {
704  _body->SetAngularDamping(value);
705  } else {
706  _bodyinfo.angularDamping = value;
707  }
708  }
709 
710 
711 #pragma mark -
712 #pragma mark FixtureDef Methods
713 
725  virtual void setDensity(float value) override;
726 
741  virtual void setFriction(float value) override;
742 
757  virtual void setRestitution(float value) override;
758 
771  virtual void setSensor(bool value) override;
772 
788  virtual void setFilterData(b2Filter value) override;
789 
790 
791 #pragma mark -
792 #pragma mark MassData Methods
793 
806  virtual Vec2 getCentroid() const override {
807  if (_body != nullptr) {
808  return Vec2(_body->GetLocalCenter().x,_body->GetLocalCenter().y);
809  } else {
810  return Vec2(_massdata.center.x,_massdata.center.y);
811  }
812  }
813 
822  virtual void setCentroid(const Vec2& value) override { setCentroid(value.x,value.y); }
823 
834  virtual void setCentroid(float x, float y) override {
835  Obstacle::setCentroid(x, y);
836  if (_body != nullptr) {
837  _body->SetMassData(&_massdata); // Protected accessor?
838  }
839  }
840 
853  virtual float getInertia() const override {
854  return (_body != nullptr ? _body->GetInertia() : _massdata.I);
855  }
856 
869  virtual void setInertia(float value) override {
870  Obstacle::setInertia(value);
871  if (_body != nullptr) {
872  _body->SetMassData(&_massdata); // Protected accessor?
873  }
874  }
875 
885  virtual float getMass() const override {
886  return (_body != nullptr ? _body->GetMass() : _massdata.mass);
887  }
888 
898  virtual void setMass(float value) override {
899  Obstacle::setMass(value);
900  if (_body != nullptr) {
901  _body->SetMassData(&_massdata); // Protected accessor?
902  }
903  }
904 
912  virtual void resetMass() override {
914  if (_body != nullptr) {
915  _body->ResetMassData();
916  }
917  }
918 
919 #pragma mark -
920 #pragma mark Physics Methods
921 
929  virtual b2Body* getBody() const override { return _body; }
930 
939  const vector<Obstacle*>& getBodies() { return _bodies; }
940 
949  const vector<b2Joint*>& getJoints() { return _joints; }
950 
963  virtual bool activatePhysics(b2World& world) override;
964 
971  virtual void deactivatePhysics(b2World& world) override;
972 
980  virtual void createFixtures() {}
981 
989  virtual void releaseFixtures() {}
990 
1001  virtual bool createJoints(b2World& world) { return false; }
1002 
1013  virtual void update(float delta) override;
1014 
1015 
1016 #pragma mark -
1017 #pragma mark Scene Graph Methods
1018 
1030  virtual void setDrawScale(const Vec2& value) override { setDrawScale(value.x,value.y); }
1031 
1045  virtual void setDrawScale(float x, float y) override;
1046 
1059  bool isSceneTracking() { return _tracking; }
1060 
1071  void setSceneTracking(bool track) { _tracking = track; }
1072 
1081  virtual void positionSceneNode() override;
1082 
1091  virtual void positionDebugNode() override;
1092 
1093 
1094 #pragma mark -
1095 #pragma mark Initializers
1096 CC_CONSTRUCTOR_ACCESS:
1100  ComplexObstacle() : Obstacle(), _body(nullptr), _tracking(false) { }
1101 
1111  virtual ~ComplexObstacle();
1112 };
1113 
1114 NS_CC_END
1115 #endif /* defined(__CC_COMPLEX_OBSTACLE_H__) */
virtual Vec2 getCentroid() const override
Definition: CUComplexObstacle.h:806
virtual void setAngle(float value) override
Definition: CUComplexObstacle.h:235
vector< Obstacle * > _bodies
Definition: CUComplexObstacle.h:63
virtual void setInertia(float value)
Definition: CUObstacle.cpp:126
virtual void setDrawScale(const Vec2 &value) override
Definition: CUComplexObstacle.h:1030
virtual float getAngularDamping() const override
Definition: CUComplexObstacle.h:682
virtual void resetMass() override
Definition: CUComplexObstacle.h:912
virtual void setCentroid(float x, float y) override
Definition: CUComplexObstacle.h:834
virtual void setLinearDamping(float value) override
Definition: CUComplexObstacle.h:657
virtual void setLinearVelocity(float x, float y) override
Definition: CUComplexObstacle.h:283
virtual void releaseFixtures()
Definition: CUComplexObstacle.h:989
virtual void setGravityScale(float value) override
Definition: CUComplexObstacle.h:612
virtual void setSleepingAllowed(bool value) override
Definition: CUComplexObstacle.h:494
virtual float getAngle() const override
Definition: CUComplexObstacle.h:221
virtual void setFriction(float value)
Definition: CUObstacle.h:562
virtual float getVY() const override
Definition: CUComplexObstacle.h:329
virtual float getInertia() const override
Definition: CUComplexObstacle.h:853
virtual void setAwake(bool value) override
Definition: CUComplexObstacle.h:455
virtual void setActive(bool value) override
Definition: CUComplexObstacle.h:416
bool isSceneTracking()
Definition: CUComplexObstacle.h:1059
virtual void setAngularDamping(float value) override
Definition: CUComplexObstacle.h:702
virtual void setFilterData(b2Filter value)
Definition: CUObstacle.h:638
virtual bool createJoints(b2World &world)
Definition: CUComplexObstacle.h:1001
virtual float getY() const override
Definition: CUComplexObstacle.h:191
void setSceneTracking(bool track)
Definition: CUComplexObstacle.h:1071
virtual bool isSleepingAllowed() const override
Definition: CUComplexObstacle.h:477
virtual void resetMass()
Definition: CUObstacle.h:713
Definition: CUComplexObstacle.h:54
virtual float getX() const override
Definition: CUComplexObstacle.h:162
virtual void setSensor(bool value)
Definition: CUObstacle.h:610
b2Body * _body
Definition: CUComplexObstacle.h:61
virtual float getAngularVelocity() const override
Definition: CUComplexObstacle.h:363
virtual b2Body * getBody() const override
Definition: CUComplexObstacle.h:929
virtual void setCentroid(const Vec2 &value)
Definition: CUObstacle.h:662
virtual b2BodyType getBodyType() const override
Definition: CUComplexObstacle.h:83
virtual void positionSceneNode()
Definition: CUObstacle.cpp:240
virtual float getGravityScale() const override
Definition: CUComplexObstacle.h:597
virtual void setBodyType(b2BodyType value) override
Definition: CUComplexObstacle.h:99
virtual void setLinearVelocity(const Vec2 &value) override
Definition: CUComplexObstacle.h:270
virtual bool isFixedRotation() const override
Definition: CUComplexObstacle.h:563
virtual void positionDebugNode()
Definition: CUObstacle.cpp:257
virtual void setVY(float value) override
Definition: CUComplexObstacle.h:347
virtual void setAngularVelocity(float value) override
Definition: CUComplexObstacle.h:377
virtual void setFixedRotation(bool value) override
Definition: CUComplexObstacle.h:577
virtual Vec2 getPosition() const override
Definition: CUComplexObstacle.h:118
virtual Vec2 getLinearVelocity() const override
Definition: CUComplexObstacle.h:254
virtual bool isAwake() const override
Definition: CUComplexObstacle.h:438
virtual bool isActive() const override
Definition: CUComplexObstacle.h:399
virtual void deactivatePhysics(b2World &world)
Definition: CUObstacle.h:895
virtual bool activatePhysics(b2World &world)
Definition: CUObstacle.h:887
virtual void setInertia(float value) override
Definition: CUComplexObstacle.h:869
virtual void setDensity(float value)
Definition: CUObstacle.h:536
virtual void setMass(float value)
Definition: CUObstacle.cpp:142
virtual void setCentroid(const Vec2 &value) override
Definition: CUComplexObstacle.h:822
const vector< Obstacle * > & getBodies()
Definition: CUComplexObstacle.h:939
virtual float getMass() const override
Definition: CUComplexObstacle.h:885
vector< b2Joint * > _joints
Definition: CUComplexObstacle.h:65
virtual void setVX(float value) override
Definition: CUComplexObstacle.h:316
virtual void setY(float value) override
Definition: CUComplexObstacle.h:205
bool _tracking
Definition: CUComplexObstacle.h:67
const vector< b2Joint * > & getJoints()
Definition: CUComplexObstacle.h:949
virtual float getLinearDamping() const override
Definition: CUComplexObstacle.h:637
virtual void setPosition(float x, float y) override
Definition: CUComplexObstacle.h:147
virtual float getVX() const override
Definition: CUComplexObstacle.h:298
Definition: CUObstacle.h:46
virtual void update(float delta)
Definition: CUObstacle.h:910
virtual void setPosition(const Vec2 &value) override
Definition: CUComplexObstacle.h:134
virtual void setBullet(bool value) override
Definition: CUComplexObstacle.h:546
virtual void setDrawScale(const Vec2 &value)
Definition: CUObstacle.h:789
virtual void setRestitution(float value)
Definition: CUObstacle.h:588
virtual void setMass(float value) override
Definition: CUComplexObstacle.h:898
virtual bool isBullet() const override
Definition: CUComplexObstacle.h:523
virtual void setX(float value) override
Definition: CUComplexObstacle.h:176
virtual void createFixtures()
Definition: CUComplexObstacle.h:980