box2dbody.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Box2D QML plugin
  3. * Copyright (C) 2010 Nokia Corporation
  4. *
  5. * This file is part of the Box2D QML plugin.
  6. *
  7. * This library is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. * License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef BOX2DBODY_H
  21. #define BOX2DBODY_H
  22. #include <QDeclarativeItem>
  23. class Box2DFixture;
  24. class Box2DWorld;
  25. class b2Body;
  26. class b2World;
  27. /**
  28. * The Box2D body, build up from a list of shapes.
  29. */
  30. class Box2DBody : public QDeclarativeItem
  31. {
  32. Q_OBJECT
  33. Q_ENUMS(BodyType)
  34. Q_PROPERTY(qreal linearDamping READ linearDamping WRITE setLinearDamping NOTIFY linearDampingChanged)
  35. Q_PROPERTY(qreal angularDamping READ angularDamping WRITE setAngularDamping NOTIFY angularDampingChanged)
  36. Q_PROPERTY(BodyType bodyType READ bodyType WRITE setBodyType NOTIFY bodyTypeChanged)
  37. Q_PROPERTY(bool bullet READ isBullet WRITE setBullet NOTIFY bulletChanged)
  38. Q_PROPERTY(bool sleepingAllowed READ sleepingAllowed WRITE setSleepingAllowed NOTIFY sleepingAllowedChanged)
  39. Q_PROPERTY(bool fixedRotation READ fixedRotation WRITE setFixedRotation NOTIFY fixedRotationChanged)
  40. Q_PROPERTY(bool active READ active WRITE setActive)
  41. Q_PROPERTY(QPointF linearVelocity READ linearVelocity WRITE setLinearVelocity NOTIFY linearVelocityChanged)
  42. Q_PROPERTY(float angularVelocity READ angularVelocity WRITE setAngularVelocity NOTIFY angularVelocityChanged)
  43. Q_PROPERTY(QDeclarativeListProperty<Box2DFixture> fixtures READ fixtures)
  44. public:
  45. enum BodyType {
  46. Static,
  47. Kinematic,
  48. Dynamic
  49. };
  50. explicit Box2DBody(QDeclarativeItem *parent = 0);
  51. ~Box2DBody();
  52. qreal linearDamping() const { return mLinearDamping; }
  53. void setLinearDamping(qreal linearDamping);
  54. qreal angularDamping() const { return mAngularDamping; }
  55. void setAngularDamping(qreal angularDamping);
  56. BodyType bodyType() const { return mBodyType; }
  57. void setBodyType(BodyType bodyType);
  58. bool isBullet() const { return mBullet; }
  59. void setBullet(bool bullet);
  60. bool sleepingAllowed() const { return mSleepingAllowed; }
  61. void setSleepingAllowed(bool allowed);
  62. bool fixedRotation() const { return mFixedRotation; }
  63. void setFixedRotation(bool fixedRotation);
  64. bool active() const { return mActive; }
  65. void setActive(bool active);
  66. QPointF linearVelocity() const { return mLinearVelocity; }
  67. void setLinearVelocity(const QPointF &linearVelocity);
  68. float angularVelocity() const { return mAngularVelocity; }
  69. void setAngularVelocity(float angularVelocity);
  70. void resetVelocities();
  71. QDeclarativeListProperty<Box2DFixture> fixtures();
  72. void initialize(b2World *world);
  73. void synchronize();
  74. void cleanup(b2World *world);
  75. Q_INVOKABLE void applyLinearImpulse(const QPointF &impulse,
  76. const QPointF &point);
  77. Q_INVOKABLE void applyTorque(qreal torque);
  78. Q_INVOKABLE QPointF getWorldCenter() const;
  79. void componentComplete();
  80. b2Body* body() const;
  81. protected:
  82. void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
  83. signals:
  84. void linearDampingChanged();
  85. void angularDampingChanged();
  86. void bodyTypeChanged();
  87. void bulletChanged();
  88. void sleepingAllowedChanged();
  89. void fixedRotationChanged();
  90. void linearVelocityChanged();
  91. void angularVelocityChanged();
  92. void bodyCreated();
  93. private slots:
  94. void onRotationChanged();
  95. private:
  96. static void append_fixture(QDeclarativeListProperty<Box2DFixture> *list,
  97. Box2DFixture *fixture);
  98. b2Body *mBody;
  99. b2World *mWorld;
  100. qreal mLinearDamping;
  101. qreal mAngularDamping;
  102. BodyType mBodyType;
  103. bool mBullet;
  104. bool mSleepingAllowed;
  105. bool mFixedRotation;
  106. bool mActive;
  107. QPointF mLinearVelocity;
  108. float mAngularVelocity;
  109. bool mSynchronizing;
  110. bool mInitializePending;
  111. QList<Box2DFixture*> mFixtures;
  112. };
  113. #endif // BOX2DBODY_H