Get an array of dots to oscillate only when a marker passes them
in
Programming Questions
•
1 year ago
Hi,
I am trying to simulate how particles behave in a sound wave.
I have a random array of dots which oscillate horizontally. But I'd like the oscillation of each to only begin when their x-coordinate is the same as that of a red dot which travels horizontally across the screen.
The code for the dots all oscillating together is,
and to try to get them to only oscillate when the red dot passes using the 'if' statement on line 20:
I don't know why the array of dots can't be seen in the second code.
I'd really appreicate any help.
Thanks,
Shane
I am trying to simulate how particles behave in a sound wave.
I have a random array of dots which oscillate horizontally. But I'd like the oscillation of each to only begin when their x-coordinate is the same as that of a red dot which travels horizontally across the screen.
The code for the dots all oscillating together is,
- int numDots = 10;
- float x, angle, frequency = PI;
- float xpos;
- float vel = 0.1;
- int[]ArrayX = new int[numDots];
- int[]ArrayY = new int[numDots];
- void setup() {
- size (600, 300);
- for (int i = 0; i < numDots; i++) {
- ArrayX[i] = (int) random(0, width);
- ArrayY[i] = (int) random(0, height);
- }
- }
- void draw() {
- background (127);
- stroke(0);
- fill(0);
- for (int i = 0; i < numDots; i++) {
- x = sin(radians(angle))*50;
- ellipse(ArrayX[i]+x, ArrayY[i], 3, 3);
- angle -= frequency/32;
- noStroke();
- fill(250, 0, 0);
- ellipse(xpos, 100, 5, 5);
- stroke(0);
- fill(0);
- xpos += vel;
- }
- }
and to try to get them to only oscillate when the red dot passes using the 'if' statement on line 20:
- int numDots = 10;
- float x, angle, frequency = PI;
- float xpos;
- float vel = 0.1;
- int[]ArrayX = new int[numDots];
- int[]ArrayY = new int[numDots];
- void setup() {
- size (600, 300);
- for (int i = 0; i < numDots; i++) {
- ArrayX[i] = (int) random(0, width);
- ArrayY[i] = (int) random(0, height);
- }
- }
- void draw() {
- background (127);
- stroke(0);
- fill(0);
- for (int i = 0; i < numDots; i++) {
- angle -= frequency/32;
- if (xpos==ArrayX[i]) {
- x = sin(radians(angle))*50;
- ellipse(ArrayX[i]+x, ArrayY[i], 3, 3);
- }
- noStroke();
- fill(250, 0, 0);
- ellipse(xpos, 100, 5, 5);
- stroke(0);
- fill(0);
- xpos += vel;
- }
- }
I don't know why the array of dots can't be seen in the second code.
I'd really appreicate any help.
Thanks,
Shane
1