We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › collide within group
Page Index Toggle Pages: 1
collide within group (Read 874 times)
collide within group
Jun 19th, 2009, 3:41am
 
hey everyone!

I want an ellipse to move through moving barriers. I already have my bouncing ball

int size = 60;
float xpos, ypos;    

float xspeed = 2.8;
float yspeed = 2.2;

int xdirection = 1;  
int ydirection = 1;

void setup()
{
 size(640, 200);
 noStroke();
 smooth();
 xpos = width/2;
 ypos = height/2;
}

void draw()
{
 background(0);
 xpos = xpos + ( xspeed * xdirection );
 ypos = ypos + ( yspeed * ydirection );
 if (xpos > width-size || xpos < 0) {
   xdirection *= -1;
 }
 if (ypos > height-size || ypos < 0) {
   ydirection *= -1;
 }
 ellipse(xpos+size/2, ypos+size/2, size, size);
}


and now I want to include it in my class which makes up the barriers

int numBalls = 7;
float spring = 0.9;
float gravity = 0.15;
float friction = -1.0;
Ball[] balls = new Ball[numBalls];
float rad;

void setup()
{
 size  (1024, 106);
 noStroke();
 smooth();
 for (int i = 0; i < numBalls; i++) {
   balls[i] = new Ball(random(width), random(height), random(20, 20), i, balls);
 }
}

void draw()
{
 background(0);
 for (int i = 0; i < numBalls; i++) {
   balls[i].collide();
   balls[i].move();
   balls[i].display();  
 }
}

class Ball {
 float x, y;
 float diameter;
 float vx = 0;
 float vy = 0;
 int id;
 Ball[] others;

 Ball(float xin, float yin, float din, int idin, Ball[] oin) {
   x = xin;
   y = yin;
   diameter = din;
   id = idin;
   others = oin;
 }
 
 void collide() {
   for (int i = id + 1; i < numBalls; i++) {
     float dx = others[i].x - x;
     float dy = others[i].y - y;
     float distance = sqrt(dx*dx + dy*dy);
     float minDist = others[i].diameter/2 + diameter/2;
     if (distance < minDist) {
       float angle = atan2(dy, dx);
       float targetX = x + cos(angle) * minDist;
       float targetY = y + sin(angle) * minDist;
       float ax = (targetX - others[i].x) * spring;
       float ay = (targetY - others[i].y) * spring;
       vx -= ax;
       vy -= ay;
       others[i].vx += ax;
       others[i].vy += ay;
     }
   }  
 }
 
 void move() {
   vy += gravity;
   x += 0;
   y += vy;
   if (x + diameter/2 > width) {
     //x = width - diameter/2;
     vx *= friction;
   }
   else if (x - diameter/2 < 0) {
     //x = diameter/2;
     vx *= friction;
   }
   if (y + diameter/2 > height) {
     y = height - diameter/2;
     vy *= friction;
   }
   else if (y - diameter/2 < 0) {
     y = diameter/2;
     vy *= friction;
   }
 }
 
 void display() {
   fill(255,200);
   ellipse(x, y, sin(rad)*2+width/48,sin(rad)*2+height/5);
   rad = rad+random(0.2,0.5);
 }
}



my plan is that the single ball moves from the left to the right and everytime it collides with the edges or the small balls from the group it bounces away - but I can't manage to include it in my class

any ideas?
Page Index Toggle Pages: 1