b2ChainShape.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <Box2D/Collision/Shapes/b2ChainShape.h>
  20. #include <Box2D/Collision/Shapes/b2EdgeShape.h>
  21. #include <new>
  22. #include <memory.h>
  23. #include <string.h>
  24. b2ChainShape::~b2ChainShape()
  25. {
  26. b2Free(m_vertices);
  27. m_vertices = NULL;
  28. m_count = 0;
  29. }
  30. void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
  31. {
  32. b2Assert(m_vertices == NULL && m_count == 0);
  33. b2Assert(count >= 3);
  34. for (int32 i = 1; i < count; ++i)
  35. {
  36. #if B2_ASSERT_ENABLED
  37. b2Vec2 v1 = vertices[i-1];
  38. b2Vec2 v2 = vertices[i];
  39. // If the code crashes here, it means your vertices are too close together.
  40. b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
  41. #endif // B2_ASSERT_ENABLED
  42. }
  43. m_count = count + 1;
  44. m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
  45. memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
  46. m_vertices[count] = m_vertices[0];
  47. m_prevVertex = m_vertices[m_count - 2];
  48. m_nextVertex = m_vertices[1];
  49. m_hasPrevVertex = true;
  50. m_hasNextVertex = true;
  51. }
  52. void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count)
  53. {
  54. b2Assert(m_vertices == NULL && m_count == 0);
  55. b2Assert(count >= 2);
  56. for (int32 i = 1; i < count; ++i)
  57. {
  58. #if B2_ASSERT_ENABLED
  59. b2Vec2 v1 = vertices[i-1];
  60. b2Vec2 v2 = vertices[i];
  61. // If the code crashes here, it means your vertices are too close together.
  62. b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
  63. #endif // B2_ASSERT_ENABLED
  64. }
  65. m_count = count;
  66. m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
  67. memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
  68. m_hasPrevVertex = false;
  69. m_hasNextVertex = false;
  70. m_prevVertex.SetZero();
  71. m_nextVertex.SetZero();
  72. }
  73. void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
  74. {
  75. m_prevVertex = prevVertex;
  76. m_hasPrevVertex = true;
  77. }
  78. void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
  79. {
  80. m_nextVertex = nextVertex;
  81. m_hasNextVertex = true;
  82. }
  83. b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
  84. {
  85. void* mem = allocator->Allocate(sizeof(b2ChainShape));
  86. b2ChainShape* clone = new (mem) b2ChainShape;
  87. clone->CreateChain(m_vertices, m_count);
  88. clone->m_prevVertex = m_prevVertex;
  89. clone->m_nextVertex = m_nextVertex;
  90. clone->m_hasPrevVertex = m_hasPrevVertex;
  91. clone->m_hasNextVertex = m_hasNextVertex;
  92. return clone;
  93. }
  94. int32 b2ChainShape::GetChildCount() const
  95. {
  96. // edge count = vertex count - 1
  97. return m_count - 1;
  98. }
  99. void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
  100. {
  101. b2Assert(0 <= index && index < m_count - 1);
  102. edge->m_type = b2Shape::e_edge;
  103. edge->m_radius = m_radius;
  104. edge->m_vertex1 = m_vertices[index + 0];
  105. edge->m_vertex2 = m_vertices[index + 1];
  106. if (index > 0)
  107. {
  108. edge->m_vertex0 = m_vertices[index - 1];
  109. edge->m_hasVertex0 = true;
  110. }
  111. else
  112. {
  113. edge->m_vertex0 = m_prevVertex;
  114. edge->m_hasVertex0 = m_hasPrevVertex;
  115. }
  116. if (index < m_count - 2)
  117. {
  118. edge->m_vertex3 = m_vertices[index + 2];
  119. edge->m_hasVertex3 = true;
  120. }
  121. else
  122. {
  123. edge->m_vertex3 = m_nextVertex;
  124. edge->m_hasVertex3 = m_hasNextVertex;
  125. }
  126. }
  127. void b2ChainShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const
  128. {
  129. b2EdgeShape edge;
  130. GetChildEdge(&edge, childIndex);
  131. edge.ComputeDistance(xf, p, distance, normal, 0);
  132. }
  133. bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
  134. {
  135. B2_NOT_USED(xf);
  136. B2_NOT_USED(p);
  137. return false;
  138. }
  139. bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  140. const b2Transform& xf, int32 childIndex) const
  141. {
  142. b2Assert(childIndex < m_count);
  143. b2EdgeShape edgeShape;
  144. int32 i1 = childIndex;
  145. int32 i2 = childIndex + 1;
  146. if (i2 == m_count)
  147. {
  148. i2 = 0;
  149. }
  150. edgeShape.m_vertex1 = m_vertices[i1];
  151. edgeShape.m_vertex2 = m_vertices[i2];
  152. return edgeShape.RayCast(output, input, xf, 0);
  153. }
  154. void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  155. {
  156. b2Assert(childIndex < m_count);
  157. int32 i1 = childIndex;
  158. int32 i2 = childIndex + 1;
  159. if (i2 == m_count)
  160. {
  161. i2 = 0;
  162. }
  163. b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
  164. b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
  165. aabb->lowerBound = b2Min(v1, v2);
  166. aabb->upperBound = b2Max(v1, v2);
  167. }
  168. void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
  169. {
  170. B2_NOT_USED(density);
  171. massData->mass = 0.0f;
  172. massData->center.SetZero();
  173. massData->I = 0.0f;
  174. }