The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.7KB

  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
  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. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "b2Joint.h"
  19. #include "b2DistanceJoint.h"
  20. #include "b2WheelJoint.h"
  21. #include "b2MouseJoint.h"
  22. #include "b2RevoluteJoint.h"
  23. #include "b2PrismaticJoint.h"
  24. #include "b2PulleyJoint.h"
  25. #include "b2GearJoint.h"
  26. #include "b2WeldJoint.h"
  27. #include "b2FrictionJoint.h"
  28. #include "b2RopeJoint.h"
  29. #include "../b2Body.h"
  30. #include "../b2World.h"
  31. #include "../../Common/b2BlockAllocator.h"
  32. #include <new>
  33. b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
  34. {
  35. b2Joint* joint = NULL;
  36. switch (def->type)
  37. {
  38. case e_distanceJoint:
  39. {
  40. void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
  41. joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
  42. }
  43. break;
  44. case e_mouseJoint:
  45. {
  46. void* mem = allocator->Allocate(sizeof(b2MouseJoint));
  47. joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
  48. }
  49. break;
  50. case e_prismaticJoint:
  51. {
  52. void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
  53. joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
  54. }
  55. break;
  56. case e_revoluteJoint:
  57. {
  58. void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
  59. joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
  60. }
  61. break;
  62. case e_pulleyJoint:
  63. {
  64. void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
  65. joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
  66. }
  67. break;
  68. case e_gearJoint:
  69. {
  70. void* mem = allocator->Allocate(sizeof(b2GearJoint));
  71. joint = new (mem) b2GearJoint((b2GearJointDef*)def);
  72. }
  73. break;
  74. case e_wheelJoint:
  75. {
  76. void* mem = allocator->Allocate(sizeof(b2WheelJoint));
  77. joint = new (mem) b2WheelJoint((b2WheelJointDef*)def);
  78. }
  79. break;
  80. case e_weldJoint:
  81. {
  82. void* mem = allocator->Allocate(sizeof(b2WeldJoint));
  83. joint = new (mem) b2WeldJoint((b2WeldJointDef*)def);
  84. }
  85. break;
  86. case e_frictionJoint:
  87. {
  88. void* mem = allocator->Allocate(sizeof(b2FrictionJoint));
  89. joint = new (mem) b2FrictionJoint((b2FrictionJointDef*)def);
  90. }
  91. break;
  92. case e_ropeJoint:
  93. {
  94. void* mem = allocator->Allocate(sizeof(b2RopeJoint));
  95. joint = new (mem) b2RopeJoint((b2RopeJointDef*)def);
  96. }
  97. break;
  98. default:
  99. b2Assert(false);
  100. break;
  101. }
  102. return joint;
  103. }
  104. void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
  105. {
  106. joint->~b2Joint();
  107. switch (joint->m_type)
  108. {
  109. case e_distanceJoint:
  110. allocator->Free(joint, sizeof(b2DistanceJoint));
  111. break;
  112. case e_mouseJoint:
  113. allocator->Free(joint, sizeof(b2MouseJoint));
  114. break;
  115. case e_prismaticJoint:
  116. allocator->Free(joint, sizeof(b2PrismaticJoint));
  117. break;
  118. case e_revoluteJoint:
  119. allocator->Free(joint, sizeof(b2RevoluteJoint));
  120. break;
  121. case e_pulleyJoint:
  122. allocator->Free(joint, sizeof(b2PulleyJoint));
  123. break;
  124. case e_gearJoint:
  125. allocator->Free(joint, sizeof(b2GearJoint));
  126. break;
  127. case e_wheelJoint:
  128. allocator->Free(joint, sizeof(b2WheelJoint));
  129. break;
  130. case e_weldJoint:
  131. allocator->Free(joint, sizeof(b2WeldJoint));
  132. break;
  133. case e_frictionJoint:
  134. allocator->Free(joint, sizeof(b2FrictionJoint));
  135. break;
  136. case e_ropeJoint:
  137. allocator->Free(joint, sizeof(b2RopeJoint));
  138. break;
  139. default:
  140. b2Assert(false);
  141. break;
  142. }
  143. }
  144. b2Joint::b2Joint(const b2JointDef* def)
  145. {
  146. b2Assert(def->bodyA != def->bodyB);
  147. m_type = def->type;
  148. m_prev = NULL;
  149. m_next = NULL;
  150. m_bodyA = def->bodyA;
  151. m_bodyB = def->bodyB;
  152. m_index = 0;
  153. m_collideConnected = def->collideConnected;
  154. m_islandFlag = false;
  155. m_userData = def->userData;
  156. m_edgeA.joint = NULL;
  157. m_edgeA.other = NULL;
  158. m_edgeA.prev = NULL;
  159. m_edgeA.next = NULL;
  160. m_edgeB.joint = NULL;
  161. m_edgeB.other = NULL;
  162. m_edgeB.prev = NULL;
  163. m_edgeB.next = NULL;
  164. }
  165. bool b2Joint::IsActive() const
  166. {
  167. return m_bodyA->IsActive() && m_bodyB->IsActive();
  168. }