CollisionShape2D.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Scene/Component.h"
  5. #include <box2d/box2d.h>
  6. namespace Urho3D
  7. {
  8. class RigidBody2D;
  9. /// 2D collision shape component.
  10. class URHO3D_API CollisionShape2D : public Component
  11. {
  12. URHO3D_OBJECT(CollisionShape2D, Component);
  13. public:
  14. /// Construct.
  15. explicit CollisionShape2D(Context* context);
  16. /// Destruct.
  17. ~CollisionShape2D() override;
  18. /// Register object factory.
  19. /// @nobind
  20. static void RegisterObject(Context* context);
  21. /// Handle enabled/disabled state change.
  22. void OnSetEnabled() override;
  23. /// Set trigger.
  24. /// @property
  25. void SetTrigger(bool trigger);
  26. /// Set filter category bits.
  27. /// @property
  28. void SetCategoryBits(int categoryBits);
  29. /// Set filter mask bits.
  30. /// @property
  31. void SetMaskBits(int maskBits);
  32. /// Set filter group index.
  33. /// @property
  34. void SetGroupIndex(int groupIndex);
  35. /// Set density.
  36. /// @property
  37. void SetDensity(float density);
  38. /// Set friction.
  39. /// @property
  40. void SetFriction(float friction);
  41. /// Set restitution.
  42. /// @property
  43. void SetRestitution(float restitution);
  44. /// Create fixture.
  45. void CreateFixture();
  46. /// Release fixture.
  47. void ReleaseFixture();
  48. /// Return trigger.
  49. /// @property
  50. bool IsTrigger() const { return fixtureDef_.isSensor; }
  51. /// Return filter category bits.
  52. /// @property
  53. int GetCategoryBits() const { return fixtureDef_.filter.categoryBits; }
  54. /// Return filter mask bits.
  55. /// @property
  56. int GetMaskBits() const { return fixtureDef_.filter.maskBits; }
  57. /// Return filter group index.
  58. /// @property
  59. int GetGroupIndex() const { return fixtureDef_.filter.groupIndex; }
  60. /// Return density.
  61. /// @property
  62. float GetDensity() const { return fixtureDef_.density; }
  63. /// Return friction.
  64. /// @property
  65. float GetFriction() const { return fixtureDef_.friction; }
  66. /// Return restitution.
  67. /// @property
  68. float GetRestitution() const { return fixtureDef_.restitution; }
  69. /// Return mass.
  70. /// @property
  71. float GetMass() const;
  72. /// Return inertia.
  73. /// @property
  74. float GetInertia() const;
  75. /// Return mass center.
  76. /// @property
  77. Vector2 GetMassCenter() const;
  78. /// Return fixture.
  79. b2Fixture* GetFixture() const { return fixture_; }
  80. protected:
  81. /// Handle node being assigned.
  82. void OnNodeSet(Node* node) override;
  83. /// Handle node transform being dirtied.
  84. void OnMarkedDirty(Node* node) override;
  85. /// Apply Node world scale.
  86. virtual void ApplyNodeWorldScale() = 0;
  87. /// Rigid body.
  88. WeakPtr<RigidBody2D> rigidBody_;
  89. /// Fixture def.
  90. b2FixtureDef fixtureDef_;
  91. /// Box2D fixture.
  92. b2Fixture* fixture_;
  93. /// Cached world scale.
  94. Vector3 cachedWorldScale_;
  95. };
  96. }