#ifndef B2_FIXTURE_H
#define B2_FIXTURE_H
#include "b2_api.h"
#include "b2_body.h"
#include "b2_collision.h"
#include "b2_shape.h"
class b2BlockAllocator;
class b2Body;
class b2BroadPhase;
class b2Fixture;
struct B2_API b2Filter
{
b2Filter()
{
categoryBits = 0x0001;
maskBits = 0xFFFF;
groupIndex = 0;
}
uint16 categoryBits;
uint16 maskBits;
int16 groupIndex;
};
struct B2_API b2FixtureDef
{
b2FixtureDef()
{
shape = nullptr;
friction = 0.2f;
restitution = 0.0f;
restitutionThreshold = 1.0f * b2_lengthUnitsPerMeter;
density = 0.0f;
isSensor = false;
}
const b2Shape* shape;
b2FixtureUserData userData;
float friction;
float restitution;
float restitutionThreshold;
float density;
bool isSensor;
b2Filter filter;
};
struct B2_API b2FixtureProxy
{
b2AABB aabb;
b2Fixture* fixture;
int32 childIndex;
int32 proxyId;
};
class B2_API b2Fixture
{
public:
b2Shape::Type GetType() const;
b2Shape* GetShape();
const b2Shape* GetShape() const;
void SetSensor(bool sensor);
bool IsSensor() const;
void SetFilterData(const b2Filter& filter);
const b2Filter& GetFilterData() const;
void Refilter();
b2Body* GetBody();
const b2Body* GetBody() const;
b2Fixture* GetNext();
const b2Fixture* GetNext() const;
b2FixtureUserData& GetUserData();
bool TestPoint(const b2Vec2& p) const;
void ComputeDistance(const b2Vec2& p, float* distance, b2Vec2* normal, int32 childIndex) const;
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
void GetMassData(b2MassData* massData) const;
void SetDensity(float density);
float GetDensity() const;
float GetFriction() const;
void SetFriction(float friction);
float GetRestitution() const;
void SetRestitution(float restitution);
float GetRestitutionThreshold() const;
void SetRestitutionThreshold(float threshold);
const b2AABB& GetAABB(int32 childIndex) const;
void Dump(int32 bodyIndex);
protected:
friend class b2Body;
friend class b2World;
friend class b2Contact;
friend class b2ContactManager;
b2Fixture();
void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
void Destroy(b2BlockAllocator* allocator);
void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
void DestroyProxies(b2BroadPhase* broadPhase);
void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
float m_density;
b2Fixture* m_next;
b2Body* m_body;
b2Shape* m_shape;
float m_friction;
float m_restitution;
float m_restitutionThreshold;
b2FixtureProxy* m_proxies;
int32 m_proxyCount;
b2Filter m_filter;
bool m_isSensor;
b2FixtureUserData m_userData;
};
inline b2Shape::Type b2Fixture::GetType() const
{
return m_shape->GetType();
}
inline b2Shape* b2Fixture::GetShape()
{
return m_shape;
}
inline const b2Shape* b2Fixture::GetShape() const
{
return m_shape;
}
inline bool b2Fixture::IsSensor() const
{
return m_isSensor;
}
inline const b2Filter& b2Fixture::GetFilterData() const
{
return m_filter;
}
inline b2FixtureUserData& b2Fixture::GetUserData()
{
return m_userData;
}
inline b2Body* b2Fixture::GetBody()
{
return m_body;
}
inline const b2Body* b2Fixture::GetBody() const
{
return m_body;
}
inline b2Fixture* b2Fixture::GetNext()
{
return m_next;
}
inline const b2Fixture* b2Fixture::GetNext() const
{
return m_next;
}
inline void b2Fixture::SetDensity(float density)
{
b2Assert(b2IsValid(density) && density >= 0.0f);
m_density = density;
}
inline float b2Fixture::GetDensity() const
{
return m_density;
}
inline float b2Fixture::GetFriction() const
{
return m_friction;
}
inline void b2Fixture::SetFriction(float friction)
{
m_friction = friction;
}
inline float b2Fixture::GetRestitution() const
{
return m_restitution;
}
inline void b2Fixture::SetRestitution(float restitution)
{
m_restitution = restitution;
}
inline float b2Fixture::GetRestitutionThreshold() const
{
return m_restitutionThreshold;
}
inline void b2Fixture::SetRestitutionThreshold(float threshold)
{
m_restitutionThreshold = threshold;
}
inline bool b2Fixture::TestPoint(const b2Vec2& p) const
{
return m_shape->TestPoint(m_body->GetTransform(), p);
}
inline void b2Fixture::ComputeDistance(const b2Vec2& p, float* d, b2Vec2* n, int32 childIndex) const
{
m_shape->ComputeDistance(m_body->GetTransform(), p, d, n, childIndex);
}
inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
{
return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
}
inline void b2Fixture::GetMassData(b2MassData* massData) const
{
m_shape->ComputeMass(massData, m_density);
}
inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
{
b2Assert(0 <= childIndex && childIndex < m_proxyCount);
return m_proxies[childIndex].aabb;
}
#endif