b2Joint.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "b2Joint.h"
  19. #include "b2DistanceJoint.h"
  20. #include "b2LineJoint.h"
  21. #include "b2MouseJoint.h"
  22. #include "b2RevoluteJoint.h"
  23. #include "b2PrismaticJoint.h"
  24. #include "b2PulleyJoint.h"
  25. #include "b2GearJoint.h"
  26. #include "../b2Body.h"
  27. #include "../b2World.h"
  28. #include "../../Common/b2BlockAllocator.h"
  29. #include "../../Collision/b2BroadPhase.h"
  30. #include <new>
  31. b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
  32. {
  33. b2Joint* joint = NULL;
  34. switch (def->type)
  35. {
  36. case e_distanceJoint:
  37. {
  38. void* mem = allocator->Allocate(sizeof(b2DistanceJoint));
  39. joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def);
  40. }
  41. break;
  42. case e_mouseJoint:
  43. {
  44. void* mem = allocator->Allocate(sizeof(b2MouseJoint));
  45. joint = new (mem) b2MouseJoint((b2MouseJointDef*)def);
  46. }
  47. break;
  48. case e_prismaticJoint:
  49. {
  50. void* mem = allocator->Allocate(sizeof(b2PrismaticJoint));
  51. joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def);
  52. }
  53. break;
  54. case e_revoluteJoint:
  55. {
  56. void* mem = allocator->Allocate(sizeof(b2RevoluteJoint));
  57. joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def);
  58. }
  59. break;
  60. case e_pulleyJoint:
  61. {
  62. void* mem = allocator->Allocate(sizeof(b2PulleyJoint));
  63. joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def);
  64. }
  65. break;
  66. case e_gearJoint:
  67. {
  68. void* mem = allocator->Allocate(sizeof(b2GearJoint));
  69. joint = new (mem) b2GearJoint((b2GearJointDef*)def);
  70. }
  71. break;
  72. case e_lineJoint:
  73. {
  74. void* mem = allocator->Allocate(sizeof(b2LineJoint));
  75. joint = new (mem) b2LineJoint((b2LineJointDef*)def);
  76. }
  77. break;
  78. default:
  79. b2Assert(false);
  80. break;
  81. }
  82. return joint;
  83. }
  84. void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
  85. {
  86. joint->~b2Joint();
  87. switch (joint->m_type)
  88. {
  89. case e_distanceJoint:
  90. allocator->Free(joint, sizeof(b2DistanceJoint));
  91. break;
  92. case e_mouseJoint:
  93. allocator->Free(joint, sizeof(b2MouseJoint));
  94. break;
  95. case e_prismaticJoint:
  96. allocator->Free(joint, sizeof(b2PrismaticJoint));
  97. break;
  98. case e_revoluteJoint:
  99. allocator->Free(joint, sizeof(b2RevoluteJoint));
  100. break;
  101. case e_pulleyJoint:
  102. allocator->Free(joint, sizeof(b2PulleyJoint));
  103. break;
  104. case e_gearJoint:
  105. allocator->Free(joint, sizeof(b2GearJoint));
  106. break;
  107. case e_lineJoint:
  108. allocator->Free(joint, sizeof(b2LineJoint));
  109. break;
  110. default:
  111. b2Assert(false);
  112. break;
  113. }
  114. }
  115. b2Joint::b2Joint(const b2JointDef* def)
  116. {
  117. m_type = def->type;
  118. m_prev = NULL;
  119. m_next = NULL;
  120. m_body1 = def->body1;
  121. m_body2 = def->body2;
  122. m_collideConnected = def->collideConnected;
  123. m_islandFlag = false;
  124. m_userData = def->userData;
  125. }