Does this line control the position of balls generated? I have basically change the value of it, but I still seeing balls coming from outside(and also from one direction...)
loc = new PVector(random(10,20), random(10,20));
Also, I have added this function on the code(which is your code actually), but it is a little bit wried when the program start-_-. I guess is the starting position wrong, so have this result...?Can you tell how can I improve it? (I think balls can generate at the center, or 0,0 is fine, but now is just like error in the program-0-)
- ArrayList <Ball> balls = new ArrayList <Ball> ();
- ArrayList <PVector> targets = new ArrayList <PVector> ();
-
- int numBalls = 750;
- float friction = -0.9;
- float spring = 0.025;
- float damp = 0.99;
-
- void setup() {
- size(1280, 720);
- smooth();
- reset();
- }
-
- void draw() {
- background(255);
- for (Ball b : balls) {
- b.update();
- b.display();
- }
- noFill();
- stroke(0);
- for (PVector p : targets) {
- ellipse(p.x, p.y, 100, 100);
- }
- }
-
- void keyPressed() {
- if (key == ' ') { reset(); }
- }
-
- void reset() {
- targets.clear();
- balls.clear();
- PVector target = new PVector();
- for (int i=0; i<numBalls; i++) {
- if (iP==0) { target = randomTarget(); targets.add(target); }
- balls.add(new Ball(target));
- }
- }
-
- PVector randomTarget() {
- PVector t = new PVector(random(60, width-60), random(60, height-60));
- while (withinDistance(t, 150)) {
- t = new PVector(random(60, width-60), random(60, height-60));
- }
- return t;
- }
-
- boolean withinDistance(PVector v, float distance) {
- for (PVector p : targets) {
- if (p.dist(v) < distance) {
- return true;
- }
- }
- return false;
- }
-
- class Ball {
- float diameter;
- PVector loc, vel, target;
- float centerSpeed;
- int id;
-
- Ball(PVector t) {
- id = balls.size();
- diameter = random(5, 25);
- loc = new PVector(random(10, 10), random(10, 10));
- while (loc.mag() < 1500) { loc.mult(10); }
- vel = new PVector();
- centerSpeed = random(0.005, 0.01);
- target = t.get();
- }
-
- void update() {
- ballCollision();
- move();
- centralize();
- vel.mult(damp);
- loc.add(vel);
- }
-
- void display() {
- color c = color((loc.x+id) % 255, (loc.y+id) % 255, (125+id)%5);
- noStroke();
- fill(c);
- ellipse(loc.x, loc.y, diameter, diameter);
- }
-
- void centralize() {
- PVector dif = PVector.sub(target, loc);
- dif.mult(centerSpeed);
- loc.add(dif);
- }
-
- void ballCollision() {
- for (int i=id+1; i<balls.size(); i++) {
- Ball other = balls.get(i);
- float distance = PVector.dist(other.loc, loc);
- float minDist = other.diameter/2 + diameter/2;
- if (distance < minDist) {
- PVector dif = PVector.sub(other.loc, loc);
- float angle = atan2(dif.y, dif.x);
- float targetX = loc.x + cos(angle) * minDist;
- float targetY = loc.y + sin(angle) * minDist;
- float ax = (targetX - other.loc.x) * spring;
- float ay = (targetY - other.loc.y) * spring;
- vel.x -= ax;
- vel.y -= ay;
- other.vel.x += ax;
- other.vel.y += ay;
- }
- }
- }
- void move() {
- loc.x += vel.x;
- loc.y += vel.y;
- if (loc.x + diameter/2 > width) {
- loc.x = width - diameter/2;
- vel.x += -0.1;
- }
- else if (loc.x - diameter/2 < 0) {
- loc.x = diameter/2;
- vel.x *= -0.1;
- }
- if (loc.y + diameter/2 > height) {
- loc.y = height - diameter/2;
- vel.y *= -0.1;
- }
- else if (loc.y - diameter/2 < 0) {
- loc.y = diameter/2;
- vel.y *= -0.1;
- }
- }
-
-
-
- }