b2Joint.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <Box2D/Dynamics/Joints/b2Joint.h>
  19. #include <Box2D/Dynamics/Joints/b2DistanceJoint.h>
  20. #include <Box2D/Dynamics/Joints/b2WheelJoint.h>
  21. #include <Box2D/Dynamics/Joints/b2MouseJoint.h>
  22. #include <Box2D/Dynamics/Joints/b2RevoluteJoint.h>
  23. #include <Box2D/Dynamics/Joints/b2PrismaticJoint.h>
  24. #include <Box2D/Dynamics/Joints/b2PulleyJoint.h>
  25. #include <Box2D/Dynamics/Joints/b2GearJoint.h>
  26. #include <Box2D/Dynamics/Joints/b2WeldJoint.h>
  27. #include <Box2D/Dynamics/Joints/b2FrictionJoint.h>
  28. #include <Box2D/Dynamics/Joints/b2RopeJoint.h>
  29. #include <Box2D/Dynamics/Joints/b2MotorJoint.h>
  30. #include <Box2D/Dynamics/b2Body.h>
  31. #include <Box2D/Dynamics/b2World.h>
  32. #include <Box2D/Common/b2BlockAllocator.h>
  33. #include <Box2D/b2ObjectDestroyNotifier.h>
  34. #include <new>
  35. b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
  36. {
  37. b2Joint* joint = NULL;
  38. switch (def->type)
  39. {
  40. case e_distanceJoint:
  41. {
  42. void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
  43. joint = new (mem) b2DistanceJoint(static_cast<const b2DistanceJointDef*>(def));
  44. }
  45. break;
  46. case e_mouseJoint:
  47. {
  48. void* mem = allocator->Allocate(sizeof(b2MouseJoint));
  49. joint = new (mem) b2MouseJoint(static_cast<const b2MouseJointDef*>(def));
  50. }
  51. break;
  52. case e_prismaticJoint:
  53. {
  54. void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
  55. joint = new (mem) b2PrismaticJoint(static_cast<const b2PrismaticJointDef*>(def));
  56. }
  57. break;
  58. case e_revoluteJoint:
  59. {
  60. void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
  61. joint = new (mem) b2RevoluteJoint(static_cast<const b2RevoluteJointDef*>(def));
  62. }
  63. break;
  64. case e_pulleyJoint:
  65. {
  66. void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
  67. joint = new (mem) b2PulleyJoint(static_cast<const b2PulleyJointDef*>(def));
  68. }
  69. break;
  70. case e_gearJoint:
  71. {
  72. void* mem = allocator->Allocate(sizeof(b2GearJoint));
  73. joint = new (mem) b2GearJoint(static_cast<const b2GearJointDef*>(def));
  74. }
  75. break;
  76. case e_wheelJoint:
  77. {
  78. void* mem = allocator->Allocate(sizeof(b2WheelJoint));
  79. joint = new (mem) b2WheelJoint(static_cast<const b2WheelJointDef*>(def));
  80. }
  81. break;
  82. case e_weldJoint:
  83. {
  84. void* mem = allocator->Allocate(sizeof(b2WeldJoint));
  85. joint = new (mem) b2WeldJoint(static_cast<const b2WeldJointDef*>(def));
  86. }
  87. break;
  88. case e_frictionJoint:
  89. {
  90. void* mem = allocator->Allocate(sizeof(b2FrictionJoint));
  91. joint = new (mem) b2FrictionJoint(static_cast<const b2FrictionJointDef*>(def));
  92. }
  93. break;
  94. case e_ropeJoint:
  95. {
  96. void* mem = allocator->Allocate(sizeof(b2RopeJoint));
  97. joint = new (mem) b2RopeJoint(static_cast<const b2RopeJointDef*>(def));
  98. }
  99. break;
  100. case e_motorJoint:
  101. {
  102. void* mem = allocator->Allocate(sizeof(b2MotorJoint));
  103. joint = new (mem) b2MotorJoint(static_cast<const b2MotorJointDef*>(def));
  104. }
  105. break;
  106. default:
  107. b2Assert(false);
  108. break;
  109. }
  110. return joint;
  111. }
  112. void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
  113. {
  114. b2NotifyObjectDestroyed(joint, b2ObjectType::JOIN, typeid(*joint).name());
  115. joint->~b2Joint();
  116. switch (joint->m_type)
  117. {
  118. case e_distanceJoint:
  119. allocator->Free(joint, sizeof(b2DistanceJoint));
  120. break;
  121. case e_mouseJoint:
  122. allocator->Free(joint, sizeof(b2MouseJoint));
  123. break;
  124. case e_prismaticJoint:
  125. allocator->Free(joint, sizeof(b2PrismaticJoint));
  126. break;
  127. case e_revoluteJoint:
  128. allocator->Free(joint, sizeof(b2RevoluteJoint));
  129. break;
  130. case e_pulleyJoint:
  131. allocator->Free(joint, sizeof(b2PulleyJoint));
  132. break;
  133. case e_gearJoint:
  134. allocator->Free(joint, sizeof(b2GearJoint));
  135. break;
  136. case e_wheelJoint:
  137. allocator->Free(joint, sizeof(b2WheelJoint));
  138. break;
  139. case e_weldJoint:
  140. allocator->Free(joint, sizeof(b2WeldJoint));
  141. break;
  142. case e_frictionJoint:
  143. allocator->Free(joint, sizeof(b2FrictionJoint));
  144. break;
  145. case e_ropeJoint:
  146. allocator->Free(joint, sizeof(b2RopeJoint));
  147. break;
  148. case e_motorJoint:
  149. allocator->Free(joint, sizeof(b2MotorJoint));
  150. break;
  151. default:
  152. b2Assert(false);
  153. break;
  154. }
  155. }
  156. b2Joint::b2Joint(const b2JointDef* def)
  157. {
  158. b2Assert(def->bodyA != def->bodyB);
  159. m_type = def->type;
  160. m_prev = NULL;
  161. m_next = NULL;
  162. m_bodyA = def->bodyA;
  163. m_bodyB = def->bodyB;
  164. m_index = 0;
  165. m_collideConnected = def->collideConnected;
  166. m_islandFlag = false;
  167. m_userData = def->userData;
  168. m_edgeA.joint = NULL;
  169. m_edgeA.other = NULL;
  170. m_edgeA.prev = NULL;
  171. m_edgeA.next = NULL;
  172. m_edgeB.joint = NULL;
  173. m_edgeB.other = NULL;
  174. m_edgeB.prev = NULL;
  175. m_edgeB.next = NULL;
  176. }
  177. bool b2Joint::IsActive() const
  178. {
  179. return m_bodyA->IsActive() && m_bodyB->IsActive();
  180. }