CUGL
Cornell University Game Library
CUSimpleObstacle.h
1 //
2 // CUSimpleObstacle.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class serves to provide a uniform interface for all single-body objects
6 // (regardless of shape). How, it still cannot be instantiated directly, as
7 // the correct instantiation depends on the shape. See BoxObstacle and
8 // CircleObstacle for concrete examples.
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_SIMPLE_OBSTACLE_H__
46 #define __CU_SIMPLE_OBSTACLE_H__
47 
48 #include "CUObstacle.h"
49 #include <cmath>
50 
51 namespace cugl {
52 
63 class SimpleObstacle : public Obstacle {
64 protected:
66  b2Body* _body;
67 
69  int _posSnap;
71  unsigned long _posFact;
73  int _angSnap;
75  unsigned long _angFact;
76 
77 
78 #pragma mark -
79 #pragma mark Constructors
80 public:
88  SimpleObstacle() : Obstacle(), _body(nullptr) {
89  _posSnap = _angSnap = -1;
90  }
91 
101  virtual ~SimpleObstacle() {
102  CUAssertLog(_body == nullptr, "You must deactive physics before deleting an object");
103  }
104 
105 
106 #pragma mark -
107 #pragma mark BodyDef Methods
108 
118  virtual b2BodyType getBodyType() const override {
119  return (_body != nullptr ? _body->GetType() : _bodyinfo.type);
120  }
121 
132  virtual void setBodyType(b2BodyType value) override {
133  if (_body != nullptr) {
134  _body->SetType(value);
135  } else {
136  _bodyinfo.type = value;
137  }
138  }
139 
149  virtual Vec2 getPosition() const override {
150  if (_body != nullptr) {
151  return Vec2(_body->GetPosition().x,_body->GetPosition().y);
152  } else {
153  return Vec2(_bodyinfo.position.x,_bodyinfo.position.y);
154  }
155  }
156 
165  virtual void setPosition(const Vec2& value) override { setPosition(value.x,value.y); }
166 
173  virtual void setPosition(float x, float y) override {
174  if (_body != nullptr) {
175  _body->SetTransform(b2Vec2(x,y),_body->GetAngle());
176  } else {
177  _bodyinfo.position.Set(x,y);
178  }
179  }
180 
186  virtual float getX() const override {
187  return (_body != nullptr ? _body->GetPosition().x : _bodyinfo.position.x);
188  }
189 
195  virtual void setX(float value) override {
196  if (_body != nullptr) {
197  _body->SetTransform(b2Vec2(value,_body->GetPosition().y),_body->GetAngle());
198  } else {
199  _bodyinfo.position.x = value;
200  }
201  }
202 
208  virtual float getY() const override {
209  return (_body != nullptr ? _body->GetPosition().y : _bodyinfo.position.y);
210  }
211 
217  virtual void setY(float value) override {
218  if (_body != nullptr) {
219  _body->SetTransform(b2Vec2(_body->GetPosition().y,value),_body->GetAngle());
220  } else {
221  _bodyinfo.position.y = value;
222  }
223  }
224 
232  virtual float getAngle() const override {
233  return (_body != nullptr ? _body->GetAngle() : _bodyinfo.angle);
234  }
235 
241  virtual void setAngle(float value) override {
242  if (_body != nullptr) {
243  _body->SetTransform(_body->GetPosition(),value);
244  } else {
245  _bodyinfo.angle = value;
246  }
247  }
248 
258  virtual Vec2 getLinearVelocity() const override {
259  if (_body != nullptr) {
260  return Vec2(_body->GetLinearVelocity().x,_body->GetLinearVelocity().y);
261  } else {
262  return Vec2(_bodyinfo.linearVelocity.x,_bodyinfo.linearVelocity.y);
263  }
264  }
265 
274  virtual void setLinearVelocity(const Vec2& value) override { setLinearVelocity(value.x,value.y); }
275 
282  virtual void setLinearVelocity(float x, float y) override {
283  if (_body != nullptr) {
284  _body->SetLinearVelocity(b2Vec2(x,y));
285  } else {
286  _bodyinfo.linearVelocity.Set(x,y);
287  }
288  }
289 
295  virtual float getVX() const override {
296  if (_body != nullptr) {
297  return _body->GetLinearVelocity().x;
298  } else {
299  return _bodyinfo.linearVelocity.x;
300  }
301  }
302 
308  virtual void setVX(float value) override {
309  if (_body != nullptr) {
310  _body->SetLinearVelocity(b2Vec2(value,_body->GetLinearVelocity().y));
311  } else {
312  _bodyinfo.linearVelocity.x = value;
313  }
314  }
315 
321  virtual float getVY() const override {
322  if (_body != nullptr) {
323  return _body->GetLinearVelocity().y;
324  } else {
325  return _bodyinfo.linearVelocity.y;
326  }
327  }
328 
334  virtual void setVY(float value) override {
335  if (_body != nullptr) {
336  _body->SetLinearVelocity(b2Vec2(_body->GetLinearVelocity().x,value));
337  } else {
338  _bodyinfo.linearVelocity.y = value;
339  }
340  }
341 
349  virtual float getAngularVelocity() const override {
350  return (_body != nullptr ? _body->GetAngularVelocity() : _bodyinfo.angularVelocity);
351  }
352 
358  virtual void setAngularVelocity(float value) override {
359  if (_body != nullptr) {
360  _body->SetAngularVelocity(value);
361  } else {
362  _bodyinfo.angularVelocity = value;
363  }
364  }
365 
376  virtual bool isActive() const override {
377  return (_body != nullptr ? _body->IsActive() : _bodyinfo.active);
378  }
379 
390  virtual void setActive(bool value) override {
391  if (_body != nullptr) {
392  _body->SetActive(value);
393  } else {
394  _bodyinfo.active = value;
395  }
396  }
397 
409  virtual bool isAwake() const override {
410  return (_body != nullptr ? _body->IsAwake() : _bodyinfo.awake);
411  }
412 
424  virtual void setAwake(bool value) override {
425  if (_body != nullptr) {
426  _body->SetAwake(value);
427  } else {
428  _bodyinfo.awake = value;
429  }
430  }
431 
443  virtual bool isSleepingAllowed() const override {
444  return (_body != nullptr ? _body->IsSleepingAllowed() : _bodyinfo.allowSleep);
445  }
446 
458  virtual void setSleepingAllowed(bool value) override {
459  if (_body != nullptr) {
460  _body->SetSleepingAllowed(value);
461  } else {
462  _bodyinfo.allowSleep = value;
463  }
464  }
465 
482  virtual bool isBullet() const override {
483  return (_body != nullptr ? _body->IsBullet() : _bodyinfo.bullet);
484  }
485 
502  virtual void setBullet(bool value) override {
503  if (_body != nullptr) {
504  _body->SetBullet(value);
505  } else {
506  _bodyinfo.bullet = value;
507  }
508  }
509 
517  virtual bool isFixedRotation() const override {
518  return (_body != nullptr ? _body->IsFixedRotation() : _bodyinfo.fixedRotation);
519  }
520 
528  virtual void setFixedRotation(bool value) override {
529  if (_body != nullptr) {
530  _body->SetFixedRotation(value);
531  } else {
532  _bodyinfo.fixedRotation = value;
533  }
534  }
535 
544  virtual float getGravityScale() const override {
545  return (_body != nullptr ? _body->GetGravityScale() : _bodyinfo.gravityScale);
546  }
547 
556  virtual void setGravityScale(float value) override {
557  if (_body != nullptr) {
558  _body->SetGravityScale(value);
559  } else {
560  _bodyinfo.gravityScale = value;
561  }
562  }
563 
578  virtual float getLinearDamping() const override {
579  return (_body != nullptr ? _body->GetLinearDamping() : _bodyinfo.linearDamping);
580  }
581 
596  virtual void setLinearDamping(float value) override {
597  if (_body != nullptr) {
598  _body->SetLinearDamping(value);
599  } else {
600  _bodyinfo.linearDamping = value;
601  }
602  }
603 
618  virtual float getAngularDamping() const override {
619  return (_body != nullptr ? _body->GetAngularDamping() : _bodyinfo.angularDamping);
620  }
621 
636  virtual void setAngularDamping(float value) override {
637  if (_body != nullptr) {
638  _body->SetAngularDamping(value);
639  } else {
640  _bodyinfo.angularDamping = value;
641  }
642  }
643 
644 
645 #pragma mark -
646 #pragma mark FixtureDef Methods
647 
656  virtual void setDensity(float value) override;
657 
669  virtual void setFriction(float value) override;
670 
682  virtual void setRestitution(float value) override;
683 
693  virtual void setSensor(bool value) override;
694 
709  virtual void setFilterData(b2Filter value) override;
710 
711 
712 #pragma mark -
713 #pragma mark MassData Methods
714 
723  virtual Vec2 getCentroid() const override {
724  if (_body != nullptr) {
725  return Vec2(_body->GetLocalCenter().x,_body->GetLocalCenter().y);
726  } else {
727  return Vec2(_massdata.center.x,_massdata.center.y);
728  }
729  }
730 
739  virtual void setCentroid(const Vec2& value) override { setCentroid(value.x,value.y); }
740 
747  virtual void setCentroid(float x, float y) override {
748  Obstacle::setCentroid(x, y);
749  if (_body != nullptr) {
750  _body->SetMassData(&_massdata); // Protected accessor?
751  }
752  }
753 
762  virtual float getInertia() const override {
763  return (_body != nullptr ? _body->GetInertia() : _massdata.I);
764  }
765 
774  virtual void setInertia(float value) override {
775  Obstacle::setInertia(value);
776  if (_body != nullptr) {
777  _body->SetMassData(&_massdata); // Protected accessor?
778  }
779  }
780 
788  virtual float getMass() const override {
789  return (_body != nullptr ? _body->GetMass() : _massdata.mass);
790  }
791 
799  virtual void setMass(float value) override {
800  Obstacle::setMass(value);
801  if (_body != nullptr) {
802  _body->SetMassData(&_massdata); // Protected accessor?
803  }
804  }
805 
809  virtual void resetMass() override {
811  if (_body != nullptr) {
812  _body->ResetMassData();
813  }
814  }
815 
816 
817 #pragma mark -
818 #pragma mark Physics Methods
819 
828  virtual b2Body* getBody() override { return _body; }
829 
840  virtual bool activatePhysics(b2World& world) override;
841 
849  virtual void deactivatePhysics(b2World& world) override;
850 
856  virtual void createFixtures() {}
857 
863  virtual void releaseFixtures() {}
864 
878  virtual void update(float delta) override;
879 
880 
881 #pragma mark -
882 #pragma mark Render Snap
883 
897  int getPositionSnap() { return _posSnap; }
898 
913  void setPositionSnap(unsigned int snap) {
914  _posSnap = snap;
915  _posFact = (unsigned long)(_posSnap >= 0 ? std::pow(10, snap) : 0);
916  }
917 
932  int getAngleSnap() { return _angSnap; }
933 
948  void setAngleSnap(unsigned int snap) {
949  _angSnap = snap;
950  _angFact = (unsigned long)(_angSnap >= 0 ? std::pow(10, snap) : 0);
951  }
952 
953 protected:
961  virtual void updateDebug() override;
962 };
963 
964 }
965 #endif /* __CU_SIMPLE_OBSTACLE_H__ */
virtual void setX(float value) override
Definition: CUSimpleObstacle.h:195
virtual void setY(float value) override
Definition: CUSimpleObstacle.h:217
float x
Definition: CUVec2.h:66
virtual b2Body * getBody() override
Definition: CUSimpleObstacle.h:828
virtual bool isAwake() const override
Definition: CUSimpleObstacle.h:409
unsigned long _angFact
Definition: CUSimpleObstacle.h:75
float y
Definition: CUVec2.h:68
virtual void setBodyType(b2BodyType value) override
Definition: CUSimpleObstacle.h:132
virtual void setFixedRotation(bool value) override
Definition: CUSimpleObstacle.h:528
virtual float getAngle() const override
Definition: CUSimpleObstacle.h:232
virtual void setActive(bool value) override
Definition: CUSimpleObstacle.h:390
virtual bool activatePhysics(b2World &world) override
Definition: CUVec2.h:61
virtual void setAngularDamping(float value) override
Definition: CUSimpleObstacle.h:636
virtual void setPosition(const Vec2 &value) override
Definition: CUSimpleObstacle.h:165
virtual void setMass(float value) override
Definition: CUSimpleObstacle.h:799
virtual float getInertia() const override
Definition: CUSimpleObstacle.h:762
virtual void setSleepingAllowed(bool value) override
Definition: CUSimpleObstacle.h:458
b2MassData _massdata
Definition: CUObstacle.h:83
virtual void setInertia(float value) override
Definition: CUSimpleObstacle.h:774
int getAngleSnap()
Definition: CUSimpleObstacle.h:932
virtual float getVY() const override
Definition: CUSimpleObstacle.h:321
virtual void updateDebug() override
virtual void setPosition(float x, float y) override
Definition: CUSimpleObstacle.h:173
virtual Vec2 getPosition() const override
Definition: CUSimpleObstacle.h:149
virtual float getMass() const override
Definition: CUSimpleObstacle.h:788
virtual void setLinearVelocity(float x, float y) override
Definition: CUSimpleObstacle.h:282
virtual void setVX(float value) override
Definition: CUSimpleObstacle.h:308
virtual void setCentroid(const Vec2 &value) override
Definition: CUSimpleObstacle.h:739
int getPositionSnap()
Definition: CUSimpleObstacle.h:897
virtual void setVY(float value) override
Definition: CUSimpleObstacle.h:334
virtual void setAwake(bool value) override
Definition: CUSimpleObstacle.h:424
virtual void setCentroid(const Vec2 &value)
Definition: CUObstacle.h:715
virtual bool isBullet() const override
Definition: CUSimpleObstacle.h:482
int _posSnap
Definition: CUSimpleObstacle.h:69
virtual void deactivatePhysics(b2World &world) override
virtual float getAngularDamping() const override
Definition: CUSimpleObstacle.h:618
Definition: CUSimpleObstacle.h:63
virtual bool isActive() const override
Definition: CUSimpleObstacle.h:376
virtual void setFriction(float value) override
virtual void setGravityScale(float value) override
Definition: CUSimpleObstacle.h:556
virtual float getY() const override
Definition: CUSimpleObstacle.h:208
virtual float getVX() const override
Definition: CUSimpleObstacle.h:295
virtual void releaseFixtures()
Definition: CUSimpleObstacle.h:863
b2BodyDef _bodyinfo
Definition: CUObstacle.h:79
virtual void setMass(float value)
void setPositionSnap(unsigned int snap)
Definition: CUSimpleObstacle.h:913
virtual void setAngularVelocity(float value) override
Definition: CUSimpleObstacle.h:358
virtual void resetMass() override
Definition: CUSimpleObstacle.h:809
int _angSnap
Definition: CUSimpleObstacle.h:73
virtual Vec2 getCentroid() const override
Definition: CUSimpleObstacle.h:723
virtual float getGravityScale() const override
Definition: CUSimpleObstacle.h:544
virtual float getAngularVelocity() const override
Definition: CUSimpleObstacle.h:349
virtual ~SimpleObstacle()
Definition: CUSimpleObstacle.h:101
virtual void update(float delta) override
virtual float getLinearDamping() const override
Definition: CUSimpleObstacle.h:578
virtual bool isFixedRotation() const override
Definition: CUSimpleObstacle.h:517
Definition: CUObstacle.h:76
virtual void setBullet(bool value) override
Definition: CUSimpleObstacle.h:502
virtual void setRestitution(float value) override
SimpleObstacle()
Definition: CUSimpleObstacle.h:88
virtual void setLinearDamping(float value) override
Definition: CUSimpleObstacle.h:596
virtual void setAngle(float value) override
Definition: CUSimpleObstacle.h:241
virtual b2BodyType getBodyType() const override
Definition: CUSimpleObstacle.h:118
b2Body * _body
Definition: CUSimpleObstacle.h:66
void setAngleSnap(unsigned int snap)
Definition: CUSimpleObstacle.h:948
virtual Vec2 getLinearVelocity() const override
Definition: CUSimpleObstacle.h:258
Definition: CUAnimationNode.h:52
virtual void setSensor(bool value) override
virtual void setDensity(float value) override
virtual bool isSleepingAllowed() const override
Definition: CUSimpleObstacle.h:443
virtual void resetMass()
Definition: CUObstacle.h:766
virtual void setInertia(float value)
virtual void createFixtures()
Definition: CUSimpleObstacle.h:856
unsigned long _posFact
Definition: CUSimpleObstacle.h:71
virtual void setFilterData(b2Filter value) override
virtual float getX() const override
Definition: CUSimpleObstacle.h:186
virtual void setCentroid(float x, float y) override
Definition: CUSimpleObstacle.h:747
virtual void setLinearVelocity(const Vec2 &value) override
Definition: CUSimpleObstacle.h:274