外观
Matter 物理
Phaser 4 指南 · 英文原文
Matter 物理是一个开源第三方物理库,Phaser有它自己的自定义版本捆绑。 加入物质的理由是它提供了一个更先进的“ 完整的” 物理系统。 如果您需要超越矩形和圆形, 物理形状更复杂, 以及约束、 关节和行为等特征, 那么物质就是要使用的系统 。
Matter 物理必须启用后才能使用。 可以通过游戏配置或每场游戏完成。 一旦启用, 您可以在游戏中添加物理化的游戏对象。 这将允许您使用内置物理函数来控制 Sprite, 如速度、 加速、 重力等 。
默认情况下, 无法为物理启用游戏对象。 这是因为并非所有游戏对象都需要。 例如, 背景图像或游戏标志可能不需要受到物理影响, 而玩家字符需要。 因此, 您必须启用您特别希望受其影响的游戏对象上的物理。 我们将在后面章节中详细论述这个问题 。
Arcade 物理和Matter 物理是两个独立的系统。例如,Arcade 物理图案不能与Matter 物理图案相撞。不能在两个系统上添加相同的Sprite,你需要选择一个或另一个。尽管这样做是不寻常的,但这两个系统实际上可以在同一场景中平行运行。这意味着可以有一个使用Arcade物理的Sprite和一个使用Matter 物理的Sprite,它们将同时工作,尽管它们不会一起互动。
物质世界
Matter 物理学世界提供了比Arcade物理更先进和更现实的物理相互作用模拟。 它在 Matter.js 库上建构了这样的模拟,它允许更复杂的物理行为,如刚性的物理体动力学、约束和高级碰撞探测。
世界组合
Matter 物理config对象定义了Matter 物理系统在全球的行为.
- 基本配置对象
js
const config = {
// ...
physics: {
default: "matter",
},
// ...
};
const game = new Phaser.Game(config);- 完整配置
js
var config = {
// ...
physics: {
default: "matter",
matter: {
// enabled: true,
// positionIterations: 6,
// velocityIterations: 4,
// constraintIterations: 2,
// enableSleeping: false,
// plugins: {
// attractors: false,
// wrap: false,
// },
// gravity: {
// x: 0,
// y: 0,
// }
// setBounds: {
// x: 0,
// y: 0,
// width: scene.sys.scale.width,
// height: scene.sys.scale.height,
// thickness: 64,
// left: true,
// right: true,
// top: true,
// bottom: true,
// },
// timing: {
// timestamp: 0,
// timeScale: 1,
// },
// correction: 1,
// getDelta: (function() { return 1000 / 60; }),
// autoUpdate: true,
// debug: false,
// debug: {
// showAxes: false,
// showAngleIndicator: false,
// angleColor: 0xe81153,
// showBroadphase: false,
// broadphaseColor: 0xffb400,
// showBounds: false,
// boundsColor: 0xffffff,
// showVelocity: false,
// velocityColor: 0x00aeef,
// showCollisions: false,
// collisionColor: 0xf5950c,
// showSeparations: false,
// separationColor: 0xffa500,
// showBody: true,
// showStaticBody: true,
// showInternalEdges: false,
// renderFill: false,
// renderLine: true,
// fillColor: 0x106909,
// fillOpacity: 1,
// lineColor: 0x28de19,
// lineOpacity: 1,
// lineThickness: 1,
// staticFillColor: 0x0d177b,
// staticLineColor: 0x1327e4,
// showSleeping: false,
// staticBodySleepOpacity: 0.7,
// sleepFillColor: 0x464646,
// sleepLineColor: 0x999a99,
// showSensors: true,
// sensorFillColor: 0x0d177b,
// sensorLineColor: 0x1327e4,
// showPositions: true,
// positionSize: 4,
// positionColor: 0xe042da,
// showJoint: true,
// jointColor: 0xe0e042,
// jointLineOpacity: 1,
// jointLineThickness: 2,
// pinSize: 4,
// pinColor: 0x42e0e0,
// springColor: 0xe042e0,
// anchorColor: 0xefefef,
// anchorSize: 4,
// showConvexHulls: false,
// hullColor: 0xd703d0
// }
},
},
// ...
};
var game = new Phaser.Game(config);控制
暂停
js
this.matter.world.pause();继续
js
this.matter.world.resume();拖动对象
js
this.matter.add.mouseSpring();
// this.matter.add.mouseSpring(options);optionsjs{ length: 0.01, stiffness: 0.1, damping: 0, angularStiffness: 1, collisionFilter: { category: 0x0001, mask: 0xFFFFFFFF, group: 0 } }collisionFilter: 拖动过滤器,参见 collision.
世界界限
启用
World :
设定界限
```js this.matter.world.setBounds(x, y, width, height); // this.matter.world.setBounds(x, y, width, height, thickness, left, right, top, bottom); ```thickness:每面壁的厚度,以像素为单位.left,right,top,bottom:如果真能创建左/右/上/下界壁.
重力
Set
jsthis.matter.world.setGravity(x, y); // this.matter.world.setGravity(x, y, scale);Disable
jsthis.matter.world.disableGravity();
约束
约束两个游戏对象
js
var constraint = this.matter.add.constraint(gameObjectA, gameObjectB);
// var constraint = this.matter.add.constraint(gameObjectA, gameObjectB, length, stiffness, options);gameObjectA,gameObjectB:物质游戏对象,或物质物理体对象.length: 约束的剩余目标长度。undefined:当前gameObjectA和gameObjectB之间的距离。 (默认值)
stiffness: 约束的僵硬性.1: 非常僵硬。 (默认值)0.2:扮演柔软的弹簧.
options:js{ pointA: { x: 0, y: 0, }, pointB: { x: 0, y: 0, }, damping: 0, angularStiffness: 0, // render: { // visible: true // } }pointA,pointB: 偏移位置( 单位:千米)gameObjectA,gameObjectB.
Alias:
js
var constraint = this.matter.add.spring(
gameObjectA,
gameObjectB,
length,
stiffness,
options
);
var constraint = this.matter.add.joint(
gameObjectA,
gameObjectB,
length,
stiffness,
options
);限制在世界位置上
js
var constraint = this.matter.add.worldConstraint(
gameObjectB,
length,
stiffness,
options
);gameObjectB:物质游戏对象,或物质体对象.length: 约束的剩余目标长度。undefined:当前gameObjectA和gameObjectB之间的距离。 (默认值)
stiffness: 约束的僵硬性.1: 非常僵硬。 (默认值)0.2:扮演柔软的弹簧.
options:js{ pointA: { x: 0, y: 0, }, pointB: { x: 0, y: 0, }, damping: 0, angularStiffness: 0, // render: { // visible: true // } }pointA: 世界立场.pointB: 偏移位置( 单位:千米)gameObjectB.
链式游戏对象
js
var composite = this.matter.add.chain(
composite,
xOffsetA,
yOffsetA,
xOffsetB,
yOffsetB,
options
);composite: Image compositexOffsetA,yOffsetA: 偏移位置( 单位:千米) gameObjectA,在比例上。- xOffset = (离相/宽)
- yOffset = (偏差/ 高度)
xOffsetB,yOffsetB: 偏移位置( 单位:千米) gameObjectB,在比例上。options:js{ length: undefined, stiffness: 1, damping: 0, angularStiffness: 0, // render: { // visible: true // } }length: 约束的剩余目标长度。undefined:当前gameObjectA和gameObjectB之间的距离。 (默认值)
stiffness: 约束的僵硬性.1: 非常僵硬。 (默认值)0.2:扮演柔软的弹簧.
compositecomposite.bodies: 一系列物理体。composite.constraints:一系列制约因素
删除约束
js
this.matter.world.removeConstraint(constraint);物质游戏对象
游戏对象是您的游戏的构件。常见的物质游戏对象包括:Sprites, Images和TileBody 。
用法
添加游戏对象
图像对象
js
var image = this.matter.add.image(x, y, key, frame);
// var image = this.matter.add.image(x, y, key, frame, config);config: Config object
精灵对象
js
var image = this.matter.add.sprite(x, y, key, frame);
// var image = this.matter.add.sprite(x, y, key, frame, config);config: Config object
任意游戏对象
js
var gameObject = this.matter.add.gameObject(gameObject);
// var gameObject = this.matter.add.gameObject(gameObject, config);config: Config object
图像复合材料
创建包含在网格安排中创建的 Matter Image 对象的新复合材料 。
js
var composite = this.matter.add.imageStack(key, frame, x, y, columns, rows);
// var composite = this.matter.add.imageStack(key, frame, x, y, columns, rows, columnGap, rowGap, options);key,frame:纹理键和帧名称.x,y:这些游戏对象的左上方位置.columns,rows: 网格中的列/行数.columnGap,rowGap: 每个列/行之间的距离。config: Config objectcomposite: 复合物质对象。composite.bodies: 一系列物理体。
游戏对象配置
js
{
label: 'Body',
shape: 'rectangle',
chamfer: null,
isStatic: false,
isSensor: false,
isSleeping: false,
ignoreGravity: false,
ignorePointer: false,
sleepThreshold: 60,
density: 0.001,
restitution: 0,
friction: 0.1,
frictionStatic: 0.5,
frictionAir: 0.01,
force: { x: 0, y: 0 },
angle: 0,
torque: 0,
collisionFilter: {
group: 0,
category: 0x0001,
mask: 0xFFFFFFFF,
},
// parts: [],
// plugin: {
// attractors: [
// (function(bodyA, bodyB) { return {x, y}}),
// ]
// },
slop: 0.05,
timeScale: 1,
}label:一个任意的String名称,帮助用户识别和管理物理体.shape:字符串 :
'rectangle','circle','trapezoid','polygon','fromVertices','fromPhysicsEditor'对象 :
矩形形状
```js { type: 'rectangle', // width: gameObject.width // height: gameObject.height } ```圆形
```js { type: 'circle', // radius: (Math.max(gameObject.width, gameObject.height) / 2), // maxSides: 25 } ```拖动形状
```js { type: 'trapezoid', // slope: 0.5, } ```多边形形状
```js { type: 'polygon', // radius: (Math.max(gameObject.width, gameObject.height) / 2), // sides: 5, } ```Vertices
```js { type: 'fromVertices', verts: points, // flagInternal: false, // removeCollinear: 0.01, // minimumArea: 10, } ```points:- 字符串:
'x0 y0 x1 y1 ...' - 数组点数 :
[{x:x0, y:y0}, {x:x1, y:y1}, ...]
- 字符串:
chamfer:null- 数字
{radius: value}{radius: [topLeft, topRight, bottomRight, bottomLeft]}
isStatic: 表示一个物理体是否被视为静止的旗帜,一个静止的物理体永远不能改变位置或角度,并且是完全固定的.isSensor:指向物理体是否是传感器的旗帜,传感器触发碰撞事件,但不会与碰撞物理体发生物理反应.isSleeping:表示物理体是否被视为睡眠的旗帜,睡眠的物理体行为类似于静态的物理体,但只是暂时的,可以震动.sleepThreshold:在设定为睡眠前,这个物理体必须拥有_近零速度_的更新数量.density: 物理体密度,即单位面积的质量。restitution: 物理体的还原/弹跳/弹性。数值总是正值,在(0, 1)的范围之内。0:碰撞可能完全没有弹性,并且不会发生波澜.0.8:物理体可能以大约80%的动能反弹.
friction: 物理体的滑动。 数值总是正值, 并且范围是 。(0, 1).0:物理体可能无限期滑动.1: 施用武力后,物理体几乎会立即停机。
frictionStatic:物理体的静电摩擦(在库伦布摩擦模型中).0:物理体在接近静止状态时,只有动态状态时,就不会"棒"friction用于使用。10:值越高,在接近固定状态时,它会用更多的力量来让物理体开始运动.
frictionAir:物理体的空气摩擦(空气阻力).0:物理体在太空中移动时永远不会减速.- 值越高,一个物理体在穿越空间时速度越快.
force: 适用于当前步骤的力。angle:物理体角,以弧度表示.torque:当前步骤中应用的扭矩(转力).collisionFilter:一个指定了这个机体碰撞过滤特性的Object.collisionFilter.groupcollisionFilter.category: 指定此物理体所属碰撞类别的位字段 。collisionFilter.mask:一个指定了物理体可能碰撞的碰撞类别的小遮罩.
slop: 容忍一个物理体被允许"沉入"或旋转到其它物理体的距离.- 默认一般应足够,尽管非常大的体可能需要更大的值来进行稳定的堆叠.
游戏对象碰撞
两个机构之间的碰撞将遵守以下规则:
- (grpupA > 0) && (groupB > 0) :
- 碰撞=( 缩写)groupA == groupB)
- (grpupA == 0) || (groupB == 0) :
- 碰撞=((()categoryA & maskB) !== 0) && ((categoryB & maskA) !== 0)
移动
速度
js
gameObject.setVelocity(x, y);js
gameObject.setVelocityX(x);js
gameObject.setVelocityY(x);加速
强制
将力量应用到物理体上.
jsgameObject.applyForce(force);force:{x, y}
从特定位置对物理体施加力量.
jsgameObject.applyForceFrom(position, force); // position, force: {x, y}position:{x, y}force:{x, y}
将推力应用到物理体的前进位置.
jsgameObject.thrust(speed);speed: 一个数字。
将推力应用到物理体的左侧位置.
jsgameObject.thrustLeft(speed);speed: 一个数字。
将推力应用到物理体的正确位置.
jsgameObject.thrustRight(speed);speed: 一个数字。
将推力应用到物理体的后方位置.
jsgameObject.thrustBack(speed);speed: 一个数字。
将推力应用到物理体的后方位置.
jsgameObject.thrustBack(speed);speed: 一个数字。
重力
忽略世界引力
jsgameObject.setIgnoreGravity(ignore);ignore: 设定为真实,以忽略世界引力的影响
摩擦力
js
gameObject.setFriction(value, air, fstatic);js
gameObject.setFrictionAir(v);js
gameObject.setFrictionStatic(v);旋转
固定轮换
js
gameObject.setFixedRotation();角速度
js
gameObject.setAngularVelocity(v);碰撞
启用
Get
jsvar isSensor = gameObject.isSensor();Set
jsgameObject.setSensor(value);value设置true以启用传感器。
碰撞组
碰撞 grpup
jsgameObject.setCollisionGroup(value);碰撞类别
获取新的碰撞类别
jsvar category = this.matter.world.nextCategory();category: 一发号码(1, 2, 4, 8, ...., 2147483648 (1<<31))
设定游戏对象的碰撞类别
jsgameObject.setCollisionCategory(category);设置分类掩码
jsgameObject.setCollidesWith([categoryA, categoryB, ...]); // gameObject.setCollidesWith(categoryA);
碰撞事件
js
this.matter.world.on("collisionstart", function (event, bodyA, bodyB) {
// var pairs = event.pairs;
});event:event.pairs: 一系列碰撞对event.pairs[i].bodyA,event.pairs[i].bodyB:物质物体。body.gameObject: 物质体的游戏对象.
bodyA,bodyB: 等于event.pairs[0].bodyA,event.pairs[0].bodyB.
碰撞绑定
Rectangle
jsgameObject.setRectangle(width, height, options);Circle
jsgameObject.setCircle(radius, options);Polygon
jsgameObject.setPolygon(radius, sides, options);Trapezoid
jsgameObject.setTrapezoid(width, height, slope, options);Any
jsgameObject.setBody(config, options);config:矩形形状
```js { type: 'rectangle', // width: gameObject.width // height: gameObject.height } ```圆形
```js { type: 'circle', // radius: (Math.max(gameObject.width, gameObject.height) / 2), // maxSides: 25 } ```拖动形状
```js { type: 'trapezoid', // slope: 0.5, } ```多边形形状
```js { type: 'polygon', // radius: (Math.max(gameObject.width, gameObject.height) / 2), // sides: 5, } ```
弹跳
js
gameObject.setBounce(v);- restitution
质量
js
gameObject.setMass(v);js
gameObject.setDensity(v);睡觉
启用
js
var config = {
// ...
physics: {
matter: {
// ...
enableSleeping: true,
// ...
},
},
};睡眠门槛
js
gameObject.setSleepThreshold(value);睡眠活动
睡觉开始
jsthis.matter.world.on("sleepstart", function (event, body) {});沉睡的结束
jsthis.matter.world.on("sleepend", function (event, body) {});
作者署名
本页包含以下作者贡献的内容。