After we have added physics to the project, we need to tell the physics engine about all the objects that will be enabled for physics processing. Let’s start with platforms. Add physical bodies to the created platforms and thus let the physics engine know about the platforms.
What is the physical body of the platform? In fact, the platform is a rectangular sprite.
In order to create the physical body of the platform that the engine can process, we need to create a rectangle that exactly matches the outline of the platform. We can accurately calculate the size and position of such a rectangle by taking the coordinates and dimensions of the current platform.
In the Platform.js file:
import * as Matter from 'matter-js';
export class Platform {
constructor(rows, cols, x) {
// ...
// specify the speed of the platform
this.dx = App.config.platforms.moveSpeed;
this.createBody();
}
createBody() {
// create a physical body
this.body = Matter.Bodies.rectangle(this.width / 2 + this.container.x, this.height / 2 + this.container.y, this.width, this.height, {friction: 0, isStatic: true});
// add the created body to the engine
Matter.World.add(App.physics.world, this.body);
// save a reference to the platform object itself for further access from the physical body object
this.body.gamePlatform = this;
}
}
And let’s take out the value of the platforms speed into the global config for the convenience of configuration:
export const Config = {
// ...
platforms: {
// ...
moveSpeed: -1.5
}
// ...
};