MouseJoint.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2006-2011 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 "MouseJoint.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. MouseJoint::MouseJoint(Body * body1, float x, float y)
  32. : Joint(body1), joint(NULL)
  33. {
  34. b2MouseJointDef def;
  35. def.bodyA = body1->world->getGroundBody();
  36. def.bodyB = body1->body;
  37. def.maxForce = 1000.0f * body1->body->GetMass();
  38. def.target = Physics::scaleDown(b2Vec2(x,y));
  39. joint = (b2MouseJoint*)createJoint(&def);
  40. }
  41. MouseJoint::~MouseJoint()
  42. {
  43. }
  44. void MouseJoint::setTarget(float x, float y)
  45. {
  46. joint->SetTarget(Physics::scaleDown(b2Vec2(x, y)));
  47. }
  48. int MouseJoint::getTarget(lua_State * L)
  49. {
  50. lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().x));
  51. lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().y));
  52. return 2;
  53. }
  54. void MouseJoint::setMaxForce(float force)
  55. {
  56. joint->SetMaxForce(Physics::scaleDown(force));
  57. }
  58. float MouseJoint::getMaxForce() const
  59. {
  60. return Physics::scaleUp(joint->GetMaxForce());
  61. }
  62. void MouseJoint::setFrequency(float hz)
  63. {
  64. joint->SetFrequency(hz);
  65. }
  66. float MouseJoint::getFrequency() const
  67. {
  68. return joint->GetFrequency();
  69. }
  70. void MouseJoint::setDampingRatio(float d)
  71. {
  72. joint->SetDampingRatio(d);
  73. }
  74. float MouseJoint::getDampingRatio() const
  75. {
  76. return joint->GetDampingRatio();
  77. }
  78. } // box2d
  79. } // physics
  80. } // love