// initialize maxAccel (around 0.0025), maxRadSpeed (around 0.125), ballRadius (in pixels) and // direction of acceleration (1 or -1) StringBall sb_1 = new StringBall(0.0038, 0.19, 6, 1); StringBall sb_2 = new StringBall(0.0026, 0.13, 9, -1); StringBall sb_3 = new StringBall(0.0022, 0.11, 12, 1); StringBall sb_4 = new StringBall(0.0018, 0.09, 15, -1); void setup() { size(750, 750); //frameRate(36); noStroke(); smooth(); } //public int count = 0; void draw() { background(255); sb_1.update(); sb_2.update(); sb_3.update(); sb_4.update(); //println( + sb_3.speedFactor + " " + sb_3.stringLength); while (dist(sb_1.xpos, sb_1.ypos, sb_2.xpos, sb_2.ypos) <= (sb_1.ballRadius + sb_2.ballRadius) && (sb_1.stringLength > 0.125 && sb_2.stringLength > 0.125)) { sb_1.radSpeed *= -1; break; } while (dist(sb_1.xpos, sb_1.ypos, sb_3.xpos, sb_3.ypos) <= (sb_1.ballRadius + sb_3.ballRadius) && (sb_1.stringLength > 0.125 && sb_3.stringLength > 0.125)) { sb_1.radSpeed *= -1; break; } while (dist(sb_2.xpos, sb_2.ypos, sb_3.xpos, sb_3.ypos) <= (sb_2.ballRadius + sb_3.ballRadius) && (sb_2.stringLength > 0.125 && sb_3.stringLength > 0.125)) { sb_2.radSpeed *= -1; break; } while (dist(sb_1.xpos, sb_1.ypos, sb_4.xpos, sb_4.ypos) <= (sb_1.ballRadius + sb_4.ballRadius) && (sb_1.stringLength > 0.125 && sb_4.stringLength > 0.125)) { sb_4.radSpeed *= -1; break; } while (dist(sb_2.xpos, sb_2.ypos, sb_4.xpos, sb_4.ypos) <= (sb_2.ballRadius + sb_4.ballRadius) && (sb_2.stringLength > 0.125 && sb_4.stringLength > 0.125)) { sb_2.radSpeed *= -1; break; } while (dist(sb_3.xpos, sb_3.ypos, sb_4.xpos, sb_4.ypos) <= (sb_3.ballRadius + sb_4.ballRadius) && (sb_3.stringLength > 0.125 && sb_4.stringLength > 0.125)) { sb_3.radSpeed *= -1; break; } } class StringBall { float maxAccel, ballRadius, xpos, ypos, angle, radSpeed, maxRadSpeed, speedFactor, radAccel, stringLength; int direction; StringBall(float mA, float mRS, float bR, int dir) { angle = 0; radSpeed = 0; maxAccel = mA; maxRadSpeed = mRS; ballRadius = bR; direction = dir; } void update() { radAccel = direction * random(0, maxAccel) * (maxRadSpeed - (radSpeed * direction)); radSpeed = radSpeed + radAccel; speedFactor = radSpeed / maxRadSpeed; angle += radSpeed; stringLength = speedFactor * speedFactor; //if (abs(radSpeed) >= maxRadSpeed) radSpeed *= - 0.9; //println("speedFactor = " + speedFactor + ", stringLength = " + stringLength); xpos = width / 2 * (1 + stringLength * cos(angle)); ypos = height / 2 * (1 + stringLength * sin(angle)); /*while (count % (rollingRadius * rollingRadius) == rollingRadius * rollingRadius - 1) { radspeed *= random(-2, 2); break; }*/ /*fill(255, 32); ellipse(width / 2 + (staticRadius + rollingRadius) * cos(angle), height / 2 + (staticRadius + rollingRadius) * sin(angle), rollingRadius * 2, rollingRadius * 2); fill(255, 16); ellipse(width / 2, height / 2, staticRadius * 2, staticRadius * 2);*/ fill(0); ellipse(xpos, ypos, ballRadius * 2, ballRadius * 2); //stroke(255); //line(width / 2, height / 2, xpos, ypos); } }