improved map side segregation moves

This commit is contained in:
Bachir Soussi Chiadmi 2023-04-26 12:36:02 +02:00
parent 72d79f8d84
commit 0c2a594fb9

View File

@ -159,7 +159,7 @@ export default {
frictionAir: 0, frictionAir: 0,
// mass: Math.pow(3, this.entites.length), // mass: Math.pow(3, this.entites.length),
mass: 10, mass: 10,
restitution: 0.1, restitution: 0.15,
collisionFilter: { collisionFilter: {
group: -1 group: -1
}, },
@ -336,18 +336,14 @@ export default {
if (this.tween) { if (this.tween) {
this.tween.update(); this.tween.update();
} }
},
onAfterEngineUpdate (event) {
// respawn element if outside screen
if(this.pos.x < 0
|| this.pos.x > this.canvas.width
|| this.pos.y < 0
|| this.pos.y > this.canvas.height){
this.pos = this.getRandomPos()
Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
}
// map_mode action
if (this.map_mode === 'action'){ if (this.map_mode === 'action'){
this.applyActionForces();
}
Matter.Body.setAngle(this.body, 0);
Matter.Body.setAngularSpeed(this.body, 0);
},
applyActionForces(){
// map_mode action
var dist, dir, x_force, ori_pos; var dist, dir, x_force, ori_pos;
var x_force = 0; var x_force = 0;
if(!this.concernement.has_agissantes) { if(!this.concernement.has_agissantes) {
@ -356,14 +352,15 @@ export default {
// the force is exponentialy proportional to the distance from the side // the force is exponentialy proportional to the distance from the side
dir = this.pos.x > this.canvas.width/2 ? 1 : -1; // get the direction to the closest side dir = this.pos.x > this.canvas.width/2 ? 1 : -1; // get the direction to the closest side
dist = (dir < 0 ? this.pos.x : this.canvas.width - this.pos.x); // get the distance from the side dist = (dir < 0 ? this.pos.x : this.canvas.width - this.pos.x); // get the distance from the side
ori_pos = {x:this.canvas.width/2, y:this.canvas.height/2}; ori_pos = {x:this.canvas.width/2, y:this.body.position.y};
x_force = Math.pow(dist/700,10) * dir; x_force = Math.pow(dist/700,10) * dir;
}else{ }else{
// has action, get to the centre // has action, get to the centre
dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
ori_pos = dir < 0 ? {x:this.canvas.width, y:this.canvas.height/2} : {x:0, y:this.canvas.height/2} ori_pos = dir < 0 ? {x:this.canvas.width, y:this.body.position.y} : {x:0, y:this.body.position.y}
x_force = Math.pow(dist/800,10) * dir; x_force = Math.pow(dist/800,10) * dir;
this.body.frictionAir = 0.05;
} }
// x_force = (dist > 200 ? Math.pow(dist/700,10) : 0) * dir // x_force = (dist > 200 ? Math.pow(dist/700,10) : 0) * dir
Matter.Body.applyForce( Matter.Body.applyForce(
@ -374,9 +371,17 @@ export default {
y: 0 y: 0
} }
); );
},
onAfterEngineUpdate (event) {
// respawn element if outside screen
if(this.pos.x < 0
|| this.pos.x > this.canvas.width
|| this.pos.y < 0
|| this.pos.y > this.canvas.height){
this.pos = this.getRandomPos()
Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
} }
Matter.Body.setAngle(this.body, 0);
Matter.Body.setAngularSpeed(this.body, 0);
this.draw() this.draw()
}, },
draw() { draw() {