/Code/Angel/Libraries/Box2D-2.1.2/Box2D/Dynamics/Joints/b2Joint.cpp

http://angel-engine.googlecode.com/ · C++ · 186 lines · 140 code · 29 blank · 17 comment · 4 complexity · 4ccfefe12112d0253b3e90fdcd1dbba5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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/b2LineJoint.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/b2Body.h>
  29. #include <Box2D/Dynamics/b2World.h>
  30. #include <Box2D/Common/b2BlockAllocator.h>
  31. #include <new>
  32. b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
  33. {
  34. b2Joint* joint = NULL;
  35. switch (def->type)
  36. {
  37. case e_distanceJoint:
  38. {
  39. void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
  40. joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
  41. }
  42. break;
  43. case e_mouseJoint:
  44. {
  45. void* mem = allocator->Allocate(sizeof(b2MouseJoint));
  46. joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
  47. }
  48. break;
  49. case e_prismaticJoint:
  50. {
  51. void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
  52. joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
  53. }
  54. break;
  55. case e_revoluteJoint:
  56. {
  57. void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
  58. joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
  59. }
  60. break;
  61. case e_pulleyJoint:
  62. {
  63. void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
  64. joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
  65. }
  66. break;
  67. case e_gearJoint:
  68. {
  69. void* mem = allocator->Allocate(sizeof(b2GearJoint));
  70. joint = new (mem) b2GearJoint((b2GearJointDef*)def);
  71. }
  72. break;
  73. case e_lineJoint:
  74. {
  75. void* mem = allocator->Allocate(sizeof(b2LineJoint));
  76. joint = new (mem) b2LineJoint((b2LineJointDef*)def);
  77. }
  78. break;
  79. case e_weldJoint:
  80. {
  81. void* mem = allocator->Allocate(sizeof(b2WeldJoint));
  82. joint = new (mem) b2WeldJoint((b2WeldJointDef*)def);
  83. }
  84. break;
  85. case e_frictionJoint:
  86. {
  87. void* mem = allocator->Allocate(sizeof(b2FrictionJoint));
  88. joint = new (mem) b2FrictionJoint((b2FrictionJointDef*)def);
  89. }
  90. break;
  91. default:
  92. b2Assert(false);
  93. break;
  94. }
  95. return joint;
  96. }
  97. void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
  98. {
  99. joint->~b2Joint();
  100. switch (joint->m_type)
  101. {
  102. case e_distanceJoint:
  103. allocator->Free(joint, sizeof(b2DistanceJoint));
  104. break;
  105. case e_mouseJoint:
  106. allocator->Free(joint, sizeof(b2MouseJoint));
  107. break;
  108. case e_prismaticJoint:
  109. allocator->Free(joint, sizeof(b2PrismaticJoint));
  110. break;
  111. case e_revoluteJoint:
  112. allocator->Free(joint, sizeof(b2RevoluteJoint));
  113. break;
  114. case e_pulleyJoint:
  115. allocator->Free(joint, sizeof(b2PulleyJoint));
  116. break;
  117. case e_gearJoint:
  118. allocator->Free(joint, sizeof(b2GearJoint));
  119. break;
  120. case e_lineJoint:
  121. allocator->Free(joint, sizeof(b2LineJoint));
  122. break;
  123. case e_weldJoint:
  124. allocator->Free(joint, sizeof(b2WeldJoint));
  125. break;
  126. case e_frictionJoint:
  127. allocator->Free(joint, sizeof(b2FrictionJoint));
  128. break;
  129. default:
  130. b2Assert(false);
  131. break;
  132. }
  133. }
  134. b2Joint::b2Joint(const b2JointDef* def)
  135. {
  136. b2Assert(def->bodyA != def->bodyB);
  137. m_type = def->type;
  138. m_prev = NULL;
  139. m_next = NULL;
  140. m_bodyA = def->bodyA;
  141. m_bodyB = def->bodyB;
  142. m_collideConnected = def->collideConnected;
  143. m_islandFlag = false;
  144. m_userData = def->userData;
  145. m_edgeA.joint = NULL;
  146. m_edgeA.other = NULL;
  147. m_edgeA.prev = NULL;
  148. m_edgeA.next = NULL;
  149. m_edgeB.joint = NULL;
  150. m_edgeB.other = NULL;
  151. m_edgeB.prev = NULL;
  152. m_edgeB.next = NULL;
  153. }
  154. bool b2Joint::IsActive() const
  155. {
  156. return m_bodyA->IsActive() && m_bodyB->IsActive();
  157. }