Guest User

Untitled

a guest
Oct 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. var width = window.innerWidth;
  2. var height = window.innerHeight;
  3. var timerID = 0;
  4. var c = document.getElementById('canvas')
  5. var ctx = c.getContext('2d');
  6. c.width = width;
  7. c.height = height;
  8.  
  9.  
  10. var speed = 10;
  11. var size = 8;
  12. var boids = [];
  13. var totalBoids = 150;
  14.  
  15. var init = function(){
  16.  
  17. for (var i = 0; i < totalBoids; i++) {
  18.  
  19. boids.push({
  20. x: Math.random() * width,
  21. y: Math.random() * height,
  22. v: {
  23. x: Math.random() * 2 - 1,
  24. y: Math.random() * 2 - 1
  25. },
  26. c: 'rgba(' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ', 1.0)'
  27. });
  28. }
  29. setInterval(update, 40);
  30. }
  31.  
  32. var calculateDistance = function(v1, v2){
  33. x = Math.abs(v1.x - v2.x);
  34. y = Math.abs(v1.y - v2.y);
  35.  
  36. return Math.sqrt((x * x) + (y * y));
  37. }
  38.  
  39. var checkWallCollisions = function(index){
  40. if (boids[index].x > width) {
  41. boids[index].x = 0;
  42. }
  43. else
  44. if (boids[index].x < 0) {
  45. boids[index].x = width;
  46. }
  47.  
  48. if (boids[index].y > height) {
  49. boids[index].y = 0;
  50. }
  51. else
  52. if (boids[index].y < 0) {
  53. boids[index].y = height;
  54. }
  55. }
  56.  
  57. var addForce = function(index, force){
  58.  
  59. boids[index].v.x += force.x;
  60. boids[index].v.y += force.y;
  61.  
  62. magnitude = calculateDistance({
  63. x: 0,
  64. y: 0
  65. }, {
  66. x: boids[index].v.x,
  67. y: boids[index].v.y
  68. });
  69.  
  70. boids[index].v.x = boids[index].v.x / magnitude;
  71. boids[index].v.y = boids[index].v.y / magnitude;
  72. }
  73.  
  74. //This should be in multiple functions, but this will
  75. //save tons of looping - Gross!
  76. var applyForces = function(index){
  77. percievedCenter = {
  78. x: 0,
  79. y: 0
  80. };
  81. flockCenter = {
  82. x: 0,
  83. y: 0
  84. };
  85. percievedVelocity = {
  86. x: 0,
  87. y: 0
  88. };
  89. count = 0;
  90. for (var i = 0; i < boids.length; i++) {
  91. if (i != index) {
  92.  
  93. //Allignment
  94. dist = calculateDistance(boids[index], boids[i]);
  95.  
  96. //console.log(dist);
  97. if (dist > 0 && dist < 50) {
  98. count++;
  99.  
  100. //Alignment
  101. percievedCenter.x += boids[i].x;
  102. percievedCenter.y += boids[i].y;
  103.  
  104. //Cohesion
  105. percievedVelocity.x += boids[i].v.x;
  106. percievedVelocity.y += boids[i].v.y;
  107. //Seperation
  108. if (calculateDistance(boids[i], boids[index]) < 12) {
  109. flockCenter.x -= (boids[i].x - boids[index].x);
  110. flockCenter.y -= (boids[i].y - boids[index].y);
  111. }
  112. }
  113. }
  114. }
  115. if (count > 0) {
  116. percievedCenter.x = percievedCenter.x / count;
  117. percievedCenter.y = percievedCenter.y / count;
  118.  
  119. percievedCenter.x = (percievedCenter.x - boids[index].x) / 400;
  120. percievedCenter.y = (percievedCenter.y - boids[index].y) / 400;
  121.  
  122. percievedVelocity.x = percievedVelocity.x / count;
  123. percievedVelocity.y = percievedVelocity.y / count;
  124.  
  125. flockCenter.x /= count;
  126. flockCenter.y /= count;
  127. }
  128.  
  129. addForce(index, percievedCenter);
  130.  
  131. addForce(index, percievedVelocity);
  132.  
  133. addForce(index, flockCenter);
  134. }
  135.  
  136. var update = function(){
  137.  
  138.  
  139. clearCanvas();
  140. for (var i = 0; i < boids.length; i++) {
  141.  
  142. //Draw boid
  143.  
  144. ctx.beginPath();
  145. ctx.strokeStyle = boids[i].c;
  146.  
  147. ctx.lineWidth = size;
  148. ctx.moveTo(boids[i].x, boids[i].y);
  149. boids[i].x += boids[i].v.x * speed;
  150. boids[i].y += boids[i].v.y * speed;
  151. applyForces(i);
  152. ctx.lineTo(boids[i].x, boids[i].y);
  153. ctx.stroke();
  154. ctx.fill();
  155.  
  156. checkWallCollisions(i);
  157.  
  158. }
  159. }
  160.  
  161. //Gui uses this to clear the canvas
  162. var clearCanvas = function(){
  163. ctx.fillStyle = 'rgba(255, 255, 255, 1.0)';
  164. ctx.beginPath();
  165. ctx.rect(0, 0, width, height);
  166. ctx.closePath();
  167. ctx.fill();
  168. }
  169.  
  170. init();
Add Comment
Please, Sign In to add comment