-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.js
47 lines (42 loc) · 1.21 KB
/
asteroid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Asteroid(){
this.pos = createVector(random(width),random(height));
this.vel = p5.Vector.random2D();
this.r = random(15, 50);
this.total = floor(random(5,15));
this.offset = [];
for (var i = 0; i< this.total; i++){
this.offset[i] = random(-10,10);
}
this.update = function(){
this.pos.add(this.vel);
}
this.render = function(){
push();
stroke(255);
noFill();
translate(this.pos.x, this.pos.y);
//ellipse(0,0, this.r * 2);
beginShape();
for(var i=0; i < this.total; i++){
var angle = map (i, 0, this.total, 0, TWO_PI);
var r = this.r + this.offset[i];
var x = r*cos(angle);
var y = r*sin(angle);
vertex(x, y);
}
endShape(CLOSE);
pop();
}
this.edges = function(){
if (this.pos.x > width + this.r){
this.pos.x = -this.r;
} else if (this.pos.x < -this.r){
this.pos.x = width + this.r;
}
if (this.pos.y > height + this.r){
this.pos.y = -this.r;
} else if (this.pos.y < -this.r){
this.pos.y = height + this.r;
}
}
}