The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.3KB

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