CollisionShape2D.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #pragma once
  23. #include "../Scene/Component.h"
  24. #include <Box2D/Box2D.h>
  25. namespace Urho3D
  26. {
  27. class RigidBody2D;
  28. /// 2D collision shape component.
  29. class URHO3D_API CollisionShape2D : public Component
  30. {
  31. URHO3D_OBJECT(CollisionShape2D, Component);
  32. public:
  33. /// Construct.
  34. explicit CollisionShape2D(Context* context);
  35. /// Destruct.
  36. ~CollisionShape2D() override;
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Handle enabled/disabled state change.
  40. void OnSetEnabled() override;
  41. /// Set trigger.
  42. /// @property
  43. void SetTrigger(bool trigger);
  44. /// Set filter category bits.
  45. /// @property
  46. void SetCategoryBits(int categoryBits);
  47. /// Set filter mask bits.
  48. /// @property
  49. void SetMaskBits(int maskBits);
  50. /// Set filter group index.
  51. /// @property
  52. void SetGroupIndex(int groupIndex);
  53. /// Set density.
  54. /// @property
  55. void SetDensity(float density);
  56. /// Set friction.
  57. /// @property
  58. void SetFriction(float friction);
  59. /// Set restitution.
  60. /// @property
  61. void SetRestitution(float restitution);
  62. /// Create fixture.
  63. void CreateFixture();
  64. /// Release fixture.
  65. void ReleaseFixture();
  66. /// Return trigger.
  67. /// @property
  68. bool IsTrigger() const { return fixtureDef_.isSensor; }
  69. /// Return filter category bits.
  70. /// @property
  71. int GetCategoryBits() const { return fixtureDef_.filter.categoryBits; }
  72. /// Return filter mask bits.
  73. /// @property
  74. int GetMaskBits() const { return fixtureDef_.filter.maskBits; }
  75. /// Return filter group index.
  76. /// @property
  77. int GetGroupIndex() const { return fixtureDef_.filter.groupIndex; }
  78. /// Return density.
  79. /// @property
  80. float GetDensity() const { return fixtureDef_.density; }
  81. /// Return friction.
  82. /// @property
  83. float GetFriction() const { return fixtureDef_.friction; }
  84. /// Return restitution.
  85. /// @property
  86. float GetRestitution() const { return fixtureDef_.restitution; }
  87. /// Return mass.
  88. /// @property
  89. float GetMass() const;
  90. /// Return inertia.
  91. /// @property
  92. float GetInertia() const;
  93. /// Return mass center.
  94. /// @property
  95. Vector2 GetMassCenter() const;
  96. /// Return fixture.
  97. b2Fixture* GetFixture() const { return fixture_; }
  98. protected:
  99. /// Handle node being assigned.
  100. void OnNodeSet(Node* node) override;
  101. /// Handle node transform being dirtied.
  102. void OnMarkedDirty(Node* node) override;
  103. /// Apply Node world scale.
  104. virtual void ApplyNodeWorldScale() = 0;
  105. /// Rigid body.
  106. WeakPtr<RigidBody2D> rigidBody_;
  107. /// Fixture def.
  108. b2FixtureDef fixtureDef_;
  109. /// Box2D fixture.
  110. b2Fixture* fixture_;
  111. /// Cached world scale.
  112. Vector3 cachedWorldScale_;
  113. };
  114. }