-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteriod.pde
72 lines (64 loc) · 1.64 KB
/
Asteriod.pde
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Asteriod {
PVector pos, vel;
int numVertices;
float[] radii;
PShape asteriod;
float offset, inc, rotateAng, angVel, maxRadius, avgRadius, threshold;
Asteriod(PVector loc, float r) {
pos = loc;
numVertices = int(random(5, 13));
maxRadius = (r==0) ? (random(125, 175)) : r;
radii = new float[numVertices];
inc = random(5);
angVel = random(-.05, .05);
threshold = 20;
if (maxRadius < 50) {
vel = PVector.sub(ship.pos, pos);
vel.normalize();
} else {
vel = PVector.random2D();
}
vel.mult(random(1.3, 3.3));
for (int i = 0; i < radii.length; i++) {
radii[i] = noise(offset)*maxRadius;
offset+=inc;
avgRadius+=radii[i];
}
avgRadius = avgRadius / radii.length;
createAsteriod();
}
void update() {
pos.add(vel);
createAsteriod();
rotateAng+=angVel;
}
void edges() {
if (pos.x > width+avgRadius) pos.x = -avgRadius;
if (pos.x < -avgRadius) pos.x = width+avgRadius;
if (pos.y < -avgRadius) pos.y = height+avgRadius;
if (pos.y > height+avgRadius) pos.y = -avgRadius;
}
void show() {
pushMatrix();
translate(pos.x, pos.y);
rotate(rotateAng);
shape(asteriod);
popMatrix();
}
void createAsteriod() {
pushStyle();
asteriod = createShape();
asteriod.beginShape();
asteriod.noFill();
asteriod.stroke(255);
asteriod.strokeWeight(2);
for (int i = 0; i < numVertices; i++) {
float x, y, angle = map(i, 0, numVertices, 0, TWO_PI);
x = radii[i] * cos(angle);
y = radii[i] * sin(angle);
asteriod.vertex(x, y);
}
asteriod.endShape(CLOSE);
popStyle();
}
}