box2dprismaticjoint.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "box2dprismaticjoint.h"
  21. #include "box2dworld.h"
  22. #include "box2dbody.h"
  23. Box2DPrismaticJoint::Box2DPrismaticJoint(QDeclarativeItem *parent) :
  24. Box2DJoint(parent),
  25. mPrismaticJointDef(),
  26. mPrismaticJoint(0)
  27. {
  28. }
  29. float Box2DPrismaticJoint::lowerTranslation() const
  30. {
  31. return mPrismaticJointDef.lowerTranslation;
  32. }
  33. void Box2DPrismaticJoint::setLowerTranslation(float lowerTranslation)
  34. {
  35. if (mPrismaticJointDef.lowerTranslation == lowerTranslation)
  36. return;
  37. mPrismaticJointDef.lowerTranslation = lowerTranslation;
  38. if (mPrismaticJoint)
  39. mPrismaticJoint->SetLimits(lowerTranslation, mPrismaticJointDef.upperTranslation);
  40. emit lowerTranslationChanged();
  41. }
  42. float Box2DPrismaticJoint::upperTranslation() const
  43. {
  44. return mPrismaticJointDef.upperTranslation;
  45. }
  46. void Box2DPrismaticJoint::setUpperTranslation(float upperTranslation)
  47. {
  48. if (mPrismaticJointDef.upperTranslation == upperTranslation)
  49. return;
  50. mPrismaticJointDef.upperTranslation = upperTranslation;
  51. if (mPrismaticJoint)
  52. mPrismaticJoint->SetLimits(mPrismaticJointDef.lowerTranslation, upperTranslation);
  53. emit upperTranslationChanged();
  54. }
  55. float Box2DPrismaticJoint::maxMotorForce() const
  56. {
  57. return mPrismaticJointDef.maxMotorForce;
  58. }
  59. void Box2DPrismaticJoint::setMaxMotorForce(float maxMotorForce)
  60. {
  61. if (mPrismaticJointDef.maxMotorForce == maxMotorForce)
  62. return;
  63. mPrismaticJointDef.maxMotorForce = maxMotorForce;
  64. if (mPrismaticJoint)
  65. mPrismaticJoint->SetMaxMotorForce(maxMotorForce);
  66. emit maxMotorForceChanged();
  67. }
  68. float Box2DPrismaticJoint::motorSpeed() const
  69. {
  70. return mPrismaticJointDef.motorSpeed;
  71. }
  72. void Box2DPrismaticJoint::setMotorSpeed(float motorSpeed)
  73. {
  74. if (mPrismaticJointDef.motorSpeed == motorSpeed)
  75. return;
  76. mPrismaticJointDef.motorSpeed = motorSpeed;
  77. if (mPrismaticJoint)
  78. mPrismaticJoint->SetMotorSpeed(motorSpeed);
  79. emit motorSpeedChanged();
  80. }
  81. bool Box2DPrismaticJoint::enableLimit() const
  82. {
  83. return mPrismaticJointDef.enableLimit;
  84. }
  85. void Box2DPrismaticJoint::setEnableLimit(bool enableLimit)
  86. {
  87. if (mPrismaticJointDef.enableLimit == enableLimit)
  88. return;
  89. mPrismaticJointDef.enableLimit = enableLimit;
  90. if (mPrismaticJoint)
  91. mPrismaticJoint->EnableLimit(enableLimit);
  92. emit enableLimitChanged();
  93. }
  94. bool Box2DPrismaticJoint::enableMotor() const
  95. {
  96. return mPrismaticJointDef.enableMotor;
  97. }
  98. void Box2DPrismaticJoint::setEnableMotor(bool enableMotor)
  99. {
  100. if (mPrismaticJointDef.enableMotor == enableMotor)
  101. return;
  102. mPrismaticJointDef.enableMotor = enableMotor;
  103. if (mPrismaticJoint)
  104. mPrismaticJoint->EnableMotor(enableMotor);
  105. emit enableMotorChanged();
  106. }
  107. QPointF Box2DPrismaticJoint::axis() const
  108. {
  109. return QPointF(mPrismaticJointDef.localAxis1.x,
  110. -mPrismaticJointDef.localAxis1.y);
  111. }
  112. void Box2DPrismaticJoint::setAxis(const QPointF &axis)
  113. {
  114. if (mPrismaticJointDef.localAxis1 == b2Vec2(axis.x(), -axis.y()))
  115. return;
  116. mPrismaticJointDef.localAxis1 = b2Vec2(axis.x(), -axis.y());
  117. emit axisChanged();
  118. }
  119. void Box2DPrismaticJoint::createJoint()
  120. {
  121. mPrismaticJointDef.Initialize(bodyA()->body(), bodyB()->body(),
  122. bodyA()->body()->GetWorldCenter(), mPrismaticJointDef.localAxis1);
  123. mPrismaticJointDef.collideConnected = collideConnected();
  124. mPrismaticJoint = static_cast<b2PrismaticJoint*>(world()->CreateJoint(&mPrismaticJointDef));
  125. mInitializePending = false;
  126. }