VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Body.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BODY_H
18 #define BODY_H
19 
20 #include <QObject>
21 #include <QPointF>
22 #include <QQmlListProperty>
23 #include <QSet>
24 #include <QVariant>
25 #include <functional>
26 #include <queue>
27 
28 class Actor;
29 class Joint;
30 class b2Body;
31 class b2Fixture;
32 class b2Shape;
33 struct b2BodyDef;
34 struct b2FixtureDef;
35 
57 class Body : public QObject {
58  Q_OBJECT
59 
63  Q_PROPERTY(Actor* actor READ getActor)
64 
65 
69 
75  Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
76 
82  Q_PROPERTY(float linearDamping READ getLinearDamping WRITE setLinearDamping
84 
90  Q_PROPERTY(float angularDamping READ getAngularDamping WRITE setAngularDamping
92 
98  Q_PROPERTY(float gravityScale READ getGravityScale WRITE setGravityScale
100 
108  Q_PROPERTY(bool magnetic READ isMagnetic WRITE setMagnetic NOTIFY magneticChanged)
109 
113  Q_PROPERTY(float magneticStrength READ getMagneticStrength WRITE setMagneticStrength
115 
133  Q_PROPERTY(QPointF position READ getPosition WRITE setPosition NOTIFY positionSet)
134 
138  Q_PROPERTY(float angleInRadians READ getAngleInRadians WRITE setAngleInRadians
140 
148 
155  Q_PROPERTY(float angularVelocity READ getAngularVelocity WRITE setAngularVelocity
157 
168  Q_PROPERTY(bool sensor READ isSensor WRITE setSensor NOTIFY sensorChanged)
169 
173  Q_PROPERTY(float density READ getDensity WRITE setDensity NOTIFY densityChanged)
174 
178  Q_PROPERTY(float friction READ getFriction WRITE setFriction NOTIFY frictionChanged)
179 
183  Q_PROPERTY(float restitution READ getRestitution WRITE setRestitution NOTIFY restitutionChanged)
184 
189 
190 public:
191 
197  Q_ENUMS(BodyType)
198  enum BodyType {
199 
206 
213 
220  };
221 
225  explicit Body(QObject* parent = 0);
226 
230  virtual ~Body();
231 
235  Actor* getActor() const;
236 
240  BodyType getBodyType() const;
241 
246  void setBodyType(BodyType value);
247 
251  bool isActive() const;
252 
257  void setActive(bool value);
258 
262  float getLinearDamping() const;
263 
268  void setLinearDamping(float value);
269 
273  float getAngularDamping() const;
274 
279  void setAngularDamping(float value);
280 
284  float getGravityScale() const;
285 
290  void setGravityScale(float value);
291 
295  bool isMagnetic() const { return mMagnetic; }
296 
301  void setMagnetic(bool value);
302 
306  float getMagneticStrength() const { return mMagneticStrength; }
307 
312  void setMagneticStrength(float value);
313 
317  QPointF getPosition() const;
318 
329  void setPosition(const QPointF& value);
330 
334  float getAngleInRadians() const;
335 
340  void setAngleInRadians(float value);
341 
345  QPointF getLinearVelocity() const;
346 
351  void setLinearVelocity(const QPointF& value);
352 
356  float getAngularVelocity() const;
357 
362  void setAngularVelocity(float value);
363 
371  bool isSensor() const { return mIsSensor; }
372 
377  void setSensor(bool value);
378 
382  float getDensity() const { return mDensity; }
383 
388  void setDensity(float value);
389 
393  float getFriction() const { return mFriction; }
394 
399  void setFriction(float value);
400 
404  float getRestitution() const { return mRestitution; }
405 
410  void setRestitution(float value);
411 
415  const QList<Joint*>& getJoints() const { return mJoints; }
416 
421 
426  void setJoints(const QList<Joint*>& value);
427 
432  Q_INVOKABLE Joint* getJoint(int index) { return mJoints[index]; }
433 
437  Q_INVOKABLE int getJointCount() const { return mJoints.size(); }
438 
443  Q_INVOKABLE void addJoint(Joint* joint);
444 
449  Q_INVOKABLE void removeJoint(Joint* joint);
450 
454  Q_INVOKABLE void clearJoints();
455 
461  Q_INVOKABLE void clearAllAttachedJoints();
462 
466  b2Body* getBody() { return mBody; }
467 
473  Q_INVOKABLE void applyTorque(float torque);
474 
479  Q_INVOKABLE void applyForceToCenter(const QPointF& force);
480 
486  void invalidateTransform() { mTransformDirty = true; }
487 
493  virtual void updateBeforePhysics();
494 
500  virtual void updateAfterPhysics();
501 
506 
512  void beginContact(Body* otherBody, const QPointF& normal);
513 
519  void endContact(Body* otherBody, const QPointF& normal);
520 
525  virtual void forEachShape(const std::function<void(b2Shape*)>& func) { (void) func; }
526 
527 protected:
531  void invalidateFixtures() { mFixturesDirty = true; }
536  void forEachFixture(const std::function<void(b2Fixture*)>& func);
537 
538 signals:
539 
543  void bodyTypeChanged();
544 
548  void activeChanged();
549 
553  void linearDampingChanged();
554 
558  void angularDampingChanged();
559 
563  void gravityScaleChanged();
564 
568  void magneticChanged();
569 
574 
578  void positionSet();
579 
583  void angleInRadiansSet();
584 
588  void linearVelocitySet();
589 
593  void angularVelocitySet();
594 
598  void sensorChanged();
599 
603  void densityChanged();
604 
608  void frictionChanged();
609 
613  void restitutionChanged();
614 
618  void jointsChanged();
619 
627  void contactBegun(Body* otherBody, QPointF normal);
628 
636  void contactEnded(Body* otherBody, QPointF normal);
637 
646  void queuedContactBegun(Body* otherBody, QPointF normal);
647 
656  void queuedContactEnded(Body* otherBody, QPointF normal);
657 
658 private:
659  b2BodyDef getBodyDef();
660  b2FixtureDef getFixtureDef();
661  void addFixtures(b2FixtureDef* def);
662  void createFixtures();
663  void destroyFixtures();
664 
665  friend class Joint;
670  void registerAttachedJoint(Joint* joint);
671  void unregisterAttachedJoint(Joint* joint);
672 
673  // --------------------------------------------------------------------------------------
674  // Internal methods to implement QQmlListProperty<Joint*>
675  // --------------------------------------------------------------------------------------
676  static void appendJoint(QQmlListProperty<Joint>* property, Joint* value);
677  static Joint* jointAt(QQmlListProperty<Joint>* property, int index);
679  static int countJoints(QQmlListProperty<Joint>* property);
680 
681  mutable Actor* mActor = nullptr;
682 
683  b2Body* mBody = nullptr;
684  bool mFixturesDirty = true;
685  bool mTransformDirty = true;
686 
687  bool mMagnetic = false;
688  float mMagneticStrength = 1.0f;
689 
690  bool mIsSensor = false;
691  float mDensity = 1.0f;
692  float mFriction = 0.3f;
693  float mRestitution = 0.3f;
694 
695  QList<Joint*> mAttachedJoints;
696  QMap<Body*, int> mContactCountMap;
697  QList<Joint*> mJoints;
698 };
699 Q_DECLARE_METATYPE(Body*)
700 
701 #endif // BODY_H
Q_INVOKABLE void addJoint(Joint *joint)
Add a Joint to the Body.
void beginContact(Body *otherBody, const QPointF &normal)
Called when this Body has made contact with another.
float density
The density value for all of this Body's b2Fixtures.
Definition: Body.h:173
float getGravityScale() const
Returns gravityScale.
Q_INVOKABLE void clearAllAttachedJoints()
Destroys all Joints associated with this Body, whether or not this Body is the Joint's parent...
QQmlListProperty< Joint > joints
List of Joint objects associated with this Body.
Definition: Body.h:188
bool active
Whether or not the Body takes part in the physics simulation.
Definition: Body.h:75
void queuedContactEnded(Body *otherBody, QPointF normal)
Queued version of contactEnded().
float getAngleInRadians() const
Returns angleInRadians.
void bodyTypeChanged()
Emitted when bodyType changes.
void setLinearDamping(float value)
Sets linearDamping.
void magneticStrengthChanged()
Emitted when magneticStrength changes.
void setAngularVelocity(float value)
Sets angularVelocity.
float getDensity() const
Returns density.
Definition: Body.h:382
bool magnetic
Whether or not magnetism is set on for this Body.
Definition: Body.h:108
A QObject container for a Box2D b2Body.
Definition: Body.h:57
A movable Body that is affected by forces or collisions.
Definition: Body.h:219
Q_INVOKABLE Joint * getJoint(int index)
Returns a Joint at a given index.
Definition: Body.h:432
void frictionChanged()
Emitted when friction changes.
void invalidateTransform()
Marks the transform for this Body as dirty.
Definition: Body.h:486
float linearDamping
Dampens the linearVelocity of the Body.
Definition: Body.h:83
float magneticStrength
Intensity of the magnetic forces involving this Body.
Definition: Body.h:114
bool isMagnetic() const
Returns magnetic.
Definition: Body.h:295
Q_INVOKABLE void applyForceToCenter(const QPointF &force)
Apply force to the associated b2Body.
float getAngularVelocity() const
Returns angularVelocity.
Q_INVOKABLE void applyTorque(float torque)
Apply torque to the associated b2Body.
const QList< Joint * > & getJoints() const
Returns joints as a const QList&.
Definition: Body.h:415
float getMagneticStrength() const
Returns magneticStrength.
Definition: Body.h:306
void setActive(bool value)
Sets active.
void setAngleInRadians(float value)
Sets angleInRadians.
void positionSet()
Emitted when position is set.
void sensorChanged()
Emitted when sensor changes.
Q_INVOKABLE void removeJoint(Joint *joint)
Remove a Joint from the Body.
void angleInRadiansSet()
Emitted when angleInRadians is set.
float angularVelocity
The rate which the Bodys angle changes.
Definition: Body.h:156
QQmlListProperty< Joint > getJointListProperty()
Returns joints as a QQmlListProperty.
BodyType
Overarching category of how this Body interacts with the world.
Definition: Body.h:198
virtual void updateAfterPhysics()
Called immediately following the physics step.
void gravityScaleChanged()
Emitted when gravityScale changes.
int size() const
void setSensor(bool value)
Sets sensor.
QPointF getLinearVelocity() const
Returns linearVelocity.
void forEachFixture(const std::function< void(b2Fixture *)> &func)
Applies func to each of the Body's fixtures.
Body(QObject *parent=0)
Constructs a Body.
QVariant property(const char *name) const
void setAngularDamping(float value)
Sets angularDamping.
QPointF linearVelocity
The rate which the Bodys position changes.
Definition: Body.h:147
float angleInRadians
Rotational component of the b2Body's transform, in radians.
Definition: Body.h:139
void setMagnetic(bool value)
Sets magnetic.
Q_INVOKABLE int getJointCount() const
Returns the number of Joints associated with this Body.
Definition: Body.h:437
void contactEnded(Body *otherBody, QPointF normal)
Emitted when a contact ends.
void linearDampingChanged()
Emitted when linearDamping changes.
QPointF getPosition() const
Returns position.
virtual ~Body()
Destroys a Body.
void angularDampingChanged()
Emitted when angularDamping changes.
void setDensity(float value)
Sets density.
A non-movable Body.
Definition: Body.h:205
QList< Body * > getContactedBodies() const
Returns a QList containing the list of Bodys in contact with this Body.
bool sensor
The sensor value for all of this Body's b2Fixtures.
Definition: Body.h:168
QPointF position
Positional component of the b2Body's transform.
Definition: Body.h:133
void densityChanged()
Emitted when density changes.
bool isActive() const
Returns active.
void setBodyType(BodyType value)
Sets bodyType.
void setJoints(const QList< Joint * > &value)
Sets joints.
void setFriction(float value)
Sets friction.
void setLinearVelocity(const QPointF &value)
Sets linearVelocity.
float gravityScale
Coefficient on the continuous force of gravity.
Definition: Body.h:99
void setPosition(const QPointF &value)
Sets position.
void endContact(Body *otherBody, const QPointF &normal)
Called when this Body loses contact with another.
b2Body * getBody()
Returns the b2Body associated with this Body.
Definition: Body.h:466
float friction
The friction value for all of this Body's b2Fixtures.
Definition: Body.h:178
void invalidateFixtures()
Marks the Body's fixtures as dirty and that they need to be recreated.
Definition: Body.h:531
float getFriction() const
Returns friction.
Definition: Body.h:393
A movable Body that is not affected by forces or collisions.
Definition: Body.h:212
void setGravityScale(float value)
Sets gravityScale.
void restitutionChanged()
Emitted when restitution changes.
Q_INVOKABLE void clearJoints()
Clear all the Joints owned by this Body.
float getAngularDamping() const
Returns angularDamping.
Actor actor
Actor object associated with this Body. Always the parent in the item hierarchy.
Definition: Body.h:63
float angularDamping
Dampens the angularVelocity of the Body.
Definition: Body.h:91
void setRestitution(float value)
Sets restitution.
Representation of an entity within the Game scene.
Definition: Actor.h:40
BodyType bodyType
BodyType of this Body.
Definition: Body.h:68
float getLinearDamping() const
Returns linearDamping.
virtual void forEachShape(const std::function< void(b2Shape *)> &func)
Calls func on the complete set of b2Shapes that represent this Body.
Definition: Body.h:525
bool isSensor() const
Returns sensor.
Definition: Body.h:371
void setMagneticStrength(float value)
Sets magneticStrength.
void queuedContactBegun(Body *otherBody, QPointF normal)
Queued version of contactBegun().
void jointsChanged()
Emitted when joints changes.
A constraint between two Bodys.
Definition: Joint.h:28
virtual void updateBeforePhysics()
Called immediately preceding the physics step.
Actor * getActor() const
Returns actor.
QObject * parent() const
float getRestitution() const
Returns restitution.
Definition: Body.h:404
float restitution
The restitution value for all of this Body's b2Fixtures.
Definition: Body.h:183
void magneticChanged()
Emitted when magnetic changes.
void contactBegun(Body *otherBody, QPointF normal)
Emitted when a contact begins.
BodyType getBodyType() const
Returns bodyType.
void activeChanged()
Emitted when active changes.
void linearVelocitySet()
Emitted when linearVelocity is set.
void angularVelocitySet()
Emitted when angularVelocity is set.