I’m trying to create a ball using this.matter.add.circle(…) and apply some velocity to it. I tried using applyForce(), changing velocity directly but that did not work. I tried to console.log(ball.body) and it came out as “undefined” even though the circle shows up on the screen.
import { Boot } from './scenes/Boot';
import { Game } from './scenes/Game';
import { GameOver } from './scenes/GameOver';
import { MainMenu } from './scenes/MainMenu';
import { Preloader } from './scenes/Preloader';
import Phaser from 'phaser';
// Find out more information about the Game Config at:
// https://newdocs.phaser.io/docs/3.70.0/Phaser.Types.Core.GameConfig
const config = {
type: Phaser.AUTO,
width: 1024,
height: 768,
parent: 'game-container',
backgroundColor: '#028af8',
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
},
physics: {
default: 'matter',
matter: {
gravity: { y: 0.5 },
debug: true
}
},
scene: [
Boot,
Preloader,
MainMenu,
Game,
GameOver
]
};
export default new Phaser.Game(config);
import { Scene } from 'phaser';
export class Game extends Scene
{
constructor ()
{
super('Game');
}
create ()
{
this.add.image(512, 384, 'background');
this.matter.world.setBounds(0, 0, 1024, 768);
const ball = this.matter.add.circle(512, 384, 50, {
restitution: 0.8,
frictionAir: 0.02,
});
console.log(ball.body);
const startPos = new Phaser.Math.Vector2(0, 0);
const endPos = new Phaser.Math.Vector2(0, 0);
this.input.on('pointerdown', pointer => {
startPos.set(pointer.x, pointer.y);
})
this.input.on("pointerup", pointer => {
endPos.set(pointer.x, pointer.y);
this.mouseVector = new Phaser.Math.Vector2((endPos.x - startPos.x), (endPos.y - startPos.y));
const force = this.mouseVector.normalize().scale(0.005);
ball.velocity.x = force.x;
ball.velocity.y = force.y;
})
}
}