PrismaticJoint.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "PrismaticJoint.h"
  21. // Module
  22. #include "Body.h"
  23. #include "World.h"
  24. #include "Physics.h"
  25. namespace love
  26. {
  27. namespace physics
  28. {
  29. namespace box2d
  30. {
  31. love::Type PrismaticJoint::type("PrismaticJoint", &Joint::type);
  32. PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected)
  33. : Joint(body1, body2)
  34. , joint(NULL)
  35. {
  36. b2PrismaticJointDef def;
  37. init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
  38. joint = (b2PrismaticJoint *)createJoint(&def);
  39. }
  40. PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle)
  41. : Joint(body1, body2)
  42. , joint(NULL)
  43. {
  44. b2PrismaticJointDef def;
  45. init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
  46. def.referenceAngle = referenceAngle;
  47. joint = (b2PrismaticJoint *)createJoint(&def);
  48. }
  49. PrismaticJoint::~PrismaticJoint()
  50. {
  51. }
  52. void PrismaticJoint::init(b2PrismaticJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected)
  53. {
  54. def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA)), b2Vec2(ax,ay));
  55. def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB)));
  56. def.lowerTranslation = 0.0f;
  57. def.upperTranslation = 100.0f;
  58. def.enableLimit = true;
  59. def.collideConnected = collideConnected;
  60. }
  61. float PrismaticJoint::getJointTranslation() const
  62. {
  63. return Physics::scaleUp(joint->GetJointTranslation());
  64. }
  65. float PrismaticJoint::getJointSpeed() const
  66. {
  67. return Physics::scaleUp(joint->GetJointSpeed());
  68. }
  69. void PrismaticJoint::setMotorEnabled(bool enable)
  70. {
  71. return joint->EnableMotor(enable);
  72. }
  73. bool PrismaticJoint::isMotorEnabled() const
  74. {
  75. return joint->IsMotorEnabled();
  76. }
  77. void PrismaticJoint::setMaxMotorForce(float force)
  78. {
  79. joint->SetMaxMotorForce(Physics::scaleDown(force));
  80. }
  81. void PrismaticJoint::setMotorSpeed(float speed)
  82. {
  83. joint->SetMotorSpeed(Physics::scaleDown(speed));
  84. }
  85. float PrismaticJoint::getMotorSpeed() const
  86. {
  87. return Physics::scaleUp(joint->GetMotorSpeed());
  88. }
  89. float PrismaticJoint::getMotorForce(float inv_dt) const
  90. {
  91. return Physics::scaleUp(joint->GetMotorForce(inv_dt));
  92. }
  93. float PrismaticJoint::getMaxMotorForce() const
  94. {
  95. return Physics::scaleUp(joint->GetMaxMotorForce());
  96. }
  97. void PrismaticJoint::setLimitsEnabled(bool enable)
  98. {
  99. joint->EnableLimit(enable);
  100. }
  101. bool PrismaticJoint::areLimitsEnabled() const
  102. {
  103. return joint->IsLimitEnabled();
  104. }
  105. void PrismaticJoint::setUpperLimit(float limit)
  106. {
  107. joint->SetLimits(joint->GetLowerLimit(), Physics::scaleDown(limit));
  108. }
  109. void PrismaticJoint::setLowerLimit(float limit)
  110. {
  111. joint->SetLimits(Physics::scaleDown(limit), joint->GetUpperLimit());
  112. }
  113. void PrismaticJoint::setLimits(float lower, float upper)
  114. {
  115. joint->SetLimits(Physics::scaleDown(lower), Physics::scaleDown(upper));
  116. }
  117. float PrismaticJoint::getLowerLimit() const
  118. {
  119. return Physics::scaleUp(joint->GetLowerLimit());
  120. }
  121. float PrismaticJoint::getUpperLimit() const
  122. {
  123. return Physics::scaleUp(joint->GetUpperLimit());
  124. }
  125. int PrismaticJoint::getLimits(lua_State *L)
  126. {
  127. lua_pushnumber(L, Physics::scaleUp(joint->GetLowerLimit()));
  128. lua_pushnumber(L, Physics::scaleUp(joint->GetUpperLimit()));
  129. return 2;
  130. }
  131. int PrismaticJoint::getAxis(lua_State *L)
  132. {
  133. b2Vec2 axis = joint->GetLocalAxisA();
  134. getBodyA()->getWorldVector(axis.x, axis.y, axis.x, axis.y);
  135. lua_pushnumber(L, axis.x);
  136. lua_pushnumber(L, axis.y);
  137. return 2;
  138. }
  139. float PrismaticJoint::getReferenceAngle() const
  140. {
  141. return joint->GetReferenceAngle();
  142. }
  143. } // box2d
  144. } // physics
  145. } // love