box2drevolutejoint.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "box2drevolutejoint.h"
  21. #include "box2dworld.h"
  22. #include "box2dbody.h"
  23. #include <QDebug>
  24. Box2DRevoluteJoint::Box2DRevoluteJoint(QDeclarativeItem *parent) :
  25. Box2DJoint(parent),
  26. mRevoluteJointDef(),
  27. mRevoluteJoint(0),
  28. mOverrideLocalAnchorA(false)
  29. {
  30. }
  31. /*Box2DRevoluteJoint::~Box2DRevoluteJoint()
  32. {
  33. cleanup(world());
  34. }*/
  35. float Box2DRevoluteJoint::lowerAngle() const
  36. {
  37. return mRevoluteJointDef.lowerAngle;
  38. }
  39. void Box2DRevoluteJoint::setLowerAngle(float lowerAngle)
  40. {
  41. if (mRevoluteJointDef.lowerAngle == lowerAngle * b2_pi)
  42. return;
  43. mRevoluteJointDef.lowerAngle = lowerAngle * b2_pi;
  44. if (mRevoluteJoint)
  45. mRevoluteJoint->SetLimits(lowerAngle * b2_pi,
  46. mRevoluteJointDef.upperAngle);
  47. emit lowerAngleChanged();
  48. }
  49. float Box2DRevoluteJoint::upperAngle() const
  50. {
  51. return mRevoluteJointDef.upperAngle;
  52. }
  53. void Box2DRevoluteJoint::setUpperAngle(float upperAngle)
  54. {
  55. if (mRevoluteJointDef.upperAngle == upperAngle * b2_pi)
  56. return;
  57. mRevoluteJointDef.upperAngle = upperAngle * b2_pi;
  58. if (mRevoluteJoint)
  59. mRevoluteJoint->SetLimits(mRevoluteJointDef.lowerAngle,
  60. upperAngle * b2_pi);
  61. emit upperAngleChanged();
  62. }
  63. float Box2DRevoluteJoint::maxMotorTorque() const
  64. {
  65. return mRevoluteJointDef.maxMotorTorque;
  66. }
  67. void Box2DRevoluteJoint::setMaxMotorTorque(float maxMotorTorque)
  68. {
  69. if (mRevoluteJointDef.maxMotorTorque == maxMotorTorque)
  70. return;
  71. mRevoluteJointDef.maxMotorTorque = maxMotorTorque;
  72. if (mRevoluteJoint)
  73. mRevoluteJoint->SetMaxMotorTorque(maxMotorTorque);
  74. emit maxMotorTorqueChanged();
  75. }
  76. float Box2DRevoluteJoint::motorSpeed() const
  77. {
  78. return mRevoluteJointDef.motorSpeed;
  79. }
  80. void Box2DRevoluteJoint::setMotorSpeed(float motorSpeed)
  81. {
  82. if (mRevoluteJointDef.motorSpeed == motorSpeed)
  83. return;
  84. mRevoluteJointDef.motorSpeed = motorSpeed;
  85. if (mRevoluteJoint)
  86. mRevoluteJoint->SetMotorSpeed(motorSpeed);
  87. emit motorSpeedChanged();
  88. }
  89. bool Box2DRevoluteJoint::enableLimit() const
  90. {
  91. return mRevoluteJointDef.enableLimit;
  92. }
  93. void Box2DRevoluteJoint::setEnableLimit(bool enableLimit)
  94. {
  95. if (mRevoluteJointDef.enableLimit == enableLimit)
  96. return;
  97. mRevoluteJointDef.enableLimit = enableLimit;
  98. if (mRevoluteJoint)
  99. mRevoluteJoint->EnableLimit(enableLimit);
  100. emit enableLimitChanged();
  101. }
  102. bool Box2DRevoluteJoint::enableMotor() const
  103. {
  104. return mRevoluteJointDef.enableMotor;
  105. }
  106. void Box2DRevoluteJoint::setEnableMotor(bool enableMotor)
  107. {
  108. if (mRevoluteJointDef.enableMotor == enableMotor)
  109. return;
  110. mRevoluteJointDef.enableMotor = enableMotor;
  111. if (mRevoluteJoint)
  112. mRevoluteJoint->EnableMotor(enableMotor);
  113. emit enableMotorChanged();
  114. }
  115. QPointF Box2DRevoluteJoint::localAnchorA() const
  116. {
  117. if (mOverrideLocalAnchorA)
  118. return mLocalAnchorA;
  119. else
  120. return QPointF(mRevoluteJointDef.localAnchorA.x * scaleRatio,
  121. -mRevoluteJointDef.localAnchorA.y * scaleRatio);
  122. }
  123. void Box2DRevoluteJoint::setLocalAnchorA(const QPointF &localAnchorA)
  124. {
  125. if (mOverrideLocalAnchorA && mLocalAnchorA == localAnchorA)
  126. return;
  127. mOverrideLocalAnchorA = true;
  128. mLocalAnchorA = localAnchorA;
  129. emit localAnchorAChanged();
  130. }
  131. void Box2DRevoluteJoint::createJoint()
  132. {
  133. b2Vec2 anchor = mOverrideLocalAnchorA ?
  134. b2Vec2(mLocalAnchorA.x() / scaleRatio,
  135. -mLocalAnchorA.y() / scaleRatio) +
  136. bodyA()->body()->GetPosition() :
  137. bodyA()->body()->GetWorldCenter();
  138. mRevoluteJointDef.Initialize(bodyA()->body(), bodyB()->body(),
  139. anchor);
  140. mRevoluteJointDef.collideConnected = collideConnected();
  141. mRevoluteJoint = static_cast<b2RevoluteJoint*>
  142. (world()->CreateJoint(&mRevoluteJointDef));
  143. mInitializePending = false;
  144. }
  145. void Box2DRevoluteJoint::release()
  146. {
  147. if (!mReleased)
  148. mReleased = true;
  149. cleanup(world());
  150. }
  151. void Box2DRevoluteJoint::grab()
  152. {
  153. if (mReleased)
  154. mReleased = false;
  155. createJoint();
  156. }
  157. void Box2DRevoluteJoint::cleanup(b2World *world)
  158. {
  159. if (mRevoluteJoint && bodyA() && bodyB()) {
  160. mRevoluteJoint->SetUserData(0);
  161. world->DestroyJoint(mRevoluteJoint);
  162. mRevoluteJoint = 0;
  163. }
  164. }