CollisionPolygon2D.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/CollisionPolygon2D.h"
  27. #include "../Atomic2D/PhysicsUtils2D.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. extern const char* ATOMIC2D_CATEGORY;
  32. CollisionPolygon2D::CollisionPolygon2D(Context* context) :
  33. CollisionShape2D(context)
  34. {
  35. fixtureDef_.shape = &polygonShape_;
  36. }
  37. CollisionPolygon2D::~CollisionPolygon2D()
  38. {
  39. }
  40. void CollisionPolygon2D::RegisterObject(Context* context)
  41. {
  42. context->RegisterFactory<CollisionPolygon2D>(ATOMIC2D_CATEGORY);
  43. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  44. ATOMIC_COPY_BASE_ATTRIBUTES(CollisionShape2D);
  45. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Vertices", GetVerticesAttr, SetVerticesAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE);
  46. }
  47. void CollisionPolygon2D::SetVertexCount(unsigned count)
  48. {
  49. vertices_.Resize(count);
  50. }
  51. void CollisionPolygon2D::SetVertex(unsigned index, const Vector2& vertex)
  52. {
  53. if (index >= vertices_.Size())
  54. return;
  55. vertices_[index] = vertex;
  56. if (index == vertices_.Size() - 1)
  57. {
  58. MarkNetworkUpdate();
  59. RecreateFixture();
  60. }
  61. }
  62. void CollisionPolygon2D::SetVertices(const PODVector<Vector2>& vertices)
  63. {
  64. vertices_ = vertices;
  65. MarkNetworkUpdate();
  66. RecreateFixture();
  67. }
  68. void CollisionPolygon2D::SetVerticesAttr(const PODVector<unsigned char>& value)
  69. {
  70. if (value.Empty())
  71. return;
  72. PODVector<Vector2> vertices;
  73. MemoryBuffer buffer(value);
  74. while (!buffer.IsEof())
  75. vertices.Push(buffer.ReadVector2());
  76. SetVertices(vertices);
  77. }
  78. PODVector<unsigned char> CollisionPolygon2D::GetVerticesAttr() const
  79. {
  80. VectorBuffer ret;
  81. for (unsigned i = 0; i < vertices_.Size(); ++i)
  82. ret.WriteVector2(vertices_[i]);
  83. return ret.GetBuffer();
  84. }
  85. void CollisionPolygon2D::ApplyNodeWorldScale()
  86. {
  87. RecreateFixture();
  88. }
  89. void CollisionPolygon2D::RecreateFixture()
  90. {
  91. ReleaseFixture();
  92. if (vertices_.Size() < 3)
  93. return;
  94. PODVector<b2Vec2> b2Vertices;
  95. unsigned count = vertices_.Size();
  96. b2Vertices.Resize(count);
  97. Vector2 worldScale(cachedWorldScale_.x_, cachedWorldScale_.y_);
  98. for (unsigned i = 0; i < count; ++i)
  99. b2Vertices[i] = ToB2Vec2(vertices_[i] * worldScale);
  100. polygonShape_.Set(&b2Vertices[0], count);
  101. CreateFixture();
  102. }
  103. }