Box2D  2.2.0
A 2D Physics Engine for Games
b2Fixture.h
00001 /*
00002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #ifndef B2_FIXTURE_H
00020 #define B2_FIXTURE_H
00021 
00022 #include <Box2D/Dynamics/b2Body.h>
00023 #include <Box2D/Collision/b2Collision.h>
00024 #include <Box2D/Collision/Shapes/b2Shape.h>
00025 
00026 class b2BlockAllocator;
00027 class b2Body;
00028 class b2BroadPhase;
00029 class b2Fixture;
00030 
00032 struct b2Filter
00033 {
00035         uint16 categoryBits;
00036 
00039         uint16 maskBits;
00040 
00044         int16 groupIndex;
00045 };
00046 
00049 struct b2FixtureDef
00050 {
00052         b2FixtureDef()
00053         {
00054                 shape = NULL;
00055                 userData = NULL;
00056                 friction = 0.2f;
00057                 restitution = 0.0f;
00058                 density = 0.0f;
00059                 filter.categoryBits = 0x0001;
00060                 filter.maskBits = 0xFFFF;
00061                 filter.groupIndex = 0;
00062                 isSensor = false;
00063         }
00064 
00067         const b2Shape* shape;
00068 
00070         void* userData;
00071 
00073         float32 friction;
00074 
00076         float32 restitution;
00077 
00079         float32 density;
00080 
00083         bool isSensor;
00084 
00086         b2Filter filter;
00087 };
00088 
00090 struct b2FixtureProxy
00091 {
00092         b2AABB aabb;
00093         b2Fixture* fixture;
00094         int32 childIndex;
00095         int32 proxyId;
00096 };
00097 
00103 class b2Fixture
00104 {
00105 public:
00108         b2Shape::Type GetType() const;
00109 
00113         b2Shape* GetShape();
00114         const b2Shape* GetShape() const;
00115 
00117         void SetSensor(bool sensor);
00118 
00121         bool IsSensor() const;
00122 
00126         void SetFilterData(const b2Filter& filter);
00127 
00129         const b2Filter& GetFilterData() const;
00130 
00132         void Refilter();
00133 
00136         b2Body* GetBody();
00137         const b2Body* GetBody() const;
00138 
00141         b2Fixture* GetNext();
00142         const b2Fixture* GetNext() const;
00143 
00146         void* GetUserData() const;
00147 
00149         void SetUserData(void* data);
00150 
00153         bool TestPoint(const b2Vec2& p) const;
00154 
00158         bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
00159 
00163         void GetMassData(b2MassData* massData) const;
00164 
00167         void SetDensity(float32 density);
00168 
00170         float32 GetDensity() const;
00171 
00173         float32 GetFriction() const;
00174 
00177         void SetFriction(float32 friction);
00178 
00180         float32 GetRestitution() const;
00181 
00184         void SetRestitution(float32 restitution);
00185 
00189         const b2AABB& GetAABB(int32 childIndex) const;
00190 
00191 protected:
00192 
00193         friend class b2Body;
00194         friend class b2World;
00195         friend class b2Contact;
00196         friend class b2ContactManager;
00197 
00198         b2Fixture();
00199 
00200         // We need separation create/destroy functions from the constructor/destructor because
00201         // the destructor cannot access the allocator (no destructor arguments allowed by C++).
00202         void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
00203         void Destroy(b2BlockAllocator* allocator);
00204 
00205         // These support body activation/deactivation.
00206         void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
00207         void DestroyProxies(b2BroadPhase* broadPhase);
00208 
00209         void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
00210 
00211         float32 m_density;
00212 
00213         b2Fixture* m_next;
00214         b2Body* m_body;
00215 
00216         b2Shape* m_shape;
00217 
00218         float32 m_friction;
00219         float32 m_restitution;
00220 
00221         b2FixtureProxy* m_proxies;
00222         int32 m_proxyCount;
00223 
00224         b2Filter m_filter;
00225 
00226         bool m_isSensor;
00227 
00228         void* m_userData;
00229 };
00230 
00231 inline b2Shape::Type b2Fixture::GetType() const
00232 {
00233         return m_shape->GetType();
00234 }
00235 
00236 inline b2Shape* b2Fixture::GetShape()
00237 {
00238         return m_shape;
00239 }
00240 
00241 inline const b2Shape* b2Fixture::GetShape() const
00242 {
00243         return m_shape;
00244 }
00245 
00246 inline bool b2Fixture::IsSensor() const
00247 {
00248         return m_isSensor;
00249 }
00250 
00251 inline const b2Filter& b2Fixture::GetFilterData() const
00252 {
00253         return m_filter;
00254 }
00255 
00256 inline void* b2Fixture::GetUserData() const
00257 {
00258         return m_userData;
00259 }
00260 
00261 inline void b2Fixture::SetUserData(void* data)
00262 {
00263         m_userData = data;
00264 }
00265 
00266 inline b2Body* b2Fixture::GetBody()
00267 {
00268         return m_body;
00269 }
00270 
00271 inline const b2Body* b2Fixture::GetBody() const
00272 {
00273         return m_body;
00274 }
00275 
00276 inline b2Fixture* b2Fixture::GetNext()
00277 {
00278         return m_next;
00279 }
00280 
00281 inline const b2Fixture* b2Fixture::GetNext() const
00282 {
00283         return m_next;
00284 }
00285 
00286 inline void b2Fixture::SetDensity(float32 density)
00287 {
00288         b2Assert(b2IsValid(density) && density >= 0.0f);
00289         m_density = density;
00290 }
00291 
00292 inline float32 b2Fixture::GetDensity() const
00293 {
00294         return m_density;
00295 }
00296 
00297 inline float32 b2Fixture::GetFriction() const
00298 {
00299         return m_friction;
00300 }
00301 
00302 inline void b2Fixture::SetFriction(float32 friction)
00303 {
00304         m_friction = friction;
00305 }
00306 
00307 inline float32 b2Fixture::GetRestitution() const
00308 {
00309         return m_restitution;
00310 }
00311 
00312 inline void b2Fixture::SetRestitution(float32 restitution)
00313 {
00314         m_restitution = restitution;
00315 }
00316 
00317 inline bool b2Fixture::TestPoint(const b2Vec2& p) const
00318 {
00319         return m_shape->TestPoint(m_body->GetTransform(), p);
00320 }
00321 
00322 inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
00323 {
00324         return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
00325 }
00326 
00327 inline void b2Fixture::GetMassData(b2MassData* massData) const
00328 {
00329         m_shape->ComputeMass(massData, m_density);
00330 }
00331 
00332 inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
00333 {
00334         b2Assert(0 <= childIndex && childIndex < m_proxyCount);
00335         return m_proxies[childIndex].aabb;
00336 }
00337 
00338 #endif
 All Classes Files Functions Variables Enumerations Enumerator Defines