本文来自http://blog.youkuaiyun.com/runaying ,引用必须注明出处!
cocos2d-x节点(b2PolygonShape.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
我们这里说的多边形是凸多边形,所谓凸多边形就是一条直线与该类多边形相交交点最多不超过两个。同时还是实心的,不能是空心。多边形的顶点范围应该在[3,b2_maxPolygonVertices]内,box2d中定义为8个,你也可以在公共模块的b2Settings.h文件中修改它的值,不过一般不建议那么做。
///cocos2d-x-3.0alpha0/external/Box2D/Collision/Shapes
//我们这里说的多边形是凸多边形,所谓凸多边形就是一条直线与该类多边形相交交点最多不超过两个。同时还是实心的,不能是空心。多边形的顶点范围应该在[3,b2_maxPolygonVertices]内,box2d中定义为8个,你也可以在公共模块的b2Settings.h文件中修改它的值,不过一般不建议那么做。
#ifndef B2_POLYGON_SHAPE_H
#define B2_POLYGON_SHAPE_H
#include <Box2D/Collision/Shapes/b2Shape.h>
///一个凸多边形
// 多边形最多有b2_maxPolygonVertices个顶点
//在大多数情况下你不需要具有太多顶点的多边形
class b2PolygonShape : public b2Shape
{
public:
b2PolygonShape(); //多边形构造函数,初始化数据
// 用soa块分配器克隆一个具体的形状
// * 参数说明: allocator :soa分配器对象指针
/// Implement b2Shape.
b2Shape* Clone(b2BlockAllocator* allocator) const;
// 获取孩子形状个数,你可以使用它去创建形状
// * 参数说明: (void)
// * 返 回 值: 孩子形状个数
/// @see b2Shape::GetChildCount
int32 GetChildCount() const;
// 复制顶点。假定所有的顶点定义了一个凸多边形
// 它假定了外部每一个边都是从右边开始的
// 顶点数在【3,b2_maxPolygonVertices】之间
// * 参数说明: vertices :顶点数组
// vertexCount :顶点数量
void Set(const b2Vec2* vertices, int32 vertexCount);
// 构建一个包围着顶点轴对齐盒子
// * 参数说明: hx :半宽
// hy :半高
void SetAsBox(float32 hx, float32 hy);
// 构建一个包围着顶点确定方位的轴对齐盒子
// * 参数说明: hx :半宽
// hy :半高
// center:盒子的中心点
// angle :盒子的旋转角度
void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
// 在这个形状中测试这个点的密封性,只适合用于凸的形状
// * 参数说明: xf : 形状的变换
// p : world坐标中的一个点
// * 返 回 值: true : 密封
// false:敞开
/// @see b2Shape::TestPoint
bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
// 投射一束光到一个孩子形状中
// * 参数说明: output :输出光线投射的结果
// input :输入光线投射
// transform :变换应用到此形状中
// childeIndex :孩子形状索引
// * 返 回 值: true : 成功
// false:失败
/// Implement b2Shape.
bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
const b2Transform& transform, int32 childIndex) const;
// 给出一个变换,计算一个孩子形状的轴对齐包围盒(aabb)
// * 参数说明: aabb : 孩子形状的aabb指针
// xf : 一个变换的引用
// childIndex : 孩子的索引值
/// @see b2Shape::ComputeAABB
void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
// 用它的大小和密度计算形状的质量
// * 参数说明: massData : 计算形状的质量
// density : 密度
/// @see b2Shape::ComputeMass
void ComputeMass(b2MassData* massData, float32 density) const;
///获取顶点数量
int32 GetVertexCount() const { return m_vertexCount; }
/// 根据顶点索引获取一个顶点
// * 参数说明: index :索引值
const b2Vec2& GetVertex(int32 index) const;
b2Vec2 m_centroid; //重心坐标
b2Vec2 m_vertices[b2_maxPolygonVertices]; //顶点坐标数组
b2Vec2 m_normals[b2_maxPolygonVertices]; //法线数组
int32 m_vertexCount; //顶点数量
};
//多边形构造函数,初始化数据
inline b2PolygonShape::b2PolygonShape()
{
m_type = e_polygon;
m_radius = b2_polygonRadius;
m_vertexCount = 0;
m_centroid.SetZero();
}
//根据顶点索引获取一个顶点
inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
{
//验证index的有效性
b2Assert(0 <= index && index < m_vertexCount);
return m_vertices[index];
}
#endif