Guest User

Untitled

a guest
Nov 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1. ArrayList <Mover> bouncers;
  2.  
  3. Mover predator;
  4. Mover leader;
  5. int predatorvictim = 0;
  6.  
  7. boolean followLeader = true;
  8. boolean hidefromPredator = true;
  9.  
  10. int bewegungsModus = 5;
  11. // 0 = BOUNCE
  12. // 1 = NOISE
  13. // 2 = STEER
  14. // 3 = SEEK
  15. // 4 = RADIAL
  16. // 5 = FLOCK
  17.  
  18. void setup ()
  19. {
  20. size (1920, 1080);
  21.  
  22. bouncers = new ArrayList ();
  23. predator = new Mover ();
  24. predator.setColor (#52A59D, #ffffff);
  25. predator.setFishSize (30);
  26. predator.speed = 6;
  27. predator.SPEED = 6;
  28.  
  29. leader = new Mover();
  30. leader.setColor (#ffffff, #666666);
  31. leader.setFishSize (30);
  32. leader.speed = 6;
  33. leader.SPEED = 6;
  34.  
  35. for (int i = 0; i < 1000; i++)
  36. {
  37. Mover newMover = new Mover();
  38. bouncers.add (newMover);
  39. }
  40.  
  41. frameRate (30);
  42. }
  43.  
  44. void draw ()
  45. {
  46. background (#9ef7dd);
  47.  
  48. // SCHWARM -----------
  49.  
  50. for (int i = 0; i < bouncers.size (); i++)
  51. {
  52. Mover m = bouncers.get (i);
  53.  
  54. if (bewegungsModus != 5) m.update (bewegungsModus);
  55. else
  56. {
  57. m.flock (bouncers);
  58. m.move();
  59. m.checkEdges();
  60. m.display();
  61.  
  62. // SWARM PREDATOR REACTION -----------------------
  63.  
  64. if (hidefromPredator)
  65. {
  66. float distance = dist (predator.getLocation().x, predator.getLocation().y, m.getLocation().x, m.getLocation().y);
  67.  
  68. if (distance < predator.getSize() * 3)
  69. {
  70. float angle = atan2 (m.getLocation().y-predator.getLocation().y, m.getLocation().x-predator.getLocation().x);
  71.  
  72. m.seperation (new PVector (cos (angle), sin (angle)), 5);
  73. }
  74. }
  75.  
  76. // SWARM LEADER REACTION ---------------------------
  77.  
  78. if (followLeader)
  79. {
  80. float distance = dist (leader.getLocation().x, leader.getLocation().y, m.getLocation().x, m.getLocation().y);
  81.  
  82. if (distance < 70)
  83. {
  84. m.steer (leader.getLocation().x, leader.getLocation().y, 1);
  85. }
  86. }
  87. }
  88. }
  89.  
  90. // PREDATOR MOVEMENT ---------------------
  91.  
  92. if (hidefromPredator)
  93. {
  94. predator.move ();
  95. PVector target = bouncers.get(predatorvictim).getLocation();
  96. predator.steer (target.x, target.y);
  97. predator.checkEdges ();
  98. predator.display();
  99.  
  100. if (PVector.dist (bouncers.get(predatorvictim).getLocation(), predator.getLocation()) < predator.getSize() * 5)
  101. {
  102. predatorvictim = (int) random (bouncers.size());
  103. }
  104. }
  105.  
  106. // LEADER MOVEMENT ---------------------
  107. if (followLeader)
  108. {
  109. leader.move ();
  110. leader.flock (bouncers);
  111. leader.checkEdges();
  112. leader.display();
  113. }
  114. }
  115.  
  116. class Fish
  117. {
  118. PVector [] location;
  119. float ellipseSize;
  120.  
  121. color c1;
  122. color c2;
  123.  
  124. Fish (float x, float y)
  125. {
  126. setRandomColor ();
  127.  
  128. location = new PVector [round (random (8, 15))];
  129. location[0] = new PVector (x, y);
  130.  
  131. for (int i = 1; i < location.length; i++)
  132. {
  133. location[i] = location[0].get ();
  134. }
  135. ellipseSize = random (30, 150);
  136. }
  137.  
  138. // GET ------------------------------
  139.  
  140. float getSize ()
  141. {
  142. return ellipseSize;
  143. }
  144.  
  145. PVector getHead ()
  146. {
  147. return location [location.length-1].get();
  148. }
  149.  
  150. PVector getTail ()
  151. {
  152. return location [0].get();
  153. }
  154.  
  155. // SET ---------------------
  156.  
  157. void setColor (color c)
  158. {
  159. c1 = c2 = c;
  160. }
  161.  
  162. void setColor (color C1, color C2)
  163. {
  164. c1 = C1;
  165. c2 = C2;
  166. }
  167.  
  168. void setRandomColor ()
  169. {
  170. int colorDice = (int) random (4);
  171.  
  172. if (colorDice == 0) c1 = #ffedbc;
  173. else if (colorDice == 1) c1 = #A75265;
  174. else if (colorDice == 2) c1 = #ec7263;
  175. else c1 = #febe7e;
  176.  
  177. float dice = random (100);
  178. if (dice < 25)
  179. {
  180. colorDice = (int) random (4);
  181.  
  182. if (colorDice == 0) c2 = #ffedbc;
  183. else if (colorDice == 1) c2 = #A75265;
  184. else if (colorDice == 2) c2 = #ec7263;
  185. else c2 = #febe7e;
  186. }
  187. else c2 = c1;
  188. }
  189.  
  190. void setHead (PVector pos)
  191. {
  192. location [location.length-1]= pos.get();
  193.  
  194. updateBody ();
  195. }
  196.  
  197. // HELPERS ---------------
  198.  
  199. void updateBody ()
  200. {
  201. for (int i = 0; i < location.length-1; i++)
  202. {
  203. location [i] = location [i+1];
  204. }
  205. }
  206.  
  207. void resetBody ()
  208. {
  209. for (int i = 0; i < location.length-1; i++)
  210. {
  211. location [i] = location [location.length-1].get();
  212. }
  213. }
  214.  
  215. // DISPLAY --------------------
  216.  
  217. void display ()
  218. {
  219. noStroke();
  220. for (int i = 0; i < location.length; i++)
  221. {
  222. color c = lerpColor (c1, c2, map (i, 0, location.length, 1, 0 ) );
  223. float s = map (i, 0, location.length, 1, ellipseSize );
  224.  
  225. fill (c);
  226. ellipse (location[i].x, location [i].y, s, s);
  227. }
  228. }
  229. }
  230.  
  231. class Mover
  232. {
  233. PVector direction;
  234.  
  235. float speed;
  236. float SPEED;
  237.  
  238. float noiseScale;
  239. float noiseStrength;
  240. float forceStrength;
  241.  
  242. Fish f;
  243.  
  244. Mover () // Konstruktor = setup der Mover Klasse
  245. {
  246. setRandomValues();
  247. }
  248.  
  249. Mover (float x, float y) // Konstruktor = setup der Mover Klasse
  250. {
  251. setRandomValues ();
  252. f.setHead (new PVector (x, y));
  253. f.resetBody();
  254. }
  255.  
  256. // SET ---------------------------
  257.  
  258. void setRandomValues ()
  259. {
  260. f = new Fish (random (width), random (height));
  261. f.ellipseSize = random (4, 15);
  262.  
  263. float angle = random (TWO_PI);
  264. direction = new PVector (cos (angle), sin (angle));
  265.  
  266. speed = random (4, 7);
  267. SPEED = speed;
  268. noiseScale = 80;
  269. noiseStrength = 1;
  270. forceStrength = random (0.1, 0.2);
  271. }
  272.  
  273. void setColor (color c)
  274. {
  275. f.setColor (c);
  276. }
  277.  
  278. void setColor (color c1, color c2)
  279. {
  280. f.setColor (c1, c2);
  281. }
  282.  
  283. void setFishSize (float s)
  284. {
  285. f.ellipseSize = s;
  286. }
  287.  
  288. // GET --------------------------------
  289.  
  290. float getSize ()
  291. {
  292. return f.getSize();
  293. }
  294.  
  295. PVector getLocation ()
  296. {
  297. return f.getHead();
  298. }
  299.  
  300. // GENEREL ------------------------------
  301.  
  302. void update ()
  303. {
  304. update (0);
  305. }
  306.  
  307. void update (int mode)
  308. {
  309. if (mode == 0) // bouncing ball
  310. {
  311. speed = SPEED * 0.7;
  312. move();
  313. checkEdgesAndBounce();
  314. }
  315. else if (mode == 1) // noise
  316. {
  317. speed = SPEED * 0.7;
  318. addNoise ();
  319. move();
  320. checkEdgesAndRelocate ();
  321. }
  322. else if (mode == 2) // steer
  323. {
  324. steer (mouseX, mouseY);
  325. move();
  326. }
  327. else if (mode == 3) // seek
  328. {
  329. speed = SPEED * 0.7;
  330. seek (mouseX, mouseY);
  331. move();
  332. }
  333. else // radial
  334. {
  335. speed = SPEED * 0.7;
  336. addRadial ();
  337. move();
  338. checkEdges();
  339. }
  340.  
  341. display();
  342. }
  343.  
  344. // FLOCK ------------------------------
  345.  
  346. void flock (ArrayList <Mover> boids)
  347. {
  348. PVector location = f.getHead();
  349.  
  350. PVector other;
  351. float otherSize ;
  352.  
  353. PVector cohesionSum = new PVector (0, 0);
  354. float cohesionCount = 0;
  355.  
  356. PVector seperationSum = new PVector (0, 0);
  357. float seperationCount = 0;
  358.  
  359. PVector alignSum = new PVector (0, 0);
  360. float speedSum = 0;
  361. float alignCount = 0;
  362.  
  363. for (int i = 0; i < boids.size(); i++)
  364. {
  365. other = boids.get(i).f.getHead();
  366. otherSize = boids.get(i).f.getSize();
  367.  
  368. float distance = PVector.dist (other, location);
  369.  
  370.  
  371. if (distance > 0 && distance <70) //align + cohesion
  372. {
  373. cohesionSum.add (other);
  374. cohesionCount++;
  375.  
  376. alignSum.add (boids.get(i).direction);
  377. speedSum += boids.get(i).speed;
  378. alignCount++;
  379. }
  380.  
  381. if (distance > 0 && distance < (f.getSize()+otherSize)*1.2) // seperate bei collision
  382. {
  383. float angle = atan2 (location.y-other.y, location.x-other.x);
  384.  
  385. seperationSum.add (cos (angle), sin (angle), 0);
  386. seperationCount++;
  387. }
  388.  
  389. if (alignCount > 10 || seperationCount > 10) break;
  390. }
  391.  
  392. // cohesion: bewege dich in die Mitte deiner Nachbarn
  393. // seperation: renne nicht in andere hinein
  394. // align: bewege dich in die Richtung deiner Nachbarn
  395.  
  396. if (cohesionCount > 0)
  397. {
  398. cohesionSum.div (cohesionCount);
  399. cohesion (cohesionSum, 1);
  400. }
  401.  
  402. if (alignCount > 0)
  403. {
  404. speedSum /= alignCount;
  405. alignSum.div (alignCount);
  406. align (alignSum, speedSum, 1.3);
  407. }
  408.  
  409. if (seperationCount > 0)
  410. {
  411. seperationSum.div (seperationCount);
  412. seperation (seperationSum, 2);
  413. }
  414. }
  415.  
  416. void cohesion (PVector force, float strength)
  417. {
  418. steer (force.x, force.y, strength);
  419. }
  420.  
  421. void seperation (PVector force, float strength)
  422. {
  423. force.limit (strength*forceStrength);
  424.  
  425. direction.add (force);
  426. direction.normalize();
  427.  
  428. speed *= 1.1;
  429. speed = constrain (speed, 0, SPEED * 1.5);
  430. }
  431.  
  432. void align (PVector force, float forceSpeed, float strength)
  433. {
  434. speed = lerp (speed, forceSpeed, strength*forceStrength);
  435.  
  436. force.normalize();
  437. force.mult (strength*forceStrength);
  438.  
  439. direction.add (force);
  440. direction.normalize();
  441. }
  442.  
  443. // HOW TO MOVE ----------------------------
  444.  
  445. void steer (float x, float y)
  446. {
  447. steer (x, y, 1);
  448. }
  449.  
  450. void steer (float x, float y, float strength)
  451. {
  452. PVector location = f.getHead();
  453.  
  454. float angle = atan2 (y-location.y, x -location.x);
  455.  
  456. PVector force = new PVector (cos (angle), sin (angle));
  457. force.mult (forceStrength * strength);
  458.  
  459. direction.add (force);
  460. direction.normalize();
  461.  
  462. float currentDistance = dist (x, y, location.x, location.y);
  463.  
  464. if (currentDistance < 70)
  465. {
  466. speed = map (currentDistance, 0, 70, 0, SPEED);
  467. }
  468. else speed = SPEED;
  469. }
  470.  
  471. void seek (float x, float y)
  472. {
  473. seek (x, y, 1);
  474. }
  475.  
  476. void seek (float x, float y, float strength)
  477. {
  478. PVector location = f.getHead();
  479.  
  480. float angle = atan2 (y-location.y, x -location.x);
  481.  
  482. PVector force = new PVector (cos (angle), sin (angle));
  483. force.mult (forceStrength * strength);
  484.  
  485. direction.add (force);
  486. direction.normalize();
  487. }
  488.  
  489. void addRadial ()
  490. {
  491. PVector location = f.getHead();
  492.  
  493. float m = noise (frameCount / (2*noiseScale));
  494. m = map (m,0, 1, - 1.2, 1.2);
  495.  
  496. float maxDistance = m * dist (0, 0, width/2, height/2);
  497. float distance = dist (location.x, location.y, width/2, height/2);
  498.  
  499. float angle = map (distance, 0, maxDistance, 0, TWO_PI);
  500.  
  501. PVector force = new PVector (cos (angle), sin (angle));
  502. force.mult (forceStrength);
  503.  
  504. direction.add (force);
  505. direction.normalize();
  506. }
  507.  
  508. void addNoise ()
  509. {
  510. PVector location = f.getHead();
  511.  
  512. float noiseValue = noise (location.x /noiseScale, location.y / noiseScale, frameCount / noiseScale);
  513. noiseValue*= TWO_PI * noiseStrength;
  514.  
  515. PVector force = new PVector (cos (noiseValue), sin (noiseValue));
  516. //Processing 2.0:
  517. //PVector force = PVector.fromAngle (noiseValue);
  518. force.mult (forceStrength);
  519. direction.add (force);
  520. direction.normalize();
  521. }
  522.  
  523. // MOVE -----------------------------------------
  524.  
  525. void move ()
  526. {
  527. PVector location = f.getHead();
  528.  
  529. PVector velocity = direction.get();
  530. velocity.mult (speed);
  531. location.add (velocity);
  532.  
  533. f.setHead (location);
  534. }
  535.  
  536. // CHECK --------------------------------------------------------
  537.  
  538. void checkEdgesAndRelocate ()
  539. {
  540. PVector location = f.getTail();
  541. float diameter = f.getSize();
  542.  
  543. if (location.x < -diameter/2)
  544. {
  545. location.x = random (-diameter/2, width+diameter/2);
  546. location.y = random (-diameter/2, height+diameter/2);
  547.  
  548. f.setHead (location);
  549. f.resetBody ();
  550. }
  551. else if (location.x > width+diameter/2)
  552. {
  553. location.x = random (-diameter/2, width+diameter/2);
  554. location.y = random (-diameter/2, height+diameter/2);
  555.  
  556. f.setHead (location);
  557. f.resetBody ();
  558. }
  559.  
  560. if (location.y < -diameter/2)
  561. {
  562. location.x = random (-diameter/2, width+diameter/2);
  563. location.y = random (-diameter/2, height+diameter/2);
  564. f.setHead (location);
  565. f.resetBody ();
  566. }
  567. else if (location.y > height + diameter/2)
  568. {
  569. location.x = random (-diameter/2, width+diameter/2);
  570. location.y = random (-diameter/2, height+diameter/2);
  571. f.setHead (location);
  572. f.resetBody ();
  573. }
  574. }
  575.  
  576.  
  577. void checkEdges ()
  578. {
  579. PVector location = f.getTail();
  580. float diameter = f.getSize();
  581.  
  582. if (location.x < -diameter / 2)
  583. {
  584. location.x = width+diameter /2;
  585. f.setHead (location);
  586. }
  587. else if (location.x > width+diameter /2)
  588. {
  589. location.x = -diameter /2;
  590. f.setHead (location);
  591. }
  592.  
  593. if (location.y < -diameter /2)
  594. {
  595. location.y = height+diameter /2;
  596. f.setHead (location);
  597. }
  598. else if (location.y > height+diameter /2)
  599. {
  600. location.y = -diameter /2;
  601. f.setHead (location);
  602. }
  603. }
  604.  
  605. void checkEdgesAndBounce ()
  606. {
  607. PVector location = f.getHead();
  608. float radius = f.getSize() / 2;
  609.  
  610. if (location.x < radius )
  611. {
  612. location.x = radius ;
  613. f.setHead (location);
  614. direction.x = direction.x * -1;
  615. }
  616. else if (location.x > width-radius )
  617. {
  618. location.x = width-radius ;
  619. f.setHead (location);
  620. direction.x *= -1;
  621. }
  622.  
  623. if (location.y < radius )
  624. {
  625. location.y = radius ;
  626. f.setHead (location);
  627. direction.y *= -1;
  628. }
  629. else if (location.y > height-radius )
  630. {
  631. location.y = height-radius ;
  632. f.setHead (location);
  633. direction.y *= -1;
  634. }
  635. }
  636.  
  637. // DISPLAY ---------------------------------------------------------------
  638.  
  639. void display ()
  640. {
  641. f.display();
  642. }
  643. }
  644.  
  645.  
  646.  
  647. void keyPressed ()
  648. {
  649. if ( key == 'l') followLeader = !followLeader;
  650. if ( key == 'p') hidefromPredator = !hidefromPredator;
  651. if (key == ' ')
  652. {
  653. bewegungsModus++;
  654. if (bewegungsModus > 5) bewegungsModus = 0;
  655. }
  656. }
Add Comment
Please, Sign In to add comment