xxxxxxxxxx
78
class Planet {
constructor(x, y, mass, speedX, speedY, name=-1) {
if(name==-1){
this.mass = mass;
this.size = map(mass, 0, 20000, 0, 100);
this.vSpeed = createVector(speedX, speedY);
}
else{
this.mass = DATA[name]['mass'];
this.rayon = DATA[name]['rayon'];
this.size = map(this.rayon, DATA['earth']['rayon'], DATA['sun']['rayon'], 5, 100);
this.vSpeed = createVector(0, DATA[name]['peri_speed']);
this.vSpeed.mult(DATA['SCALE_SPEED']);
this.life = 0;
this.last_life = 0;
this.name = name;
this.binaryShow = true;
}
this.start_pos = createVector(-DATA[name]['peri_dist'], 0);
this.pos = this.start_pos.copy();
this.last_pos = this.pos.copy();
planets.push(this);
}
show(){
strokeWeight(this.size);
let scale = DATA['SCALE_POS']
point(this.pos.x*scale,this.pos.y*scale);
// console.log(this.pos.x*scale,this.pos.y*scale);
}
update(planet2){
let a = dist(this.pos.x, this.pos.y, this.start_pos.x, this.start_pos.y)
let b = dist(this.last_pos.x, this.last_pos.y, this.start_pos.x, this.start_pos.y)
this.last_pos = this.pos.copy();
if(a-b>0 && this.binaryShow){
console.log(this.life-this.last_life-1, this.name);
this.last_life = this.life;
this.binaryShow = false;
}
if(a-b < 0){
this.binaryShow = true;
}
// console.log(this.life);
let acc = createVector(planet2.pos.x-this.pos.x, planet2.pos.y-this.pos.y);
// line(this.pos.x, this.pos.y, acc.x, acc.y);
// console.log(planet2.mass, acc, acc.mag());
let newMag = (G*planet2.mass)/(acc.mag()*acc.mag());
acc.normalize();
acc.mult(newMag);
// console.log(acc);
let scale_speed = DATA['SCALE_SPEED'];
// console.log(acc, this.vSpeed);
acc.mult(scale_speed**2);
this.vSpeed.add(acc);
// console.log(this.vSpeed);
this.pos.add(this.vSpeed);
this.life++;
// acc.mult(-5);
// stroke('red');
// strokeWeight(5);
// line(this.pos.x, this.pos.y, this.pos.x-acc.x, this.pos.y-acc.y);
// line(this.pos.x, this.pos.y, this.pos.x-this.vSpeed.x, this.pos.y-this.vSpeed.y);
// line(this.pos.x, this.pos.y, this.vSpeed.x-this.pos.x, this.vSpeed.y-this.pos.y);
}
}