b2_shape.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_SHAPE_H
  19. #define B2_SHAPE_H
  20. #include "b2_api.h"
  21. #include "b2_math.h"
  22. #include "b2_collision.h"
  23. class b2BlockAllocator;
  24. /// This holds the mass data computed for a shape.
  25. struct B2_API b2MassData
  26. {
  27. /// The mass of the shape, usually in kilograms.
  28. float mass;
  29. /// The position of the shape's centroid relative to the shape's origin.
  30. b2Vec2 center;
  31. /// The rotational inertia of the shape about the local origin.
  32. float I;
  33. };
  34. /// A shape is used for collision detection. You can create a shape however you like.
  35. /// Shapes used for simulation in b2World are created automatically when a b2Fixture
  36. /// is created. Shapes may encapsulate a one or more child shapes.
  37. class B2_API b2Shape
  38. {
  39. public:
  40. enum Type
  41. {
  42. e_circle = 0,
  43. e_edge = 1,
  44. e_polygon = 2,
  45. e_chain = 3,
  46. e_typeCount = 4
  47. };
  48. virtual ~b2Shape() {}
  49. /// Clone the concrete shape using the provided allocator.
  50. virtual b2Shape* Clone(b2BlockAllocator* allocator) const = 0;
  51. /// Get the type of this shape. You can use this to down cast to the concrete shape.
  52. /// @return the shape type.
  53. Type GetType() const;
  54. /// Get the number of child primitives.
  55. virtual int32 GetChildCount() const = 0;
  56. /// Test a point for containment in this shape. This only works for convex shapes.
  57. /// @param xf the shape world transform.
  58. /// @param p a point in world coordinates.
  59. virtual bool TestPoint(const b2Transform& xf, const b2Vec2& p) const = 0;
  60. /// Cast a ray against a child shape.
  61. /// @param output the ray-cast results.
  62. /// @param input the ray-cast input parameters.
  63. /// @param transform the transform to be applied to the shape.
  64. /// @param childIndex the child shape index
  65. virtual bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  66. const b2Transform& transform, int32 childIndex) const = 0;
  67. /// Given a transform, compute the associated axis aligned bounding box for a child shape.
  68. /// @param aabb returns the axis aligned box.
  69. /// @param xf the world transform of the shape.
  70. /// @param childIndex the child shape
  71. virtual void ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const = 0;
  72. /// Compute the mass properties of this shape using its dimensions and density.
  73. /// The inertia tensor is computed about the local origin.
  74. /// @param massData returns the mass data for this shape.
  75. /// @param density the density in kilograms per meter squared.
  76. virtual void ComputeMass(b2MassData* massData, float density) const = 0;
  77. Type m_type;
  78. /// Radius of a shape. For polygonal shapes this must be b2_polygonRadius. There is no support for
  79. /// making rounded polygons.
  80. float m_radius;
  81. };
  82. inline b2Shape::Type b2Shape::GetType() const
  83. {
  84. return m_type;
  85. }
  86. #endif