本文来自http://blog.csdn.net/runaying ,引用必须注明出处!
cocos2d-x节点(b2Fixture.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
//对物理世界中的对象进行详细定制例如:密度,摩擦细数,恢复系数、过滤,投射光线
///cocos2d-x-3.0alpha0/external/Box2D/Dynamics
//对物理世界中的对象进行详细定制例如: 密度,摩擦细数,恢复系数、过滤,投射光线
#ifndef B2_FIXTURE_H
#define B2_FIXTURE_H
#include <Box2D/Dynamics/b2Body.h>
#include <Box2D/Collision/b2Collision.h>
#include <Box2D/Collision/Shapes/b2Shape.h>
class b2BlockAllocator;
class b2Body;
class b2BroadPhase;
class b2Fixture;
///他拥有联系过滤数据
struct b2Filter
{
b2Filter()
{
categoryBits = 0x0001;
maskBits = 0xFFFF;
groupIndex = 0;
}
/// 碰撞类别 bits. 通常情况下你会设置一个 bit.
uint16 categoryBits;
/// 碰撞屏蔽 bits.他指出了 shape 接受的碰撞类型
uint16 maskBits;
//碰撞组允许一组特定的对象 从来都不碰撞(反向)或者 一直碰撞(正向)零意味着没有碰撞组
///Non-zero 过滤是针对屏蔽位
int16 groupIndex;
};
/// 一个 fixture(定制器)定义用于创建一个 fixture(定制器). 这个类定义了一个抽象的 fixture(定制器),你可以放心的重用这个 fixture(定制器)
struct b2FixtureDef
{
//此构造函数设置定义的 fixture (使用默认值).
b2FixtureDef()
{
shape = NULL;
userData = NULL;
friction = 0.2f;
restitution = 0.0f;
density = 0.0f;
isSensor = false;
}
/// shape必须被设置. 这个 shape 会被克隆,所以你可以在堆里面创建这个 shape
const b2Shape* shape;
/// 使用这个存储应用程序特定fixture (定制器)数据
void* userData;
/// 摩擦系数,通常的范围为 [0,1].
float32 friction;
/// 恢复(弹性)原状的范围 [0,1].
float32 restitution;
/// 密度, 通常以 kg/m^2 为单位.
float32 density;
/// shape 的传感器,收集形状信息但是不会发生碰撞响应
bool isSensor;
/// Contact filtering data.
b2Filter filter;
};
/// 这个代理内部使用连接 connect(定制器)到 broad-phase.
struct b2FixtureProxy
{
b2AABB aabb;
b2Fixture* fixture;
int32 childIndex;
int32 proxyId;
};
/// 一个与 shape 关联的 fixture(定制器)用于 body 的碰撞检测.
//一个 fixture(定制器)从他的 parent 继承了转换。 fixture(定制器)持有其他非几何数据,如摩擦,碰撞过滤器等
/// Fixtures(定制器)是通过 b2Body::CreateFixture 的 .
/// @warning 你不能重用这个 fixtures(定制器).
class b2Fixture
{
public:
/// 获取 child shape的类型. 你可以使用这个把它向下准缓为具体的形状
/// @return the shape type.
b2Shape::Type GetType() const;
/// 获取 child shape. 你可以修改这个 child shape,然而你不应该改变顶点数目,因为它可能会让一些碰撞缓存机制崩溃
///操纵的形状可能会导致非物理行为
b2Shape* GetShape();
const b2Shape* GetShape() const;
/// Set 如果这个 fixture(定制器)是一个传感器
void SetSensor(bool sensor);
/// 这个 fixture(定制器)是一个传感器 (non-solid)?
/// @return true 如果 shape 是一个传感器
bool IsSensor() const;v
/// Set 过滤的联系数据. 无论这个 body 是否 active 、 awake 这都将不会更新联系,直到下一个时间步长
/// 这将自动调用重新筛选。
void SetFilterData(const b2Filter& filter);
/// Get 过滤的联系数据.
const b2Filter& GetFilterData() const;
/// 如果你想创建先前使用 b2ContactFilter::ShouldCollide 禁用的碰撞,可以调用这个.
void Refilter();
/// Get 这个 fixture(定制器) parent body . 如果 fixture(定制器)没有连接他就是 NULL
/// @return the parent body.
b2Body* GetBody();
const b2Body* GetBody() const;
// 获取 parent body's 的 fixture(定制器)列表里面的下一个 fixture(定制器)
/// @return 下一个 shape.
b2Fixture* GetNext();
const b2Fixture* GetNext() const;
/// 获取在 fixture(定制器)定义时分配的用户数据. 使用这个获取应用程序特定的数据。
void* GetUserData() const;
/// 设置的用户数据。使用这个存储应用程序特定的数据。
void SetUserData(void* data);
/// 这个 fixture(定制器)的测试点容量.
/// @param p 一个 world 坐标系中的点.
bool TestPoint(const b2Vec2& p) const;
/// 针对这个形状的投射光线
/// @param output 光线投射结果.
/// @param input 光线投射的输入参数。
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
/// Get 这个 fixture(定制器)的质量.质量数据是基于密度和形状的
///转动惯量与 shape's 的原点有关,这个操作可能是非常昂贵的
void GetMassData(b2MassData* massData) const;
/// Set 这个 fixture(定制器)的密度. 这将不会自动调整 body 的质量,你必须调用 b2Body::ResetMassData 更新 body's 的质量.
void SetDensity(float32 density);
/// Get 这个 fixture(定制器)的密度.
float32 GetDensity() const;
/// Get 摩擦系数.
float32 GetFriction() const;
/// Set 摩擦系数. 这将不会改变现有联系的摩擦系数
void SetFriction(float32 friction);
/// Get 恢复系数.
float32 GetRestitution() const;
///设置恢复系数. 这将不会改变现有联系的恢复系数
void SetRestitution(float32 restitution);
///获取这个 fixture's(定制器)的 AABB. 这个 AABB 可以放大 and/or 过时.
/// 如果你需要一个更准确的 AABB, 你可以使用 shape、body 转换计算
const b2AABB& GetAABB(int32 childIndex) const;
/// 转存这个 fixture(定制器)到日志文件中
void Dump(int32 bodyIndex);
protected:
friend class b2Body;
friend class b2World;
friend class b2Contact;
friend class b2ContactManager;
b2Fixture();
// 我们需要从 构造/析构函数 里面分离 创建/销毁 函数,因为析构函数不能访问分配器( C++ 允许没有析构函数参数).
void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
void Destroy(b2BlockAllocator* allocator);
// 这些 body 支持 激活/停用.
void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
void DestroyProxies(b2BroadPhase* broadPhase);
void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
float32 m_density;
b2Fixture* m_next;
b2Body* m_body;
b2Shape* m_shape;
float32 m_friction;
float32 m_restitution;
b2FixtureProxy* m_proxies;
int32 m_proxyCount;
b2Filter m_filter;
bool m_isSensor;
void* 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 void* b2Fixture::GetUserData() const
{
return m_userData;
}
inline void b2Fixture::SetUserData(void* data)
{
m_userData = data;
}
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(float32 density)
{
b2Assert(b2IsValid(density) && density >= 0.0f);
m_density = density;
}
inline float32 b2Fixture::GetDensity() const
{
return m_density;
}
inline float32 b2Fixture::GetFriction() const
{
return m_friction;
}
inline void b2Fixture::SetFriction(float32 friction)
{
m_friction = friction;
}
inline float32 b2Fixture::GetRestitution() const
{
return m_restitution;
}
inline void b2Fixture::SetRestitution(float32 restitution)
{
m_restitution = restitution;
}
inline bool b2Fixture::TestPoint(const b2Vec2& p) const
{
return m_shape->TestPoint(m_body->GetTransform(), p);
}
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