2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
22 #include "../../Common/b2Math.h"
23 #include "../b2Collision.h"
25 class b2BlockAllocator;
29 /// This holds the mass data computed for a shape.
32 /// The mass of the shape, usually in kilograms.
35 /// The position of the shape's centroid relative to the shape's origin.
38 /// The rotational inertia of the shape.
42 /// This holds contact filtering data.
45 /// The collision category bits. Normally you would just set one bit.
48 /// The collision mask bits. This states the categories that this
49 /// shape would accept for collision.
52 /// Collision groups allow a certain group of objects to never collide (negative)
53 /// or always collide (positive). Zero means no collision group. Non-zero group
54 /// filtering always wins against the mask bits.
58 /// The various collision shape types supported by Box2D.
67 /// A shape definition is used to construct a shape. This class defines an
68 /// abstract shape definition. You can reuse shape definitions safely.
71 /// The constructor sets the default shape definition values.
74 type = e_unknownShape;
79 filter.categoryBits = 0x0001;
80 filter.maskBits = 0xFFFF;
81 filter.groupIndex = 0;
85 virtual ~b2ShapeDef() {}
87 /// Holds the shape type for down-casting.
90 /// Use this to store application specify shape data.
93 /// The shape's friction coefficient, usually in the range [0,1].
96 /// The shape's restitution (elasticity) usually in the range [0,1].
99 /// The shape's density, usually in kg/m^2.
102 /// A sensor shape collects contact information but never generates a collision
106 /// Contact filtering data.
110 /// A shape is used for collision detection. Shapes are created in b2World.
111 /// You can use shape for collision detection before they are attached to the world.
112 /// @warning you cannot reuse shapes.
116 /// Get the type of this shape. You can use this to down cast to the concrete shape.
117 /// @return the shape type.
118 b2ShapeType GetType() const;
120 /// Is this shape a sensor (non-solid)?
121 /// @return the true if the shape is a sensor.
122 bool IsSensor() const;
124 /// Set the contact filtering data. You must call b2World::Refilter to correct
125 /// existing contacts/non-contacts.
126 void SetFilterData(const b2FilterData& filter);
128 /// Get the contact filtering data.
129 const b2FilterData& GetFilterData() const;
131 /// Get the parent body of this shape. This is NULL if the shape is not attached.
132 /// @return the parent body.
135 /// Get the next shape in the parent body's shape list.
136 /// @return the next shape.
139 /// Get the user data that was assigned in the shape definition. Use this to
140 /// store your application specific data.
143 /// Set the user data. Use this to store your application specific data.
144 void SetUserData(void* data);
146 /// Test a point for containment in this shape. This only works for convex shapes.
147 /// @param xf the shape world transform.
148 /// @param p a point in world coordinates.
149 virtual bool TestPoint(const b2XForm& xf, const b2Vec2& p) const = 0;
151 /// Perform a ray cast against this shape.
152 /// @param xf the shape world transform.
153 /// @param lambda returns the hit fraction. You can use this to compute the contact point
154 /// p = (1 - lambda) * segment.p1 + lambda * segment.p2.
155 /// @param normal returns the normal at the contact point. If there is no intersection, the normal
157 /// @param segment defines the begin and end point of the ray cast.
158 /// @param maxLambda a number typically in the range [0,1].
159 /// @return true if there was an intersection.
160 virtual bool TestSegment( const b2XForm& xf,
163 const b2Segment& segment,
164 float32 maxLambda) const = 0;
166 /// Given a transform, compute the associated axis aligned bounding box for this shape.
167 /// @param aabb returns the axis aligned box.
168 /// @param xf the world transform of the shape.
169 virtual void ComputeAABB(b2AABB* aabb, const b2XForm& xf) const = 0;
171 /// Given two transforms, compute the associated swept axis aligned bounding box for this shape.
172 /// @param aabb returns the axis aligned box.
173 /// @param xf1 the starting shape world transform.
174 /// @param xf2 the ending shape world transform.
175 virtual void ComputeSweptAABB( b2AABB* aabb,
177 const b2XForm& xf2) const = 0;
179 /// Compute the mass properties of this shape using its dimensions and density.
180 /// The inertia tensor is computed about the local origin, not the centroid.
181 /// @param massData returns the mass data for this shape.
182 virtual void ComputeMass(b2MassData* massData) const = 0;
184 /// Get the maximum radius about the parent body's center of mass.
185 float32 GetSweepRadius() const;
187 /// Get the coefficient of friction.
188 float32 GetFriction() const;
190 /// Get the coefficient of restitution.
191 float32 GetRestitution() const;
196 friend class b2World;
198 static b2Shape* Create(const b2ShapeDef* def, b2BlockAllocator* allocator);
199 static void Destroy(b2Shape* shape, b2BlockAllocator* allocator);
201 b2Shape(const b2ShapeDef* def);
204 void CreateProxy(b2BroadPhase* broadPhase, const b2XForm& xf);
205 void DestroyProxy(b2BroadPhase* broadPhase);
206 bool Synchronize(b2BroadPhase* broadPhase, const b2XForm& xf1, const b2XForm& xf2);
207 void RefilterProxy(b2BroadPhase* broadPhase, const b2XForm& xf);
209 virtual void UpdateSweepRadius(const b2Vec2& center) = 0;
215 // Sweep radius relative to the parent body's center of mass.
216 float32 m_sweepRadius;
220 float32 m_restitution;
223 b2FilterData m_filter;
230 inline b2ShapeType b2Shape::GetType() const
235 inline bool b2Shape::IsSensor() const
240 inline void b2Shape::SetFilterData(const b2FilterData& filter)
245 inline const b2FilterData& b2Shape::GetFilterData() const
250 inline void* b2Shape::GetUserData()
255 inline void b2Shape::SetUserData(void* data)
260 inline b2Body* b2Shape::GetBody()
265 inline b2Shape* b2Shape::GetNext()
270 inline float32 b2Shape::GetSweepRadius() const
272 return m_sweepRadius;
275 inline float32 b2Shape::GetFriction() const
280 inline float32 b2Shape::GetRestitution() const
282 return m_restitution;