gravity

by pif on March 9th, 2010
No notes
Syntax: Java
Show lines - Hide lines - Show in textbox - Download
int amount=50;
PVector[] b = new PVector[amount];
float x=100;
float y=0;
 
float gy = .05;
float gx = .00;
 
float sx=1.0;
float sy=0.0;
 
float k=-0.7;
 
void mouseClicked()
{
  y=20;
}
 
void keyPressed()
{
  if(key==' ')
  {
     x=100;
     y=0;
 
     gy = 0.05;
     gx = 0.05;
 
     sx=0.0;
     sy=0.0;
 
     k=-0.7;
  }
  if(keyCode==UP)
  {
    gx+=gx;
    gy+=gy;
  }
  if(keyCode==DOWN)
  {
    gx-=gx;
    gy-=gy;
  }  
}
 
void setup()
{
  size(400,300);
  smooth();
  ellipseMode(CENTER);
  for(int i=0;i<amount;++i)
    b[i] = new PVector(0,0);
}
 
void draw()
{
  background(255);
  fill(0);
  noStroke();
 
  //gx=random(-1,1)/2;
  x+=sx;
  sx+=gx;
 
  y+=sy;
  sy+=gy;
 
  if(y>height-20 || y<0)
  {
    if(dist(x,y,mouseX,height-10)<20)
    {    y=constrain(y,0,height);
        sy*=k;
    }
    else println("AAAAAAAAAAAAAAAA!");
  }
  if(x>width || x<0)
  {
    x=constrain(x,0,width);
    sx*=k;
  }
 
  for(int i=1;i<amount;++i)
    b[i-1]=b[i];
  b[amount-1] = new PVector(x,y);
 
 
  for(int i=0;i<amount;++i)
  {
    fill(255-5*i);
    ellipse(b[i].x,b[i].y,20,20);
  }
 
  rectMode(CENTER);
  rect(mouseX,height-10,20,20);
 
}

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS