I instantiate a bunch of extended Phaser.Physics.Arcade.Sprite that move around the screen. When they hit each others, everything works has expected with the basic arcade body.
Although, It doesn't if I add "this.body.setCircle(200);" (or "this.setCircle(200);" btw, I'm not sure about the difference) in their constructor. Instead of smoothly deflecting on impact as I would have hoped, they stop immediately.
Here's the code of my class:
import Phaser from "phaser";
export default class Sheep extends Phaser.Physics.Arcade.Sprite {
target: Phaser.Math.Vector2 = new Phaser.Math.Vector2();
speed: number = 50;
reach: boolean = true;
constructor(scene: Phaser.Scene, x: number, y: number, spriteKey: string) {
super(scene, x, y, spriteKey);
this.setOrigin(0, 0);
scene.physics.add.existing(this);
scene.add.existing(this);
this.setCircle(5);
this.visible = false;
//this.scale = 0.05;
//this.setBounce(1);
}
setRandomTarget() {
this.target = new Phaser.Math.Vector2(
Math.random() * this.scene.scale.width,
Math.random() * this.scene.scale.height
);
}
preUpdate(time: number, delta: number) {
super.preUpdate(delta, time);
if (
this.reach ||
this.target.distance(new Phaser.Math.Vector2(this.x, this.y)) <=
this.speed
) {
this.setRandomTarget();
this.reach = false;
this.setHeading();
}
}
setHeading() {
let vPos = new Phaser.Math.Vector2(this.x, this.y);
let vTarget = this.target.clone();
let velocity = vTarget.subtract(vPos).normalize().scale(this.speed);
this.setVelocity(velocity.x, velocity.y);
}
}
Here's the code to create them and add them to the scene:
for (let i = 0; i < 10; i++) {
this.sheeps.push(
new Sheep(
this,
Math.round(Math.random() * width),
Math.round(Math.random() * height),
"sheep2"
)
);
}
let sheepGroup = this.physics.add.group(this.sheeps);
this.physics.add.collider(sheepGroup, sheepGroup);
Here's the result: Circular collision Rectangular collision