b2ChainShape.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2006-2010 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/Collision/Shapes/b2ChainShape.h>
  19. #include <Box2D/Collision/Shapes/b2EdgeShape.h>
  20. #include <new>
  21. #include <string.h>
  22. b2ChainShape::~b2ChainShape()
  23. {
  24. Clear();
  25. }
  26. void b2ChainShape::Clear()
  27. {
  28. b2Free(m_vertices);
  29. m_vertices = NULL;
  30. m_count = 0;
  31. }
  32. void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
  33. {
  34. b2Assert(m_vertices == NULL && m_count == 0);
  35. b2Assert(count >= 3);
  36. for (int32 i = 1; i < count; ++i)
  37. {
  38. b2Vec2 v1 = vertices[i-1];
  39. b2Vec2 v2 = vertices[i];
  40. // If the code crashes here, it means your vertices are too close together.
  41. b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
  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 the code crashes here, it means your vertices are too close together.
  59. b2Assert(b2DistanceSquared(vertices[i-1], vertices[i]) > b2_linearSlop * b2_linearSlop);
  60. }
  61. m_count = count;
  62. m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
  63. memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
  64. m_hasPrevVertex = false;
  65. m_hasNextVertex = false;
  66. m_prevVertex.SetZero();
  67. m_nextVertex.SetZero();
  68. }
  69. void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
  70. {
  71. m_prevVertex = prevVertex;
  72. m_hasPrevVertex = true;
  73. }
  74. void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
  75. {
  76. m_nextVertex = nextVertex;
  77. m_hasNextVertex = true;
  78. }
  79. b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
  80. {
  81. void* mem = allocator->Allocate(sizeof(b2ChainShape));
  82. b2ChainShape* clone = new (mem) b2ChainShape;
  83. clone->CreateChain(m_vertices, m_count);
  84. clone->m_prevVertex = m_prevVertex;
  85. clone->m_nextVertex = m_nextVertex;
  86. clone->m_hasPrevVertex = m_hasPrevVertex;
  87. clone->m_hasNextVertex = m_hasNextVertex;
  88. return clone;
  89. }
  90. int32 b2ChainShape::GetChildCount() const
  91. {
  92. // edge count = vertex count - 1
  93. return m_count - 1;
  94. }
  95. void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
  96. {
  97. b2Assert(0 <= index && index < m_count - 1);
  98. edge->m_type = b2Shape::e_edge;
  99. edge->m_radius = m_radius;
  100. edge->m_vertex1 = m_vertices[index + 0];
  101. edge->m_vertex2 = m_vertices[index + 1];
  102. if (index > 0)
  103. {
  104. edge->m_vertex0 = m_vertices[index - 1];
  105. edge->m_hasVertex0 = true;
  106. }
  107. else
  108. {
  109. edge->m_vertex0 = m_prevVertex;
  110. edge->m_hasVertex0 = m_hasPrevVertex;
  111. }
  112. if (index < m_count - 2)
  113. {
  114. edge->m_vertex3 = m_vertices[index + 2];
  115. edge->m_hasVertex3 = true;
  116. }
  117. else
  118. {
  119. edge->m_vertex3 = m_nextVertex;
  120. edge->m_hasVertex3 = m_hasNextVertex;
  121. }
  122. }
  123. bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
  124. {
  125. B2_NOT_USED(xf);
  126. B2_NOT_USED(p);
  127. return false;
  128. }
  129. bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  130. const b2Transform& xf, int32 childIndex) const
  131. {
  132. b2Assert(childIndex < m_count);
  133. b2EdgeShape edgeShape;
  134. int32 i1 = childIndex;
  135. int32 i2 = childIndex + 1;
  136. if (i2 == m_count)
  137. {
  138. i2 = 0;
  139. }
  140. edgeShape.m_vertex1 = m_vertices[i1];
  141. edgeShape.m_vertex2 = m_vertices[i2];
  142. return edgeShape.RayCast(output, input, xf, 0);
  143. }
  144. void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  145. {
  146. b2Assert(childIndex < m_count);
  147. int32 i1 = childIndex;
  148. int32 i2 = childIndex + 1;
  149. if (i2 == m_count)
  150. {
  151. i2 = 0;
  152. }
  153. b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
  154. b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
  155. aabb->lowerBound = b2Min(v1, v2);
  156. aabb->upperBound = b2Max(v1, v2);
  157. }
  158. void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
  159. {
  160. B2_NOT_USED(density);
  161. massData->mass = 0.0f;
  162. massData->center.SetZero();
  163. massData->I = 0.0f;
  164. }