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 & HelpSyntax Questions › More from "Learning processing"
Page Index Toggle Pages: 1
More from "Learning processing" (Read 782 times)
More from "Learning processing"
Aug 11th, 2009, 9:41am
 
Here is a sketch i am working on. Im wondering if someone could give me a clue as to how to get ball thats caught to act more naturally.  I guess it would need to be dragged a bit or maybe bounce off the edge of the other ball to prevent dragging.  

EDIT: Im going to try to add in these functions using the circle collision with swapping velocities example.


Quote:
Ball ball1;
Ballcaught ballcaught1;

void setup() {
  size(1000,700);
  smooth();

  ball1 = new Ball(150);
  ballcaught1 = new Ballcaught(25);
}

void draw() {
  background(50);
  ball1.move();
  ballcaught1.moveBallcaught();
  if (ballcaught1.hitEdge(ball1)) {
    ballcaught1.turnBack();
  }
  ball1.display();
  ballcaught1.displayBallcaught();
}



class Ball {
  float r;
  float x,y;
  float xspeed,yspeed;
  color c = color(200,200);
  Ball(float tempR) {
    r = tempR;
    x = random(width);
    y = random(height);
    xspeed = random(-3,5);
    yspeed = random(-3,5);
  }

  void move() {
    x += xspeed;
    y += yspeed;
    if (x > width || x < 0) {
      xspeed *= -1;
    }
    if (y > height || y < 0) {
      yspeed *= -1;
    }
  }

  void highlight() {
    c = color(200,0,100,99);
  }

  void display() {
    stroke(0);
    strokeWeight(15);
    fill(c);
    ellipse(x,y,r*2,r*2);
    c = color(0,150,0);
  }
}



class Ballcaught {
  float r;
  float x,y;
  float xspeed,yspeed;
  color c = color(200,200);
  Ballcaught(float tempR) {
    r = tempR;
    x = ball1.x;
    y = ball1.y;
    xspeed = random(-3,4);
    yspeed = random(-3,4);
  }

  void moveBallcaught() {
    x += xspeed;
    y += yspeed;
  }

  boolean hitEdge(Ball b ) {
    float distance = dist(x,y,b.x,b.y);
    if (distance > b.r) {
      return true;
    } 
    else {
      return false;
    }
  }

  void turnBack() {
    x = ball1.x;
    y = ball1.y;
    xspeed *= -1;
    yspeed *= -1;
  }

  void displayBallcaught() {
    stroke(0);
    strokeWeight(7);
    fill(c);
    ellipse(x,y,r*2,r*2);
    c = color(170,200,0);
  }
}






Re: More from "Learning processing"
Reply #1 - Aug 13th, 2009, 2:58am
 
i changed ur code a little and managed to come up with this.. its not perfect but maybe someone can take a look at it and change it too..  Cheesy

Code:

float x1,x2,y1,y2;      //big circle=(x1,y2) small circle=(x2,y2)
float speed1x,speed1y; //speed/direction
float speed2x,speed2y;
float r1=100; float r2=20;  //radius
color c1=#14FC00;     //green
color c2=#EBFC00;     //yellow


void setup()
{
 size(500,300); smooth();
 //set intial positions
 x1=width/2; y1=height/2;
 x2=width/2; y2=height/2;
 circle(x1,y1,r1,c1);circle(x2,y2,r2,c2);
 speed1x=random(1,2);speed1y=random(1,2);
 speed2x=random(1,3);speed2y=random(1,3);
 
 
}

void draw()
{
 background(0);
 drawbigcircle();
 drawsmallcircle(check());
 
}

color check()
{
float p=atan2( (y2-y1),(x2-x1));

float pointx1=x1+(r1-r2)*cos(p);
float pointy1=y1+(r1-r2)*sin(p);

float d1= sqrt( sq(pointx1-x1)+sq(pointy1-y1));
float d2= sqrt( sq(x2-x1)+sq(y2-y1));

if(d2>=d1)
{
  x2=pointx1;y2=pointy1;
 
  speed2x*=-1;
  speed2y*=-1;
  return c2=#FF0000;
}
 
 x2+=speed2x;
 y2+=speed2y;
 return  c2=#EBFC00;

}


//////////////////////////////////////////////////////////////
void drawbigcircle()
{
 if((x1+r1)>width || (x1-r1)<=0) speed1x*=-1;
 if((y1+r1)>height || (y1-r1)<=0) speed1y*=-1;

 x1+=speed1x; y1+=speed1y;
 
 circle(x1,y1,r1,c1);
 
 
}

///////////////////////////////////////////////////////////////
void drawsmallcircle(color _c2)
{
 if(x2+r2>width || x2-r2<=0) speed2x*=-1;
 if(y2+r2>height || y2-r2<=0) speed2y*=-1;
 
 x2+=speed2x; y2+=speed2y;
 
 circle(x2,y2,r2,_c2);
 
}

//////////////////////////////////////////////////////////////
void circle(float _x, float _y, float _r, color _c)
{
 stroke(0);strokeWeight(2);fill(_c);
 ellipse(_x,_y,_r*2,_r*2);
 
}
Page Index Toggle Pages: 1