File indexing completed on 2025-02-16 03:29:32
0001 /* 0002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 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 float32 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 float32 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_circle = 0, 0049 e_edge = 1, 0050 e_polygon = 2, 0051 e_chain = 3, 0052 e_typeCount = 4 0053 }; 0054 0055 virtual ~b2Shape() {} 0056 0057 /// Clone the concrete shape using the provided allocator. 0058 virtual b2Shape* Clone(b2BlockAllocator* allocator) const = 0; 0059 0060 /// Get the type of this shape. You can use this to down cast to the concrete shape. 0061 /// @return the shape type. 0062 Type GetType() const; 0063 0064 /// Get the number of child primitives. 0065 virtual int32 GetChildCount() const = 0; 0066 0067 /// Test a point for containment in this shape. This only works for convex shapes. 0068 /// @param xf the shape world transform. 0069 /// @param p a point in world coordinates. 0070 virtual bool TestPoint(const b2Transform& xf, const b2Vec2& p) const = 0; 0071 0072 /// Cast a ray against a child shape. 0073 /// @param output the ray-cast results. 0074 /// @param input the ray-cast input parameters. 0075 /// @param transform the transform to be applied to the shape. 0076 /// @param childIndex the child shape index 0077 virtual bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, 0078 const b2Transform& transform, int32 childIndex) const = 0; 0079 0080 /// Given a transform, compute the associated axis aligned bounding box for a child shape. 0081 /// @param aabb returns the axis aligned box. 0082 /// @param xf the world transform of the shape. 0083 /// @param childIndex the child shape 0084 virtual void ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const = 0; 0085 0086 /// Compute the mass properties of this shape using its dimensions and density. 0087 /// The inertia tensor is computed about the local origin. 0088 /// @param massData returns the mass data for this shape. 0089 /// @param density the density in kilograms per meter squared. 0090 virtual void ComputeMass(b2MassData* massData, float32 density) const = 0; 0091 0092 Type m_type; 0093 float32 m_radius; 0094 }; 0095 0096 inline b2Shape::Type b2Shape::GetType() const 0097 { 0098 return m_type; 0099 } 0100 0101 #endif