b2Shape.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #ifndef B2_SHAPE_H
  20. #define B2_SHAPE_H
  21. #include <Box2D/Common/b2BlockAllocator.h>
  22. #include <Box2D/Common/b2Math.h>
  23. #include <Box2D/Collision/b2Collision.h>
  24. /// This holds the mass data computed for a shape.
  25. struct b2MassData
  26. {
  27. /// The mass of the shape, usually in kilograms.
  28. float32 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. float32 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 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. /// Compute the distance from the current shape to the specified point. This only works for convex shapes.
  61. /// @param xf the shape world transform.
  62. /// @param p a point in world coordinates.
  63. /// @param distance returns the distance from the current shape.
  64. /// @param normal returns the direction in which the distance increases.
  65. virtual void ComputeDistance(const b2Transform& xf, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const= 0;
  66. /// Cast a ray against a child shape.
  67. /// @param output the ray-cast results.
  68. /// @param input the ray-cast input parameters.
  69. /// @param transform the transform to be applied to the shape.
  70. /// @param childIndex the child shape index
  71. virtual bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  72. const b2Transform& transform, int32 childIndex) const = 0;
  73. /// Given a transform, compute the associated axis aligned bounding box for a child shape.
  74. /// @param aabb returns the axis aligned box.
  75. /// @param xf the world transform of the shape.
  76. /// @param childIndex the child shape
  77. virtual void ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const = 0;
  78. /// Compute the mass properties of this shape using its dimensions and density.
  79. /// The inertia tensor is computed about the local origin.
  80. /// @param massData returns the mass data for this shape.
  81. /// @param density the density in kilograms per meter squared.
  82. virtual void ComputeMass(b2MassData* massData, float32 density) const = 0;
  83. Type m_type;
  84. float32 m_radius;
  85. };
  86. inline b2Shape::Type b2Shape::GetType() const
  87. {
  88. return m_type;
  89. }
  90. #endif