creepyfish.pde 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ArrayList<Fish> fishes = new ArrayList<Fish>();
  2. void setup() {
  3. size(800, 600);
  4. for(int i = 0; i < 20; i++) {
  5. Fish currentFish = new Fish(random(50, 750), random(50, 550));
  6. currentFish.setAngle(random(0, 360));
  7. currentFish.swimForward();
  8. fishes.add(currentFish);
  9. }
  10. }
  11. void draw() {
  12. for(Fish fish : fishes) {
  13. fish.update();
  14. }
  15. background(0, 119, 190);
  16. for(Fish fish : fishes) {
  17. fish.draw();
  18. }
  19. }
  20. class Fish {
  21. private float x;
  22. private float y;
  23. private PVector position;
  24. private PVector velocity;
  25. private FishNode head;
  26. private float angle;
  27. public Fish(float x, float y) {
  28. float startingTheta = random(0, (float)(2 * Math.PI));
  29. FishNode childNode = new FishNode(null, 7, 7, 20, 0, 0.3, startingTheta);
  30. childNode = new FishNode(childNode, 15, 15, 30, 0, 0.4, (float)(startingTheta + Math.PI / 4));
  31. head = new FishNode(childNode, 20, 20, 0, 0, 0, 0);
  32. position = new PVector(x, y);
  33. velocity = new PVector();
  34. this.angle = 0;
  35. }
  36. public void setAngle(float angle) {
  37. this.angle = angle;
  38. }
  39. public void swimForward() {
  40. velocity.x = -cos(radians(angle));
  41. velocity.y = -sin(radians(angle));
  42. }
  43. public void update() {
  44. position.add(velocity);
  45. if(position.x < 0) {
  46. position.x = 0;
  47. angle += random(-90, 90);
  48. swimForward();
  49. }
  50. if(position.x > 800) {
  51. position.x = 800;
  52. angle += random(-90, 90);
  53. swimForward();
  54. }
  55. if(position.y < 0) {
  56. position.y = 0;
  57. angle += random(-90, 90);
  58. swimForward();
  59. }
  60. if(position.y > 600) {
  61. position.y = 600;
  62. angle += random(-90, 90);
  63. swimForward();
  64. }
  65. head.update();
  66. }
  67. public void draw() {
  68. pushMatrix();
  69. translate(position.x, position.y);
  70. pushMatrix();
  71. rotate(radians(angle));
  72. head.draw(null);
  73. popMatrix();
  74. popMatrix();
  75. }
  76. }
  77. class FishNode {
  78. private FishNode child;
  79. public float x;
  80. public float y;
  81. private float width;
  82. private float height;
  83. private float theta;
  84. private float swimOffset;
  85. private float flopDirection;
  86. private float amplitude;
  87. public FishNode(FishNode child, float width, float height, float x, float y, float amplitude, float theta) {
  88. this.child = child;
  89. this.width = width;
  90. this.height = height;
  91. this.x = x;
  92. this.y = y;
  93. this.theta = theta;
  94. swimOffset = 0.0f;
  95. flopDirection = 1;
  96. this.amplitude = amplitude;
  97. }
  98. public void setTheta(float theta) {
  99. this.theta = theta;
  100. }
  101. public float getTheta() {
  102. return theta;
  103. }
  104. public float getSine() {
  105. return amplitude * sin(theta);
  106. }
  107. public float getX() {
  108. return x;
  109. }
  110. public float getY() {
  111. return y;
  112. }
  113. public float getHeight() {
  114. return height;
  115. }
  116. public float getWidth() {
  117. return width;
  118. }
  119. public float getTopY() {
  120. return getHeight() / 2;
  121. }
  122. public float getBottomY() {
  123. return -getHeight() / 2;
  124. }
  125. public void update() {
  126. theta += 0.1;
  127. if(child != null) {
  128. child.update();
  129. }
  130. }
  131. public void draw(FishNode parent) {
  132. fill(0,0,0,0);
  133. strokeWeight(2);
  134. stroke(200,200,200,255);
  135. pushMatrix();
  136. rotate(getSine());
  137. if(parent != null) {
  138. line(0, parent.getTopY(), getX(), getTopY());
  139. line(0, parent.getBottomY(), getX(), getBottomY());
  140. }
  141. translate(getX(), getY());
  142. ellipse(0, 0, width, height);
  143. if(child != null) {
  144. child.draw(this);
  145. }
  146. popMatrix();
  147. }
  148. }