b2ChainShape.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <memory.h>
  22. #include <string.h>
  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. m_prevVertex.SetZero();
  66. m_nextVertex.SetZero();
  67. }
  68. void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
  69. {
  70. m_prevVertex = prevVertex;
  71. m_hasPrevVertex = true;
  72. }
  73. void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
  74. {
  75. m_nextVertex = nextVertex;
  76. m_hasNextVertex = true;
  77. }
  78. b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
  79. {
  80. void* mem = allocator->Allocate(sizeof(b2ChainShape));
  81. b2ChainShape* clone = new (mem) b2ChainShape;
  82. clone->CreateChain(m_vertices, m_count);
  83. clone->m_prevVertex = m_prevVertex;
  84. clone->m_nextVertex = m_nextVertex;
  85. clone->m_hasPrevVertex = m_hasPrevVertex;
  86. clone->m_hasNextVertex = m_hasNextVertex;
  87. return clone;
  88. }
  89. int32 b2ChainShape::GetChildCount() const
  90. {
  91. // edge count = vertex count - 1
  92. return m_count - 1;
  93. }
  94. void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
  95. {
  96. b2Assert(0 <= index && index < m_count - 1);
  97. edge->m_type = b2Shape::e_edge;
  98. edge->m_radius = m_radius;
  99. edge->m_vertex1 = m_vertices[index + 0];
  100. edge->m_vertex2 = m_vertices[index + 1];
  101. if (index > 0)
  102. {
  103. edge->m_vertex0 = m_vertices[index - 1];
  104. edge->m_hasVertex0 = true;
  105. }
  106. else
  107. {
  108. edge->m_vertex0 = m_prevVertex;
  109. edge->m_hasVertex0 = m_hasPrevVertex;
  110. }
  111. if (index < m_count - 2)
  112. {
  113. edge->m_vertex3 = m_vertices[index + 2];
  114. edge->m_hasVertex3 = true;
  115. }
  116. else
  117. {
  118. edge->m_vertex3 = m_nextVertex;
  119. edge->m_hasVertex3 = m_hasNextVertex;
  120. }
  121. }
  122. bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
  123. {
  124. B2_NOT_USED(xf);
  125. B2_NOT_USED(p);
  126. return false;
  127. }
  128. bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  129. const b2Transform& xf, int32 childIndex) const
  130. {
  131. b2Assert(childIndex < m_count);
  132. b2EdgeShape edgeShape;
  133. int32 i1 = childIndex;
  134. int32 i2 = childIndex + 1;
  135. if (i2 == m_count)
  136. {
  137. i2 = 0;
  138. }
  139. edgeShape.m_vertex1 = m_vertices[i1];
  140. edgeShape.m_vertex2 = m_vertices[i2];
  141. return edgeShape.RayCast(output, input, xf, 0);
  142. }
  143. void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
  144. {
  145. b2Assert(childIndex < m_count);
  146. int32 i1 = childIndex;
  147. int32 i2 = childIndex + 1;
  148. if (i2 == m_count)
  149. {
  150. i2 = 0;
  151. }
  152. b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
  153. b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
  154. aabb->lowerBound = b2Min(v1, v2);
  155. aabb->upperBound = b2Max(v1, v2);
  156. }
  157. void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
  158. {
  159. B2_NOT_USED(density);
  160. massData->mass = 0.0f;
  161. massData->center.SetZero();
  162. massData->I = 0.0f;
  163. }