-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBall
126 lines (111 loc) · 2.5 KB
/
Ball
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class Ball
{
AudioPlayer player;
float X = 0;
float Y = 0;
PVector speed;
PVector speedDir = new PVector(1,1);
float radius = 100;
boolean drawLines = false;
color waveColor;
int incr = 0;
boolean followMouse = false;
Ball(AudioPlayer player, float X, float Y)
{
this.player = player;
this.X = X;
this.Y = Y;
}
void draw()
{
//move the ball based on the wave
float avg = 0;
int j = 0;
for (int i = 0; i< player.bufferSize() - 1 && i < width; i++)
{
j = i + 1;
avg += player.mix.get(i);
}
avg = abs(avg / j);
speed = new PVector(1000 * avg, 1000 * avg);
//println(avg * 100);
///////////
if (drawLines)
{
DrawWithLines();
DrawWithShape();
}
else DrawWithShape();
if (followMouse)
{
X = mouseX;
Y = mouseY;
}
else
{
X += speed.x * speedDir.x;
Y += speed.y * speedDir.y;
WallCollision();
}
incr+= 1;
if (incr > 360) incr = 0;
}
void DrawWithLines()
{
float sampleIncr = player.bufferSize() / 360;
for (int i = 0; i < 360; i++)
{
float ballRadius = 100 + player.mix.get(int(i * sampleIncr)) * 100;
float angle = radians(i);
float x = X + ballRadius * cos(angle);
float y = Y + ballRadius * sin(angle);
if (i + incr > 360) waveColor = color(i + (incr - 360), 255, 255);
else waveColor = color(i + incr, 255, 255);
stroke(waveColor);
line(X, Y, x, y);
}
if (incr > 360) incr = 0;
}
void DrawWithShape()
{
float sampleIncr = player.bufferSize() / 360;
if (incr > 360) waveColor = color((incr - 360), 255, 255);
else waveColor = color(incr, 255, 255);
stroke(waveColor);
if (!drawLines) fill(0);
beginShape();
for (int i = 0; i < 360; i++)
{
float ballRadius = 100 + player.mix.get(int(i * sampleIncr)) * 100;
float angle = radians(i);
float x = X + ballRadius * cos(angle);
float y = Y + ballRadius * sin(angle);
vertex(x, y);
//fill(100 +player.mix.get(int(i * sampleIncr)) * 100,0,255 -(player.mix.get(int(i * sampleIncr)) * 100));
}
endShape();
}
void WallCollision()
{
if (X > width)
{
X = width;
speedDir.x = -speedDir.x;
}
if (X < 0)
{
X = 0;
speedDir.x = -speedDir.x;
}
if (Y > height)
{
Y = height;
speedDir.y = -speedDir.y;
}
if (Y < 0)
{
Y = 0;
speedDir.y = -speedDir.y;
}
}
}//End Class