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.

172 lines
4.4KB

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