collision problem with floating bubbles
in
Programming Questions
•
1 year ago
hi again,
i know maybe i ask too much but i should really know where my problem is.. my firends and teachers in my university have no idea what processing is.. maybe they heard about it, but they cant be helpful.
so; my code was only one bubble, now there are many. and i wrote a void collision() but it seems it doesnt effective.
i'm sure that my void collision() is true, and i dont see any fail messages. i saw a collision example, and i just want my bubbles have the same collision effect. am i missing something?
here is my code:
- int numBlob = 15;
- float spring = 0.005;
- float gravity = 0.0003;
- float friction = -0.9;
- Blob[] blobs = new Blob[numBlob];
- void setup()
- {
- size(1000, 600);
- stroke(0);
- smooth();
- //noStroke();
- frameRate(320);
- for(int i = 0; i < numBlob; i++)
- {
- blobs[i] = new Blob(25,random(10,70),50,50,random(10, 90),i,blobs);
- }
- /*blobs[0] = new Blob(41,52,6,22,random(20, 40),blobs);
- blobs[1] = new Blob(41,52,6,22,blobs);
- blobs[2] = new Blob(30, 35,2,33,blobs);
- blobs[3] = new Blob(12, 30,25,30,blobs);
- blobs[4] = new Blob(66, 10,6,24,blobs);
- blobs[5] = new Blob(10, 10,6,22,blobs);
- blobs[6] = new Blob(20, 20,12,35,blobs);
- */
- }
- void draw()
- {
- fill(118, 0, 0,50);
- //background(#C95F45);
- rect(0, 0, width, height);
- strokeWeight(0.001);
- for (int i = 0; i < numBlob; i++)
- {
- blobs[i].collide();
- blobs[i].move();
- blobs[i].ciz();
- }
- }
- class Blob
- {
- float vx = 0;
- float vy = 0;
- int id;
- float diameter;
- float ci_x = random(1,30);
- float ci_y = random(1,30);
- /*float ci_x;
- float ci_y;*/
- float move_x = noise(50);
- float move_y = noise(50);
- float currentx;
- float currenty;
- float num = random(50);
- int Segments;
- float buyuk;
- Blob[] others;
- Blob(int seg, float b, float cx, float cy, float din, int idin, Blob[] oin)
- {
- currentx = width / 2;
- currenty = height / 2;
- ci_x = cx;
- ci_y = cy;
- Segments = seg;
- buyuk = b;
- diameter = din;
- id = idin;
- others = oin;
- }
- void move()
- {
- /*vy += gravity;
- ci_x += vx;
- ci_y += vy;*/
- if (ci_x + diameter/2 > width) {
- ci_x = width - diameter/2;
- move_x = -move_x;
- move_x *= friction;
- }
- if (ci_y + diameter/2 > height) {
- ci_y = height - diameter/2;
- move_y = -move_y;
- move_y *= friction;
- }
- if (ci_x - diameter/2 < 0) {
- //ci_x = 0;
- ci_x = diameter/2;
- move_x = -move_x;
- move_x *= friction;
- }
- if (ci_y - diameter/2 < 0) {
- //ci_y = 0;
- ci_y = diameter/2;
- move_y = -move_y;
- move_y *= friction;
- }
- }
- void ciz() {
- translate(width * noise(num+30), height * noise(num+70));
- rotate(20 * noise(num+10));
- num = num + 0.0009;
- float x[] = new float[Segments];
- float y[] = new float[Segments];
- for (int i=0; i<Segments; i++) {
- //float reb = random(5);
- float angle = float(i) / float(Segments) * TWO_PI;
- float distance = buyuk + 5 * noise(i, frameCount/80.0);
- x[i] = ci_x + sin(angle) * distance+30;
- y[i] = ci_y + cos(angle) * distance+30;
- }
- beginShape();
- fill(200);
- for (int i=0; i<Segments+4; i++) {
- curveVertex(x[i % Segments], y[i % Segments]);
- }
- endShape();
- resetMatrix();
- }
- void collide()
- {
- for (int i = id + 1; i < numBlob; i++) {
- float dx = others[i].ci_x - ci_x;
- float dy = others[i].ci_y - ci_y;
- float distance = sqrt(dx*dx + dy*dy);
- float minDist = others[i].diameter/2 + diameter/2;
- if (distance < minDist) {
- float angle2 = atan2(dy, dx);
- float targetX = ci_x + cos(angle2) * minDist;
- float targetY = ci_y + sin(angle2) * minDist;
- float ax = (targetX - others[i].ci_x) * spring;
- float ay = (targetY - others[i].ci_y) * spring;
- vx -= ax;
- vy -= ay;
- others[i].vx += ax;
- others[i].vy += ay;
- }
- }
- }
- }
1