Precalculate a dynamic scene (Bouncy Bubbles)
in
Programming Questions
•
8 months ago
Hi!
I want to precalculate a dynamic scene like the
Bouncy Bubbles Example.
For that, i made a 2D-Array-PVector-return-method, which gives you all the positions of the single particles back.
The values that are going into the functions are the start positions and the start directions of the particles, and the value of how many steps should be calculated.
Now the problem, when you run the calculation (hit Arrow Key Up or Down), you can notice that only the balls with a lower array-id value are effecting the higher ones, but not vise versa. And this is where I'm stuck. I hope anyone could help me with this issue. Thx!
here the method:
- PVector [][] fP(PVector [] posIn, PVector [] dir, int steps){
- int num = posIn.length;
- float [][] posOX = new float [num][steps];
- float [][] posOY = new float [num][steps];
- PVector [][] posOut = new PVector[num][steps];
- for(int i=0; i<num;i++){
- posOX[i][0] = posIn[i].x;
- posOY[i][0] = posIn[i].y;
- }
- float [] vx = new float[num];
- float [] vy = new float[num];
- for(int i=0; i<num;i++){
- vx[i]=dir[i].x;
- vy[i]=dir[i].y;
- }
- float minDist;
- float ax2, ay2;
- // START CALCULATION
- for(int i=0; i<num;i++){
- for(int j = 0; j < steps-1;j++){
- posOX[i][j+1] = posOX[i][j];
- posOY[i][j+1] = posOY[i][j];
- vx[i] += g.x;
- vy[i] += g.y;
- // BORDERS
- if(posOX[i][j+1]+vx[i] > width-(diameter/2)-1){
- posOX[i][j+1]= width-(diameter/2);
- vy[i] *= friction; vx[i] *= -bounce;
- } else if(posOX[i][j+1]-vx[i] < (diameter/2)){
- posOX[i][j+1]= (diameter/2)+1;
- vy[i] *= friction; vx[i] *= -bounce;
- }
- if(posOY[i][j+1] + vy[i] > height-(diameter/2)-1){
- posOY[i][j+1] = height-(diameter/2);
- vx[i] *= friction; vy[i] *= -bounce;
- } else if(posOY[i][j+1] - vy[i] < (diameter/2)){
- posOY[i][j+1]= (diameter/2)+1;
- vx[i] *= friction; vy[i] *= -bounce;
- }
- // Borders
- // COLLISION
- for(int i2=0; i2<num;i2++){
- for(int i0=0; i0<num;i0++){
- float dx2 = posOX[i0][j+1] - posOX[i2][j+1];
- float dy2 = posOY[i0][j+1] - posOY[i2][j+1];
- float distance2=dist(posOX[i0][j+1],posOY[i0][j+1], posOX[i2][j+1],posOY[i2][j+1]);
- //println( nf( distance2 ,3,2) + " : ");
- minDist = diameter;
- if (distance2 < minDist) {
- float angle2 = atan2(dy2, dx2);
- float targetX2 = posOX[i0][j+1] + cos(angle2) * (minDist);
- float targetY2 = posOY[i0][j+1] + sin(angle2) * (minDist);
- ax2 = (targetX2 - posOX[i2][j+1]) * spring;
- ay2 = (targetY2 - posOY[i2][j+1]) * spring;
- vx[i0] += (ax2*calm);
- vy[i0] += (ay2*calm);
- vx[i2] -= (ax2*calm);
- vy[i2] -= (ay2*calm);
- }
- }
- }
- // Collision
- // update pos
- posOX[i][j+1] += vx[i];
- posOY[i][j+1] += vy[i];
- }
- }
- for(int i=0; i<num;i++){
- for(int j = 0; j < steps;j++){
- posOut[i][j] = new PVector(posOX[i][j], posOY[i][j]);
- }
- }
- return posOut;
- }
and here the the rest:
- int num = 6; // num of particles
- int steps = 400; // steps to calculate
- float bounce = 0.2;
- float calm = 0.1;
- float spring = 1.0;
- float friction = 0.85; // .95
- PVector [] startP = new PVector[num];
- PVector [] dir = new PVector[num];
- float diameter = 30;
- PVector g = new PVector(0, 1);
- PVector [][] thePos;
- int showSteps = 1;
- int nextSt = 1;
- void setup(){
- size(500,400); background(255); noStroke();
- for(int i = 0; i < num; i++){
- startP[i] = new PVector((int)random(diameter+1,width-(diameter+1)), (int)random(diameter+1, height/2));
- dir[i] = new PVector((int)random(-4,4), (int)random(-4,4));
- }
- thePos = fP(startP, dir, steps);
- }
- void draw(){
- background(255);
- noStroke();
- fill(0,255,0,150);
- for(int i=0; i<num;i++){
- for(int j = 0; j < steps;j++){
- ellipse(thePos[i][j].x, thePos[i][j].y, 2,2);
- }
- }
- fill(255,0,0,200);
- for(int i=0; i<num;i++){
- ellipse(thePos[i][showSteps-1].x, thePos[i][showSteps-1].y, diameter, diameter);
- }
- stroke(0,0,255);
- strokeWeight(0.2);
- for(int i=1; i<num;i++){
- line(thePos[num-i][showSteps-1].x, thePos[num-i][showSteps-1].y, thePos[num-i-1][showSteps-1].x, thePos[num-i-1][showSteps-1].y);
- }
- /*
- stroke(0,100);
- for(int i=0; i<num;i++){
- for(int j=0; j<num;j++){
- line(thePos[i][showSteps-1].x, thePos[i][showSteps-1].y, thePos[j][showSteps-1].x, thePos[j][showSteps-1].y);
- }
- }
- */
- }
- void keyPressed(){
- if (key == CODED) {
- if (keyCode == UP) {
- if(showSteps+nextSt <= steps){
- showSteps+=nextSt;
- } else {
- showSteps=steps;
- }
- } else if (keyCode == DOWN) {
- if(showSteps-nextSt > 1){
- showSteps-=nextSt;
- } else {
- showSteps=1;
- }
- }
- //println(showSteps);
- }
- }
1