You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.2 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Adapted from Flocking Processing example by Daniel Schiffman:
  2. // http://processing.org/learning/topics/flocking.html
  3. project.currentStyle = {
  4. strokeColor: 'white',
  5. strokeWidth: 2,
  6. strokeCap: 'round'
  7. };
  8. new Path.Rectangle(view.bounds).fillColor = 'black';
  9. var head = new Path.Ellipse([0, 0], [13, 8]);
  10. head.fillColor = 'white';
  11. head.strokeColor = null;
  12. var headSymbol = new SymbolDefinition(head);
  13. var size = view.size;
  14. var Boid = Base.extend({
  15. initialize: function(position, maxSpeed, maxForce) {
  16. var strength = Math.random() * 0.5;
  17. this.acc = new Point(0, 0);
  18. this.vel = Point.random() * 2 - 1;
  19. this.loc = position.clone();
  20. this.r = 30;
  21. this.maxSpeed = maxSpeed + strength;
  22. this.maxForce = maxForce + strength;
  23. this.head = headSymbol.place();
  24. this.path = new Path();
  25. this.shortPath = new Path();
  26. this.shortPath.strokeWidth = 4;
  27. for (var i = 0, l = strength * 10 + 10; i < l; i++) {
  28. this.path.add(this.loc);
  29. if (i < 3)
  30. this.shortPath.add(this.loc);
  31. }
  32. this.firstSegment = this.path.segments[0];
  33. this.count = 0;
  34. this.lastRot = 0;
  35. },
  36. run: function(boids) {
  37. this.lastLoc = this.loc.clone();
  38. if (!groupTogether) {
  39. this.flock(boids);
  40. } else {
  41. this.align(boids);
  42. }
  43. this.borders();
  44. this.update();
  45. this.firstSegment.point = this.loc;
  46. var lastPoint = this.firstSegment.point;
  47. var lastVector = this.loc - this.lastLoc;
  48. var segments = this.path.segments;
  49. for (var i = 1, l = segments.length; i < l; i++) {
  50. var segment = segments[i];
  51. var vector = lastPoint - segment.point;
  52. this.count += this.vel.length * 10;
  53. var rotLength = Math.sin((this.count + i * 3) / 300);
  54. var rotated = lastVector.rotate(90).normalize(rotLength);
  55. lastPoint += lastVector.normalize(-5 - this.vel.length / 3);
  56. segment.point = lastPoint;
  57. segment.point += rotated;
  58. lastVector = vector;
  59. }
  60. this.path.smooth();
  61. this.head.position = this.loc;
  62. var vector = this.loc - this.lastLoc;
  63. var rot = vector.angle;
  64. this.head.rotate(rot - this.lastRot);
  65. this.lastRot = rot;
  66. var shortSegments = this.shortPath.segments;
  67. for (var i = 0; i < 3; i++)
  68. shortSegments[i] = segments[i].clone();
  69. },
  70. // We accumulate a new acceleration each time based on three rules
  71. flock: function(boids) {
  72. var sep = this.separate(boids) * 3;
  73. var ali = this.align(boids);
  74. var coh = this.cohesion(boids);
  75. this.acc += sep + ali + coh;
  76. },
  77. update: function() {
  78. // Update velocity
  79. this.vel += this.acc;
  80. // Limit speed (vector#limit?)
  81. this.vel.length = Math.min(this.maxSpeed, this.vel.length);
  82. this.loc += this.vel;
  83. // Reset acceleration to 0 each cycle
  84. this.acc.length = 0;
  85. },
  86. seek: function(target) {
  87. this.acc += this.steer(target, false);
  88. },
  89. arrive: function(target) {
  90. this.acc += this.steer(target, true);
  91. },
  92. // A method that calculates a steering vector towards a target
  93. // Takes a second argument, if true, it slows down as it approaches
  94. // the target
  95. steer: function(target, slowdown) {
  96. var steer,
  97. desired = target - this.loc,
  98. d = desired.length;
  99. if (d > 0) {
  100. // Two options for desired vector magnitude
  101. // (1 -- based on distance, 2 -- maxSpeed)
  102. if (slowdown && d < 100) {
  103. // // This damping is somewhat arbitrary:
  104. desired.length = this.maxSpeed * (d / 100);
  105. } else {
  106. desired.length = this.maxSpeed;
  107. }
  108. steer = desired - this.vel;
  109. steer.length = Math.min(this.maxForce, steer.length);
  110. } else {
  111. steer = new Point(0, 0);
  112. }
  113. return steer;
  114. },
  115. borders: function() {
  116. var loc = this.loc;
  117. var r = this.r;
  118. var oldLoc = this.loc.clone();
  119. var width = size.width;
  120. var height = size.height;
  121. if (loc.x < -r) loc.x = width + r;
  122. if (loc.y < -r) loc.y = height + r;
  123. if (loc.x > width + r) loc.x = -r;
  124. if (loc.y > height + r) loc.y = -r;
  125. var vector = this.loc - oldLoc;
  126. if (!vector.isZero())
  127. this.path.position += vector;
  128. },
  129. separate: function(boids) {
  130. var desiredSeperation = 60;
  131. var steer = new Point(0, 0);
  132. var count = 0;
  133. // For every boid in the system, check if it's too close
  134. for (var i = 0, l = boids.length; i < l; i++) {
  135. var other = boids[i];
  136. var d = other.loc.getDistance(this.loc);
  137. if (d > 0 && d < desiredSeperation) {
  138. // Calculate vector pointing away from neighbor
  139. var diff = this.loc - other.loc;
  140. steer += diff.normalize(1 / d);
  141. count++;
  142. }
  143. }
  144. // Average -- divide by how many
  145. if (count > 0)
  146. steer /= count;
  147. if (steer.length > 0) {
  148. // Implement Reynolds: Steering = Desired - Velocity
  149. steer.length = this.maxSpeed;
  150. steer -= this.vel;
  151. steer.length = Math.min(steer.length, this.maxForce);
  152. }
  153. return steer;
  154. },
  155. // Alignment
  156. // For every nearby boid in the system, calculate the average velocity
  157. align: function(boids) {
  158. var neighborDist = 25;
  159. var steer = new Point(0, 0);
  160. var count = 0;
  161. var nearest = 999;
  162. var closestPoint;
  163. for (var i = 0, l = boids.length; i < l; i++) {
  164. var other = boids[i];
  165. var d = this.loc.getDistance(other.loc);
  166. if (d > 0 && d < nearest) {
  167. closestPoint = other.loc;
  168. nearest = d;
  169. }
  170. if (d > 0 && d < neighborDist) {
  171. steer += other.vel;
  172. count++;
  173. }
  174. }
  175. if (count > 0)
  176. steer /= count;
  177. if (steer.length > 0) {
  178. // Implement Reynolds: Steering = Desired - Velocity
  179. steer.length = this.maxSpeed;
  180. steer -= this.vel;
  181. steer.length = Math.min(steer.length, this.maxForce);
  182. }
  183. return steer;
  184. },
  185. // Cohesion
  186. // For the average location (i.e. center) of all nearby boids,
  187. // calculate steering vector towards that location
  188. cohesion: function(boids) {
  189. var neighborDist = 100;
  190. var sum = new Point(0, 0);
  191. var count = 0;
  192. for (var i = 0, l = boids.length; i < l; i++) {
  193. var other = boids[i];
  194. var d = this.loc.getDistance(other.loc);
  195. if (d > 0 && d < neighborDist) {
  196. sum += other.loc; // Add location
  197. count++;
  198. }
  199. }
  200. if (count > 0) {
  201. sum /= count;
  202. // Steer towards the location
  203. return this.steer(sum, false);
  204. }
  205. return sum;
  206. }
  207. });
  208. var heartPath = new Path([
  209. [[514.6962890625, 624.703125], [7.0966796875, -26.3369140625], [-7.10205078125, -27.0244140625]],
  210. [[484.29052734375, 548.6025390625], [13.16845703125, 23.7060546875], [-13.173828125, -23.70703125]],
  211. [[407.84619140625, 438.14453125], [37.79296875, 49.935546875], [-27.71630859375, -36.6435546875]],
  212. [[356.654296875, 368.400390625], [6.41015625, 9.8505859375], [-10.53759765625, -16.02978515625]],
  213. [[333.80712890625, 324.25146484375], [4.69189453125, 13.3994140625], [-4.697265625, -13.39892578125]],
  214. [[326.76416015625, 283.53857421875], [0, 13.74267578125], [0, -25.42431640625]],
  215. [[352.18798828125, 219.634765625], [-16.95263671875, 17.17822265625], [16.94775390625, -17.1787109375]],
  216. [[415.0615234375, 193.8671875], [-24.96826171875, 0], [25.19287109375, 0]],
  217. [[480.68310546875, 220.66552734375], [-18.552734375, -17.86572265625], [13.96826171875, 13.28662109375]],
  218. [[514.6962890625, 280.10302734375], [-8.70703125, -26.3369140625], [7.55859375, -25.88037109375]],
  219. [[546.6484375, 221.0087890625], [-13.7431640625, 13.517578125], [19.0087890625, -18.32177734375]],
  220. [[612.61328125, 193.5234375], [-24.9677734375, 0], [24.7373046875, 0]],
  221. [[675.486328125, 219.119140625], [-17.177734375, -17.06005859375], [17.1787109375, 17.06591796875]],
  222. [[701.2548828125, 280.10302734375], [0, -23.58837890625], [0, 20.61376953125]],
  223. [[686.1376953125, 344.52197265625], [10.076171875, -22.33203125], [-10.08203125, 22.33203125]],
  224. [[627.73046875, 432.3046875], [28.8603515625, -36.1875], [-37.5673828125, 47.412109375]],
  225. [[545.6171875, 549.1171875], [17.1787109375, -30.458984375], [-13.517578125, 24.0498046875]]
  226. ]);
  227. heartPath.closed = true;
  228. heartPath.position = view.center;
  229. heartPath.strokeColor = null;
  230. heartPath.scale(1.5);
  231. var groupTogether = false;
  232. var pathLength = heartPath.length;
  233. var mouseDown = false;
  234. var boids = [];
  235. // Add the boids:
  236. for (var i = 0; i < 300; i++) {
  237. var position = view.center;
  238. boids.push(new Boid(position, 10, 0.05));
  239. }
  240. function onFrame(event) {
  241. for (var i = 0, l = boids.length; i < l; i++) {
  242. if (groupTogether) {
  243. var length = ((i + event.count / 30) % l) / l * pathLength;
  244. var point = heartPath.getPointAt(length);
  245. boids[i].arrive(point);
  246. }
  247. boids[i].run(boids);
  248. }
  249. }
  250. // Reposition the heart path whenever the window is resized:
  251. function onResize(event) {
  252. size = view.size;
  253. heartPath.position = view.center;
  254. }