File indexing completed on 2025-08-03 03:49:55

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_SHAPE_H
0020 #define B2_SHAPE_H
0021 
0022 #include <Box2D/Common/b2BlockAllocator.h>
0023 #include <Box2D/Common/b2Math.h>
0024 #include <Box2D/Collision/b2Collision.h>
0025 
0026 /// This holds the mass data computed for a shape.
0027 struct b2MassData
0028 {
0029     /// The mass of the shape, usually in kilograms.
0030     qreal mass;
0031 
0032     /// The position of the shape's centroid relative to the shape's origin.
0033     b2Vec2 center;
0034 
0035     /// The rotational inertia of the shape about the local origin.
0036     qreal I;
0037 };
0038 
0039 /// A shape is used for collision detection. You can create a shape however you like.
0040 /// Shapes used for simulation in b2World are created automatically when a b2Fixture
0041 /// is created. Shapes may encapsulate a one or more child shapes.
0042 class b2Shape
0043 {
0044 public:
0045     
0046     enum Type
0047     {
0048         e_unknown= -1,
0049         e_circle = 0,
0050         e_edge = 1,
0051         e_polygon = 2,
0052         e_loop = 3,
0053         e_typeCount = 4
0054     };
0055 
0056     b2Shape() { m_type = e_unknown; }
0057     virtual ~b2Shape() {}
0058 
0059     /// Clone the concrete shape using the provided allocator.
0060     virtual b2Shape* Clone(b2BlockAllocator* allocator) const = 0;
0061 
0062     /// Get the type of this shape. You can use this to down cast to the concrete shape.
0063     /// @return the shape type.
0064     Type GetType() const;
0065 
0066     /// Get the number of child primitives.
0067     virtual int32 GetChildCount() const = 0;
0068 
0069     /// Test a point for containment in this shape. This only works for convex shapes.
0070     /// @param xf the shape world transform.
0071     /// @param p a point in world coordinates.
0072     virtual bool TestPoint(const b2Transform& xf, const b2Vec2& p) const = 0;
0073 
0074     /// Cast a ray against a child shape.
0075     /// @param output the ray-cast results.
0076     /// @param input the ray-cast input parameters.
0077     /// @param transform the transform to be applied to the shape.
0078     /// @param childIndex the child shape index
0079     virtual bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
0080                         const b2Transform& transform, int32 childIndex) const = 0;
0081 
0082     /// Given a transform, compute the associated axis aligned bounding box for a child shape.
0083     /// @param aabb returns the axis aligned box.
0084     /// @param xf the world transform of the shape.
0085     /// @param childIndex the child shape
0086     virtual void ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const = 0;
0087 
0088     /// Compute the mass properties of this shape using its dimensions and density.
0089     /// The inertia tensor is computed about the local origin.
0090     /// @param massData returns the mass data for this shape.
0091     /// @param density the density in kilograms per meter squared.
0092     virtual void ComputeMass(b2MassData* massData, qreal density) const = 0;
0093 
0094     Type m_type;
0095     qreal m_radius;
0096 };
0097 
0098 inline b2Shape::Type b2Shape::GetType() const
0099 {
0100     return m_type;
0101 }
0102 
0103 #endif