PhysicsBody::createEdgeBox()的定义如下:
PhysicsBody::createEdgeBox(const Size& size, const PhysicsMaterial& material, float border/* = 1*/, const Vec2& offset)
第一个参数是Size类型,可以传入如visibleSize等参数。
第二个参数是一个PhysicsMaterial类型的参数,这个参数有三个成员变量,float dentity, float restitution, float friction.分别代表了密度,弹性系数和摩擦系数。密度即为物理学上的密度,取值为0或正数。
摩擦系数在0.0-1.0之间,0.0表示无摩擦力,1.0表示强摩擦力。
弹性系数在0.0-1.0之间,0.0表示物体不会弹起,1.0表示物体被完全弹起,即弹性碰撞。
这个参数经常会用一个常量PHYSICSBODY_MATERIAL_DEFAULT替代,这个常量定义如下:
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
即把密度设为0.1,弹性系数设为0.5,摩擦系数设为0.5.
第三个参数是边框的宽度。
第四个参数API中的解释是A Vec2 object, it is the offset from the body’s center of gravity in body local coordinates.
在创建边框时用到了该参数:
cpVect vec[4] = {};
vec[0] = PhysicsHelper::point2cpv(Vec2(-size.width/2+offset.x, -size.height/2+offset.y));
vec[1] = PhysicsHelper::point2cpv(Vec2(+size.width/2+offset.x, -size.height/2+offset.y));
vec[2] = PhysicsHelper::point2cpv(Vec2(+size.width/2+offset.x, +size.height/2+offset.y));
vec[3] = PhysicsHelper::point2cpv(Vec2(-size.width/2+offset.x, +size.height/2+offset.y));
可以看出,该参数的作用是,边框会整体移动该变量的数值。如当参数设置为Vec2(100,100)时,边框会整体向右向上各移动100的距离。