b2ChainShape.cpp 4.9 KB

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