源码
/**
* Creates a new rigid body model with a rectangle hull.
* The options parameter is an object that specifies any properties you wish to override the defaults.
* See the properties section of the `Matter.Body` module for detailed information on what you can pass via the `options` object.
* @method rectangle
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
* @param {object} [options]
* @return {body} A new rectangle body
*/
Bodies.rectangle = function(x, y, width, height, options) {
options = options || {};
var rectangle = {
label: 'Rectangle Body',
position: { x: x, y: y },
vertices: Vertices.fromPath('L 0 0 L ' + width + ' 0 L ' + width + ' ' + height + ' L 0 ' + height)
};
if (options.chamfer) {
var chamfer = options.chamfer;
rectangle.vertices = Vertices.chamfer(rectangle.vertices, chamfer.radius,
chamfer.quality, chamfer.qualityMin, chamfer.qualityMax);
delete options.chamfer;
}
return Body.create(Common.extend({}, rectangle, options));
};
在Matter中,坐标系是左上角坐标系,所以x,y参数的值也是左上角坐标系的值,
另外在这个方法中(x,y)的坐标指的是矩形的中心点。
如下图,该矩形的x,y都是150。width是指x方向的长度,height是指y方向的长度。
而options参数是一个表,其中可以设置刚体的各种属性,例如上一章设置的isStatic属性,
该属性设置的是,是否收到物理世界的重力影响,如果是isStatic为true,则代表刚体不受重力影响