CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUComplexObstacle.h
1 //
2 // CUComplexObstacle.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 class uses our standard shared-pointer architecture.
14 //
15 // 1. The constructor does not perform any initialization; it just sets all
16 // attributes to their defaults.
17 //
18 // 2. All initialization takes place via init methods, which can fail if an
19 // object is initialized more than once.
20 //
21 // 3. All allocation takes place via static constructors which return a shared
22 // pointer.
23 //
24 // CUGL zlib License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
44 //
45 // Author: Walker White
46 // Version: 11/6/16
47 //
48 #ifndef __CC_COMPLEX_OBSTACLE_H__
49 #define __CC_COMPLEX_OBSTACLE_H__
50 
51 #include <Box2D/Dynamics/Joints/b2Joint.h>
52 #include "CUObstacle.h"
53 
54 
55 namespace cugl {
56 
57 #pragma mark -
58 #pragma mark Complex Obstacle
59 
60 
85 class ComplexObstacle : public Obstacle {
86 protected:
88  b2Body* _body;
90  std::vector<std::shared_ptr<Obstacle>> _bodies;
92  std::vector<b2Joint*> _joints;
93 
94 #pragma mark -
95 #pragma mark Scene Graph Internals
96 
103  virtual void resetDebug() override;
104 
112  virtual void updateDebug() override;
113 
114 #pragma mark -
115 #pragma mark Constructors
116 public:
124  ComplexObstacle() : Obstacle(), _body(nullptr) { }
125 
135  virtual ~ComplexObstacle();
136 
137 
138 #pragma mark -
139 #pragma mark BodyDef Methods
140 
151  virtual b2BodyType getBodyType() const override {
152  return (_body != nullptr ? _body->GetType() : _bodyinfo.type);
153  }
154 
167  virtual void setBodyType(b2BodyType value) override {
168  if (_body != nullptr) {
169  _body->SetType(value);
170  } else {
171  _bodyinfo.type = value;
172  }
173  }
174 
186  virtual Vec2 getPosition() const override {
187  if (_body != nullptr) {
188  return Vec2(_body->GetPosition().x,_body->GetPosition().y);
189  } else {
190  return Vec2(_bodyinfo.position.x,_bodyinfo.position.y);
191  }
192  }
193 
202  virtual void setPosition(const Vec2& value) override { setPosition(value.x,value.y); }
203 
215  virtual void setPosition(float x, float y) override {
216  if (_body != nullptr) {
217  _body->SetTransform(b2Vec2(x,y),_body->GetAngle());
218  } else {
219  _bodyinfo.position.Set(x,y);
220  }
221  }
222 
230  virtual float getX() const override {
231  return (_body != nullptr ? _body->GetPosition().x : _bodyinfo.position.x);
232  }
233 
244  virtual void setX(float value) override {
245  if (_body != nullptr) {
246  _body->SetTransform(b2Vec2(value,_body->GetPosition().y),_body->GetAngle());
247  } else {
248  _bodyinfo.position.x = value;
249  }
250  }
251 
259  virtual float getY() const override {
260  return (_body != nullptr ? _body->GetPosition().y : _bodyinfo.position.y);
261  }
262 
273  virtual void setY(float value) override {
274  if (_body != nullptr) {
275  _body->SetTransform(b2Vec2(_body->GetPosition().x,value),_body->GetAngle());
276  } else {
277  _bodyinfo.position.y = value;
278  }
279  }
280 
289  virtual float getAngle() const override {
290  return (_body != nullptr ? _body->GetAngle() : _bodyinfo.angle);
291  }
292 
303  virtual void setAngle(float value) override {
304  if (_body != nullptr) {
305  _body->SetTransform(_body->GetPosition(),value);
306  } else {
307  _bodyinfo.angle = value;
308  }
309  }
310 
322  virtual Vec2 getLinearVelocity() const override {
323  if (_body != nullptr) {
324  return Vec2(_body->GetLinearVelocity().x,_body->GetLinearVelocity().y);
325  } else {
326  return Vec2(_bodyinfo.linearVelocity.x,_bodyinfo.linearVelocity.y);
327  }
328  }
329 
338  virtual void setLinearVelocity(const Vec2& value) override { setLinearVelocity(value.x,value.y); }
339 
351  virtual void setLinearVelocity(float x, float y) override {
352  if (_body != nullptr) {
353  _body->SetLinearVelocity(b2Vec2(x,y));
354  } else {
355  _bodyinfo.linearVelocity.Set(x,y);
356  }
357  }
358 
366  virtual float getVX() const override {
367  if (_body != nullptr) {
368  return _body->GetLinearVelocity().x;
369  } else {
370  return _bodyinfo.linearVelocity.x;
371  }
372  }
373 
384  virtual void setVX(float value) override {
385  if (_body != nullptr) {
386  _body->SetLinearVelocity(b2Vec2(value,_body->GetLinearVelocity().y));
387  } else {
388  _bodyinfo.linearVelocity.x = value;
389  }
390  }
391 
397  virtual float getVY() const override {
398  if (_body != nullptr) {
399  return _body->GetLinearVelocity().y;
400  } else {
401  return _bodyinfo.linearVelocity.y;
402  }
403  }
404 
415  virtual void setVY(float value) override {
416  if (_body != nullptr) {
417  _body->SetLinearVelocity(b2Vec2(value,_body->GetLinearVelocity().y));
418  } else {
419  _bodyinfo.linearVelocity.x = value;
420  }
421  }
422 
431  virtual float getAngularVelocity() const override {
432  return (_body != nullptr ? _body->GetAngularVelocity() : _bodyinfo.angularVelocity);
433  }
434 
445  virtual void setAngularVelocity(float value) override {
446  if (_body != nullptr) {
447  _body->SetAngularVelocity(value);
448  } else {
449  _bodyinfo.angularVelocity = value;
450  }
451  }
452 
467  virtual bool isActive() const override {
468  return (_body != nullptr ? _body->IsActive() : _bodyinfo.active);
469  }
470 
484  virtual void setActive(bool value) override {
485  if (_body != nullptr) {
486  _body->SetActive(value);
487  } else {
488  _bodyinfo.active = value;
489  }
490  }
491 
506  virtual bool isAwake() const override {
507  return (_body != nullptr ? _body->IsAwake() : _bodyinfo.awake);
508  }
509 
523  virtual void setAwake(bool value) override {
524  if (_body != nullptr) {
525  _body->SetAwake(value);
526  } else {
527  _bodyinfo.awake = value;
528  }
529  }
530 
545  virtual bool isSleepingAllowed() const override {
546  return (_body != nullptr ? _body->IsSleepingAllowed() : _bodyinfo.allowSleep);
547  }
548 
562  virtual void setSleepingAllowed(bool value) override {
563  if (_body != nullptr) {
564  _body->SetSleepingAllowed(value);
565  } else {
566  _bodyinfo.allowSleep = value;
567  }
568  }
569 
570 
591  virtual bool isBullet() const override {
592  return (_body != nullptr ? _body->IsBullet() : _bodyinfo.bullet);
593  }
594 
614  virtual void setBullet(bool value) override {
615  if (_body != nullptr) {
616  _body->SetBullet(value);
617  } else {
618  _bodyinfo.bullet = value;
619  }
620  }
621 
631  virtual bool isFixedRotation() const override {
632  return (_body != nullptr ? _body->IsFixedRotation() : _bodyinfo.fixedRotation);
633  }
634 
645  virtual void setFixedRotation(bool value) override {
646  if (_body != nullptr) {
647  _body->SetFixedRotation(value);
648  } else {
649  _bodyinfo.fixedRotation = value;
650  }
651  }
652 
665  virtual float getGravityScale() const override {
666  return (_body != nullptr ? _body->GetGravityScale() : _bodyinfo.gravityScale);
667  }
668 
680  virtual void setGravityScale(float value) override {
681  if (_body != nullptr) {
682  _body->SetGravityScale(value);
683  } else {
684  _bodyinfo.gravityScale = value;
685  }
686  }
687 
705  virtual float getLinearDamping() const override {
706  return (_body != nullptr ? _body->GetLinearDamping() : _bodyinfo.linearDamping);
707  }
708 
725  virtual void setLinearDamping(float value) override {
726  if (_body != nullptr) {
727  _body->SetLinearDamping(value);
728  } else {
729  _bodyinfo.linearDamping = value;
730  }
731  }
732 
750  virtual float getAngularDamping() const override {
751  return (_body != nullptr ? _body->GetAngularDamping() : _bodyinfo.angularDamping);
752  }
753 
770  virtual void setAngularDamping(float value) override {
771  if (_body != nullptr) {
772  _body->SetAngularDamping(value);
773  } else {
774  _bodyinfo.angularDamping = value;
775  }
776  }
777 
778 
779 #pragma mark -
780 #pragma mark FixtureDef Methods
781 
793  virtual void setDensity(float value) override;
794 
809  virtual void setFriction(float value) override;
810 
825  virtual void setRestitution(float value) override;
826 
839  virtual void setSensor(bool value) override;
840 
856  virtual void setFilterData(b2Filter value) override;
857 
858 
859 #pragma mark -
860 #pragma mark MassData Methods
861 
874  virtual Vec2 getCentroid() const override {
875  if (_body != nullptr) {
876  return Vec2(_body->GetLocalCenter().x,_body->GetLocalCenter().y);
877  } else {
878  return Vec2(_massdata.center.x,_massdata.center.y);
879  }
880  }
881 
890  virtual void setCentroid(const Vec2& value) override { setCentroid(value.x,value.y); }
891 
903  virtual void setCentroid(float x, float y) override {
904  Obstacle::setCentroid(x, y);
905  if (_body != nullptr) {
906  _body->SetMassData(&_massdata); // Protected accessor?
907  }
908  }
909 
922  virtual float getInertia() const override {
923  return (_body != nullptr ? _body->GetInertia() : _massdata.I);
924  }
925 
938  virtual void setInertia(float value) override {
939  Obstacle::setInertia(value);
940  if (_body != nullptr) {
941  _body->SetMassData(&_massdata); // Protected accessor?
942  }
943  }
944 
954  virtual float getMass() const override {
955  return (_body != nullptr ? _body->GetMass() : _massdata.mass);
956  }
957 
967  virtual void setMass(float value) override {
968  Obstacle::setMass(value);
969  if (_body != nullptr) {
970  _body->SetMassData(&_massdata); // Protected accessor?
971  }
972  }
973 
981  virtual void resetMass() override {
983  if (_body != nullptr) {
984  _body->ResetMassData();
985  }
986  }
987 
988 #pragma mark -
989 #pragma mark Physics Methods
990 
998  virtual b2Body* getBody() override { return _body; }
999 
1008  const std::vector<std::shared_ptr<Obstacle>>& getBodies() { return _bodies; }
1009 
1018  const std::vector<b2Joint*>& getJoints() { return _joints; }
1019 
1032  virtual bool activatePhysics(b2World& world) override;
1033 
1040  virtual void deactivatePhysics(b2World& world) override;
1041 
1049  virtual void createFixtures() {}
1050 
1058  virtual void releaseFixtures() {}
1059 
1070  virtual bool createJoints(b2World& world) { return false; }
1071 
1082  virtual void update(float delta) override;
1083 
1084 
1085 #pragma mark -
1086 #pragma mark Scene Graph Methods
1087 
1095  virtual void setDebugColor(Color4 color) override;
1096 
1106  void setDebugColor(Color4 color, bool cascade);
1107 
1124  virtual void setDebugScene(const std::shared_ptr<Node>& node) override;
1125 
1126 };
1127 
1128 }
1129 #endif /* __CU_COMPLEX_OBSTACLE_H__ */
virtual void setDebugScene(const std::shared_ptr< Node > &node) override
virtual bool createJoints(b2World &world)
Definition: CUComplexObstacle.h:1070
virtual float getLinearDamping() const override
Definition: CUComplexObstacle.h:705
virtual void setGravityScale(float value) override
Definition: CUComplexObstacle.h:680
float x
Definition: CUVec2.h:66
const std::vector< std::shared_ptr< Obstacle > > & getBodies()
Definition: CUComplexObstacle.h:1008
virtual float getVY() const override
Definition: CUComplexObstacle.h:397
virtual bool activatePhysics(b2World &world) override
float y
Definition: CUVec2.h:68
virtual ~ComplexObstacle()
virtual void resetMass() override
Definition: CUComplexObstacle.h:981
virtual float getAngularDamping() const override
Definition: CUComplexObstacle.h:750
Definition: CUVec2.h:61
virtual void setFriction(float value) override
virtual void setLinearVelocity(float x, float y) override
Definition: CUComplexObstacle.h:351
virtual Vec2 getCentroid() const override
Definition: CUComplexObstacle.h:874
const std::vector< b2Joint * > & getJoints()
Definition: CUComplexObstacle.h:1018
b2MassData _massdata
Definition: CUObstacle.h:83
virtual void releaseFixtures()
Definition: CUComplexObstacle.h:1058
virtual void deactivatePhysics(b2World &world) override
virtual bool isFixedRotation() const override
Definition: CUComplexObstacle.h:631
virtual void setDensity(float value) override
virtual void setFilterData(b2Filter value) override
ComplexObstacle()
Definition: CUComplexObstacle.h:124
virtual void setSleepingAllowed(bool value) override
Definition: CUComplexObstacle.h:562
virtual void setLinearVelocity(const Vec2 &value) override
Definition: CUComplexObstacle.h:338
virtual float getGravityScale() const override
Definition: CUComplexObstacle.h:665
virtual void setBullet(bool value) override
Definition: CUComplexObstacle.h:614
virtual float getX() const override
Definition: CUComplexObstacle.h:230
virtual void setBodyType(b2BodyType value) override
Definition: CUComplexObstacle.h:167
virtual float getAngularVelocity() const override
Definition: CUComplexObstacle.h:431
virtual void setCentroid(const Vec2 &value)
Definition: CUObstacle.h:715
virtual float getY() const override
Definition: CUComplexObstacle.h:259
virtual bool isBullet() const override
Definition: CUComplexObstacle.h:591
virtual void update(float delta) override
virtual bool isActive() const override
Definition: CUComplexObstacle.h:467
std::vector< b2Joint * > _joints
Definition: CUComplexObstacle.h:92
virtual void setInertia(float value) override
Definition: CUComplexObstacle.h:938
virtual b2BodyType getBodyType() const override
Definition: CUComplexObstacle.h:151
b2BodyDef _bodyinfo
Definition: CUObstacle.h:79
virtual void setMass(float value)
virtual b2Body * getBody() override
Definition: CUComplexObstacle.h:998
virtual void setCentroid(float x, float y) override
Definition: CUComplexObstacle.h:903
virtual void resetDebug() override
virtual void setVY(float value) override
Definition: CUComplexObstacle.h:415
virtual void setSensor(bool value) override
virtual void setRestitution(float value) override
virtual void setPosition(const Vec2 &value) override
Definition: CUComplexObstacle.h:202
virtual void setAngle(float value) override
Definition: CUComplexObstacle.h:303
virtual bool isAwake() const override
Definition: CUComplexObstacle.h:506
virtual void updateDebug() override
virtual void setCentroid(const Vec2 &value) override
Definition: CUComplexObstacle.h:890
Definition: CUObstacle.h:76
virtual void setActive(bool value) override
Definition: CUComplexObstacle.h:484
Definition: CUComplexObstacle.h:85
virtual void createFixtures()
Definition: CUComplexObstacle.h:1049
b2Body * _body
Definition: CUComplexObstacle.h:88
virtual float getVX() const override
Definition: CUComplexObstacle.h:366
virtual float getMass() const override
Definition: CUComplexObstacle.h:954
virtual Vec2 getLinearVelocity() const override
Definition: CUComplexObstacle.h:322
Definition: CUColor4.h:1104
virtual void setAngularDamping(float value) override
Definition: CUComplexObstacle.h:770
virtual void setVX(float value) override
Definition: CUComplexObstacle.h:384
virtual void setFixedRotation(bool value) override
Definition: CUComplexObstacle.h:645
virtual void setY(float value) override
Definition: CUComplexObstacle.h:273
virtual float getInertia() const override
Definition: CUComplexObstacle.h:922
Definition: CUAnimationNode.h:52
virtual Vec2 getPosition() const override
Definition: CUComplexObstacle.h:186
virtual void setDebugColor(Color4 color) override
virtual void setMass(float value) override
Definition: CUComplexObstacle.h:967
virtual void setAwake(bool value) override
Definition: CUComplexObstacle.h:523
virtual bool isSleepingAllowed() const override
Definition: CUComplexObstacle.h:545
virtual void resetMass()
Definition: CUObstacle.h:766
virtual void setPosition(float x, float y) override
Definition: CUComplexObstacle.h:215
std::vector< std::shared_ptr< Obstacle > > _bodies
Definition: CUComplexObstacle.h:90
virtual void setInertia(float value)
virtual void setLinearDamping(float value) override
Definition: CUComplexObstacle.h:725
virtual void setAngularVelocity(float value) override
Definition: CUComplexObstacle.h:445
virtual void setX(float value) override
Definition: CUComplexObstacle.h:244
virtual float getAngle() const override
Definition: CUComplexObstacle.h:289