gravity

by pif on March 9th, 2010
No notes
Syntax: Java
Show lines - Hide lines - Show in textbox - Download
  1. int amount=50;
  2. PVector[] b = new PVector[amount];
  3. float x=100;
  4. float y=0;
  5.  
  6. float gy = .05;
  7. float gx = .00;
  8.  
  9. float sx=1.0;
  10. float sy=0.0;
  11.  
  12. float k=-0.7;
  13.  
  14. void mouseClicked()
  15. {
  16. y=20;
  17. }
  18.  
  19. void keyPressed()
  20. {
  21. if(key==' ')
  22. {
  23. x=100;
  24. y=0;
  25.  
  26. gy = 0.05;
  27. gx = 0.05;
  28.  
  29. sx=0.0;
  30. sy=0.0;
  31.  
  32. k=-0.7;
  33. }
  34. if(keyCode==UP)
  35. {
  36. gx+=gx;
  37. gy+=gy;
  38. }
  39. if(keyCode==DOWN)
  40. {
  41. gx-=gx;
  42. gy-=gy;
  43. }
  44. }
  45.  
  46. void setup()
  47. {
  48. size(400,300);
  49. smooth();
  50. ellipseMode(CENTER);
  51. for(int i=0;i<amount;++i)
  52. b[i] = new PVector(0,0);
  53. }
  54.  
  55. void draw()
  56. {
  57. background(255);
  58. fill(0);
  59. noStroke();
  60.  
  61. //gx=random(-1,1)/2;
  62. x+=sx;
  63. sx+=gx;
  64.  
  65. y+=sy;
  66. sy+=gy;
  67.  
  68. if(y>height-20 || y<0)
  69. {
  70. if(dist(x,y,mouseX,height-10)<20)
  71. { y=constrain(y,0,height);
  72. sy*=k;
  73. }
  74. else println("AAAAAAAAAAAAAAAA!");
  75. }
  76. if(x>width || x<0)
  77. {
  78. x=constrain(x,0,width);
  79. sx*=k;
  80. }
  81.  
  82. for(int i=1;i<amount;++i)
  83. b[i-1]=b[i];
  84. b[amount-1] = new PVector(x,y);
  85.  
  86.  
  87. for(int i=0;i<amount;++i)
  88. {
  89. fill(255-5*i);
  90. ellipse(b[i].x,b[i].y,20,20);
  91. }
  92.  
  93. rectMode(CENTER);
  94. rect(mouseX,height-10,20,20);
  95.  
  96. }

Leave a Reply

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

Subscribe to this comment feed via RSS