b2Joint.cpp 5.2 KB

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