CollisionChain2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/MemoryBuffer.h"
  25. #include "../IO/VectorBuffer.h"
  26. #include "../Atomic2D/CollisionChain2D.h"
  27. #include "../Atomic2D/PhysicsUtils2D.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. extern const char* ATOMIC2D_CATEGORY;
  32. CollisionChain2D::CollisionChain2D(Context* context) :
  33. CollisionShape2D(context),
  34. loop_(false)
  35. {
  36. fixtureDef_.shape = &chainShape_;
  37. }
  38. CollisionChain2D::~CollisionChain2D()
  39. {
  40. }
  41. void CollisionChain2D::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<CollisionChain2D>(ATOMIC2D_CATEGORY);
  44. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  45. ATOMIC_ACCESSOR_ATTRIBUTE("Loop", GetLoop, SetLoop, bool, false, AM_DEFAULT);
  46. ATOMIC_COPY_BASE_ATTRIBUTES(CollisionShape2D);
  47. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Vertices", GetVerticesAttr, SetVerticesAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE);
  48. }
  49. void CollisionChain2D::SetLoop(bool loop)
  50. {
  51. if (loop == loop_)
  52. return;
  53. loop_ = loop;
  54. MarkNetworkUpdate();
  55. RecreateFixture();
  56. }
  57. void CollisionChain2D::SetVertexCount(unsigned count)
  58. {
  59. vertices_.Resize(count);
  60. }
  61. void CollisionChain2D::SetVertex(unsigned index, const Vector2& vertex)
  62. {
  63. if (index >= vertices_.Size())
  64. return;
  65. vertices_[index] = vertex;
  66. if (index == vertices_.Size() - 1)
  67. {
  68. MarkNetworkUpdate();
  69. RecreateFixture();
  70. }
  71. }
  72. void CollisionChain2D::SetVertices(const PODVector<Vector2>& vertices)
  73. {
  74. vertices_ = vertices;
  75. MarkNetworkUpdate();
  76. RecreateFixture();
  77. }
  78. void CollisionChain2D::SetVerticesAttr(const PODVector<unsigned char>& value)
  79. {
  80. if (value.Empty())
  81. return;
  82. PODVector<Vector2> vertices;
  83. MemoryBuffer buffer(value);
  84. while (!buffer.IsEof())
  85. vertices.Push(buffer.ReadVector2());
  86. SetVertices(vertices);
  87. }
  88. PODVector<unsigned char> CollisionChain2D::GetVerticesAttr() const
  89. {
  90. VectorBuffer ret;
  91. for (unsigned i = 0; i < vertices_.Size(); ++i)
  92. ret.WriteVector2(vertices_[i]);
  93. return ret.GetBuffer();
  94. }
  95. void CollisionChain2D::ApplyNodeWorldScale()
  96. {
  97. RecreateFixture();
  98. }
  99. void CollisionChain2D::RecreateFixture()
  100. {
  101. ReleaseFixture();
  102. PODVector<b2Vec2> b2Vertices;
  103. unsigned count = vertices_.Size();
  104. b2Vertices.Resize(count);
  105. Vector2 worldScale(cachedWorldScale_.x_, cachedWorldScale_.y_);
  106. for (unsigned i = 0; i < count; ++i)
  107. b2Vertices[i] = ToB2Vec2(vertices_[i] * worldScale);
  108. if (loop_)
  109. chainShape_.CreateLoop(&b2Vertices[0], count);
  110. else
  111. chainShape_.CreateChain(&b2Vertices[0], count);
  112. CreateFixture();
  113. }
  114. }