xxxxxxxxxx
82
class Ball{
constructor(x,y,z,c,radius){
this.position = new p5.Vector(x, y,z);
this.vel = new p5.Vector(0, 0);
this.acc = p5.Vector.random3D();
this.radius = radius;
this.c = c;
this.rry = [];
}
update(){
this.acc = p5.Vector.random3D();
this.acc.normalize();
this.acc.mult(random(1,3));
this.rry.unshift(createVector(this.position.x,this.position.y,this.position.z));
this.vel.add(this.acc);
this.vel.limit(8);
this.position.add(this.vel);
}
displayWorm(){
let tmp = this.rry.splice(0,10);
push();
beginShape();
noFill();
strokeWeight(2);
stroke(this.c);
for(let i =0; i < tmp.length; i+=1){
let x = tmp[i].x;
let y = tmp[i].y;
let z = tmp[i].z;
push();
if(i == 0){
translate(x,y,z);
fill(this.c);
sphere(this.radius);
pop();
}
curveVertex(x,y,z);
}
endShape();
pop()
this.rry = [];
this.rry = tmp;
}
getLocation(){
return this.position;
}
checkEdges() {
if (this.position.x > width - this.radius) {
this.position.x = width - this.radius;
this.vel.x *= -1;
} else if (this.position.x < -width) {
this.vel.x *= -1;
} else if (this.position.y > height - this.radius) {
this.position.y = height - this.radius;
this.vel.y *= -1;
} else if (this.position.y < -height) {
this.vel.y *= -1;
}else if(this.position.z > width - this.radius){
this.position.z = width - this.radius;
this.vel.z *= -1;
}else if(this.position.z < -width){
this.vel.z *= -1;
}
}
}