CollisionChain2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Copyright (c) 2008-2020 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 "../Urho2D/CollisionChain2D.h"
  27. #include "../Urho2D/PhysicsUtils2D.h"
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. extern const char* URHO2D_CATEGORY;
  32. CollisionChain2D::CollisionChain2D(Context* context) :
  33. CollisionShape2D(context),
  34. loop_(false)
  35. {
  36. fixtureDef_.shape = &chainShape_;
  37. }
  38. CollisionChain2D::~CollisionChain2D() = default;
  39. void CollisionChain2D::RegisterObject(Context* context)
  40. {
  41. context->RegisterFactory<CollisionChain2D>(URHO2D_CATEGORY);
  42. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  43. URHO3D_ACCESSOR_ATTRIBUTE("Loop", GetLoop, SetLoop, bool, false, AM_DEFAULT);
  44. URHO3D_COPY_BASE_ATTRIBUTES(CollisionShape2D);
  45. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Vertices", GetVerticesAttr, SetVerticesAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE);
  46. }
  47. void CollisionChain2D::SetLoop(bool loop)
  48. {
  49. if (loop == loop_)
  50. return;
  51. loop_ = loop;
  52. MarkNetworkUpdate();
  53. RecreateFixture();
  54. }
  55. void CollisionChain2D::SetVertexCount(unsigned count)
  56. {
  57. vertices_.Resize(count);
  58. }
  59. void CollisionChain2D::SetVertex(unsigned index, const Vector2& vertex)
  60. {
  61. if (index >= vertices_.Size())
  62. return;
  63. vertices_[index] = vertex;
  64. if (index == vertices_.Size() - 1)
  65. {
  66. MarkNetworkUpdate();
  67. RecreateFixture();
  68. }
  69. }
  70. void CollisionChain2D::SetVertices(const PODVector<Vector2>& vertices)
  71. {
  72. vertices_ = vertices;
  73. MarkNetworkUpdate();
  74. RecreateFixture();
  75. }
  76. void CollisionChain2D::SetVerticesAttr(const PODVector<unsigned char>& value)
  77. {
  78. if (value.Empty())
  79. return;
  80. PODVector<Vector2> vertices;
  81. MemoryBuffer buffer(value);
  82. while (!buffer.IsEof())
  83. vertices.Push(buffer.ReadVector2());
  84. SetVertices(vertices);
  85. }
  86. PODVector<unsigned char> CollisionChain2D::GetVerticesAttr() const
  87. {
  88. VectorBuffer ret;
  89. for (unsigned i = 0; i < vertices_.Size(); ++i)
  90. ret.WriteVector2(vertices_[i]);
  91. return ret.GetBuffer();
  92. }
  93. void CollisionChain2D::ApplyNodeWorldScale()
  94. {
  95. RecreateFixture();
  96. }
  97. void CollisionChain2D::RecreateFixture()
  98. {
  99. ReleaseFixture();
  100. PODVector<b2Vec2> b2Vertices;
  101. unsigned count = vertices_.Size();
  102. b2Vertices.Resize(count);
  103. Vector2 worldScale(cachedWorldScale_.x_, cachedWorldScale_.y_);
  104. for (unsigned i = 0; i < count; ++i)
  105. b2Vertices[i] = ToB2Vec2(vertices_[i] * worldScale);
  106. chainShape_.Clear();
  107. if (loop_)
  108. chainShape_.CreateLoop(&b2Vertices[0], count);
  109. else
  110. chainShape_.CreateChain(&b2Vertices[0], count);
  111. CreateFixture();
  112. }
  113. }