CollisionBox2D.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/CollisionBox2D.h"
  6. #include "../Physics2D/PhysicsUtils2D.h"
  7. #include "../DebugNew.h"
  8. namespace Urho3D
  9. {
  10. extern const char* PHYSICS2D_CATEGORY;
  11. static const Vector2 DEFAULT_BOX_SIZE(0.01f, 0.01f);
  12. CollisionBox2D::CollisionBox2D(Context* context) :
  13. CollisionShape2D(context),
  14. size_(DEFAULT_BOX_SIZE),
  15. center_(Vector2::ZERO),
  16. angle_(0.0f)
  17. {
  18. float halfWidth = size_.x_ * 0.5f * cachedWorldScale_.x_;
  19. float halfHeight = size_.y_ * 0.5f * cachedWorldScale_.y_;
  20. boxShape_.SetAsBox(halfWidth, halfHeight);
  21. fixtureDef_.shape = &boxShape_;
  22. }
  23. CollisionBox2D::~CollisionBox2D() = default;
  24. void CollisionBox2D::RegisterObject(Context* context)
  25. {
  26. context->RegisterFactory<CollisionBox2D>(PHYSICS2D_CATEGORY);
  27. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  28. URHO3D_ACCESSOR_ATTRIBUTE("Size", GetSize, SetSize, DEFAULT_BOX_SIZE, AM_DEFAULT);
  29. URHO3D_ACCESSOR_ATTRIBUTE("Center", GetCenter, SetCenter, Vector2::ZERO, AM_DEFAULT);
  30. URHO3D_ACCESSOR_ATTRIBUTE("Angle", GetAngle, SetAngle, 0.0f, AM_DEFAULT);
  31. URHO3D_COPY_BASE_ATTRIBUTES(CollisionShape2D);
  32. }
  33. void CollisionBox2D::SetSize(const Vector2& size)
  34. {
  35. if (size == size_)
  36. return;
  37. size_ = size;
  38. MarkNetworkUpdate();
  39. RecreateFixture();
  40. }
  41. void CollisionBox2D::SetSize(float width, float height)
  42. {
  43. SetSize(Vector2(width, height));
  44. }
  45. void CollisionBox2D::SetCenter(const Vector2& center)
  46. {
  47. if (center == center_)
  48. return;
  49. center_ = center;
  50. MarkNetworkUpdate();
  51. RecreateFixture();
  52. }
  53. void CollisionBox2D::SetCenter(float x, float y)
  54. {
  55. SetCenter(Vector2(x, y));
  56. }
  57. void CollisionBox2D::SetAngle(float angle)
  58. {
  59. if (angle == angle_)
  60. return;
  61. angle_ = angle;
  62. MarkNetworkUpdate();
  63. RecreateFixture();
  64. }
  65. void CollisionBox2D::ApplyNodeWorldScale()
  66. {
  67. RecreateFixture();
  68. }
  69. void CollisionBox2D::RecreateFixture()
  70. {
  71. ReleaseFixture();
  72. float worldScaleX = cachedWorldScale_.x_;
  73. float worldScaleY = cachedWorldScale_.y_;
  74. float halfWidth = size_.x_ * 0.5f * worldScaleX;
  75. float halfHeight = size_.y_ * 0.5f * worldScaleY;
  76. Vector2 scaledCenter = center_ * Vector2(worldScaleX, worldScaleY);
  77. if (scaledCenter == Vector2::ZERO && angle_ == 0.0f)
  78. boxShape_.SetAsBox(halfWidth, halfHeight);
  79. else
  80. boxShape_.SetAsBox(halfWidth, halfHeight, ToB2Vec2(scaledCenter), angle_ * M_DEGTORAD);
  81. CreateFixture();
  82. }
  83. }