box2dfixture.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include "box2dfixture.h"
  21. #include "box2dworld.h"
  22. #include <QDebug>
  23. Box2DFixture::Box2DFixture(QDeclarativeItem *parent) :
  24. QDeclarativeItem(parent),
  25. mFixtureDef(),
  26. mFixture(0)
  27. {
  28. }
  29. float Box2DFixture::density() const
  30. {
  31. return mFixtureDef.density;
  32. }
  33. void Box2DFixture::setDensity(float density)
  34. {
  35. if (mFixtureDef.density == density)
  36. return;
  37. mFixtureDef.density = density;
  38. if (mFixture)
  39. mFixture->SetDensity(density);
  40. emit densityChanged();
  41. }
  42. float Box2DFixture::friction() const
  43. {
  44. return mFixtureDef.friction;
  45. }
  46. void Box2DFixture::setFriction(float friction)
  47. {
  48. if (mFixtureDef.friction == friction)
  49. return;
  50. mFixtureDef.friction = friction;
  51. if (mFixture)
  52. mFixture->SetFriction(friction);
  53. emit frictionChanged();
  54. }
  55. float Box2DFixture::restitution() const
  56. {
  57. return mFixtureDef.restitution;
  58. }
  59. void Box2DFixture::setRestitution(float restitution)
  60. {
  61. if (mFixtureDef.restitution == restitution)
  62. return;
  63. mFixtureDef.restitution = restitution;
  64. if (mFixture)
  65. mFixture->SetRestitution(restitution);
  66. emit restitutionChanged();
  67. }
  68. bool Box2DFixture::isSensor() const
  69. {
  70. return mFixtureDef.isSensor;
  71. }
  72. void Box2DFixture::setSensor(bool sensor)
  73. {
  74. if (mFixtureDef.isSensor == sensor)
  75. return;
  76. mFixtureDef.isSensor = sensor;
  77. if (mFixture)
  78. mFixture->SetSensor(sensor);
  79. emit sensorChanged();
  80. }
  81. Box2DFixture::CategoryFlags Box2DFixture::categories() const
  82. {
  83. return CategoryFlags(mFixtureDef.filter.categoryBits);
  84. }
  85. void Box2DFixture::setCategories(CategoryFlags layers)
  86. {
  87. if (mFixtureDef.filter.categoryBits == layers)
  88. return;
  89. mFixtureDef.filter.categoryBits = layers;
  90. emit categoriesChanged();
  91. }
  92. Box2DFixture::CategoryFlags Box2DFixture::collidesWith() const
  93. {
  94. return CategoryFlags(mFixtureDef.filter.maskBits);
  95. }
  96. void Box2DFixture::setCollidesWith(CategoryFlags layers)
  97. {
  98. if (mFixtureDef.filter.maskBits == layers)
  99. return;
  100. mFixtureDef.filter.maskBits = layers;
  101. emit collidesWithChanged();
  102. }
  103. int Box2DFixture::groupIndex() const
  104. {
  105. return mFixtureDef.filter.groupIndex;
  106. }
  107. void Box2DFixture::setGroupIndex(int groupIndex)
  108. {
  109. if (mFixtureDef.filter.groupIndex == groupIndex)
  110. return;
  111. mFixtureDef.filter.groupIndex = groupIndex;
  112. emit groupIndexChanged();
  113. }
  114. void Box2DFixture::createFixture(b2Body *body)
  115. {
  116. b2Shape *shape = createShape();
  117. if (!shape)
  118. return;
  119. mFixtureDef.shape = shape;
  120. mFixture = body->CreateFixture(&mFixtureDef);
  121. mFixture->SetUserData(this);
  122. delete shape;
  123. }
  124. void Box2DFixture::emitBeginContact(Box2DFixture *other)
  125. {
  126. emit beginContact(other);
  127. }
  128. void Box2DFixture::emitContactChanged(Box2DFixture *other)
  129. {
  130. emit contactChanged(other);
  131. }
  132. void Box2DFixture::emitEndContact(Box2DFixture *other)
  133. {
  134. emit endContact(other);
  135. }
  136. b2Shape *Box2DBox::createShape()
  137. {
  138. const float32 _x = x() / scaleRatio;
  139. const float32 _y = -y() / scaleRatio;
  140. b2Vec2 vertices[4];
  141. vertices[0].Set(_x, _y);
  142. vertices[1].Set(_x, _y - height() / scaleRatio);
  143. vertices[2].Set(_x + width() / scaleRatio, _y - height() / scaleRatio);
  144. vertices[3].Set(_x + width() / scaleRatio, _y);
  145. int32 count = 4;
  146. b2PolygonShape *shape = new b2PolygonShape;
  147. shape->Set(vertices, count);
  148. return shape;
  149. }
  150. b2Shape *Box2DCircle::createShape()
  151. {
  152. b2CircleShape *shape = new b2CircleShape;
  153. shape->m_radius = mRadius / scaleRatio;
  154. shape->m_p.Set(x() / scaleRatio, -y() / scaleRatio);
  155. return shape;
  156. }
  157. void Box2DCircle::setRadius(float radius)
  158. {
  159. if (mRadius == radius)
  160. return;
  161. mRadius = radius;
  162. b2CircleShape *shape = static_cast<b2CircleShape*>(getShape());
  163. if (shape) {
  164. shape->m_radius = mRadius / scaleRatio; // update 2B engine
  165. shape->m_p.Set(x() / scaleRatio, -y() / scaleRatio);
  166. }
  167. emit radiusChanged();
  168. }
  169. b2Shape *Box2DPolygon::createShape()
  170. {
  171. const int count = mVertices.length();
  172. if (count < 2 || count > b2_maxPolygonVertices) {
  173. qWarning() << "Polygon: Invalid number of vertices:" << count;
  174. return 0;
  175. }
  176. b2Vec2 vertices[count];
  177. for (int i = 0; i < count; ++i) {
  178. const QPointF &point = mVertices.at(i).toPointF();
  179. vertices[i].Set(point.x() / scaleRatio, -point.y() / scaleRatio);
  180. }
  181. b2PolygonShape *shape = new b2PolygonShape;
  182. shape->Set(vertices, count);
  183. return shape;
  184. }