simulator.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* eslint-disable no-shadow */
  2. var test = require('tap').test;
  3. var dimensions = 2;
  4. var Body = require('../lib/codeGenerators/generateCreateBody')(dimensions);
  5. var createSimulator = require('../lib/createPhysicsSimulator');
  6. test('Can step without bodies', function (t) {
  7. var simulator = createSimulator();
  8. t.equal(simulator.bodies.length, 0, 'There should be no bodies');
  9. t.equal(simulator.springs.length, 0, 'There should be no springs');
  10. simulator.step();
  11. t.end();
  12. });
  13. test('it has settings exposed', function(t) {
  14. var mySettings = { };
  15. var simulator = createSimulator(mySettings);
  16. t.ok(mySettings === simulator.settings, 'settings are exposed');
  17. t.end();
  18. });
  19. test('it gives amount of total movement', function(t) {
  20. var simulator = createSimulator();
  21. var body1 = new Body(-10, 0);
  22. var body2 = new Body(10, 0);
  23. simulator.addBody(body1);
  24. simulator.addBody(body2);
  25. simulator.step();
  26. var totalMoved = simulator.getTotalMovement();
  27. t.ok(!isNaN(totalMoved), 'Amount of total movement is returned');
  28. t.end();
  29. });
  30. test('it can add a body at given position', function(t) {
  31. var simulator = createSimulator();
  32. var pos1 = {x: -10, y: 0};
  33. var pos2 = {x: 10, y: 0};
  34. simulator.addBodyAt(pos1);
  35. simulator.addBodyAt(pos2);
  36. t.equal(simulator.bodies.length, 2, 'All bodies are added');
  37. var body1 = simulator.bodies[0];
  38. t.equal(body1.pos.x, -10, 'X is there');
  39. t.equal(body1.pos.y, 0, 'Y is there');
  40. var body2 = simulator.bodies[1];
  41. t.equal(body2.pos.x, 10, 'X is there');
  42. t.equal(body2.pos.y, 0, 'Y is there');
  43. t.end();
  44. });
  45. test('Does not update position of one body', function (t) {
  46. var simulator = createSimulator();
  47. var body = new Body(0, 0);
  48. simulator.addBody(body);
  49. simulator.step(1);
  50. t.equal(simulator.bodies.length, 1, 'Number of bodies is 1');
  51. t.equal(simulator.springs.length, 0, 'Number of springs is 0');
  52. t.equal(simulator.bodies[0], body, 'Body points to actual object');
  53. t.equal(body.pos.x, 0, 'X is not changed');
  54. t.equal(body.pos.y, 0, 'Y is not changed');
  55. t.end();
  56. });
  57. test('throws on no body or no pos', t => {
  58. var simulator = createSimulator();
  59. t.throws(() => simulator.addBody(), /Body is required/);
  60. t.throws(() => simulator.addBodyAt(), /Body position is required/);
  61. t.end();
  62. });
  63. test('throws on no spring', t => {
  64. var simulator = createSimulator();
  65. t.throws(() => simulator.addSpring(), /Cannot add null spring to force simulator/);
  66. t.end();
  67. });
  68. test('Can add and remove forces', function (t) {
  69. var simulator = createSimulator();
  70. var testForce = function () {};
  71. simulator.addForce('foo', testForce);
  72. t.equal(simulator.getForces().get('foo'), testForce);
  73. simulator.removeForce('foo');
  74. t.equal(simulator.getForces().get('foo'), undefined);
  75. simulator.removeForce('foo');
  76. // should still be good
  77. t.end();
  78. });
  79. test('Can configure forces', function (t) {
  80. t.test('Gravity', function (t) {
  81. var simulator = createSimulator();
  82. var body1 = new Body(0, 0);
  83. var body2 = new Body(1, 0);
  84. simulator.addBody(body1);
  85. simulator.addBody(body2);
  86. simulator.step();
  87. // by default gravity is negative, bodies should repel each other:
  88. var x1 = body1.pos.x;
  89. var x2 = body2.pos.x;
  90. t.ok(x1 < 0, 'Body 1 moves away from body 2');
  91. t.ok(x2 > 1, 'Body 2 moves away from body 1');
  92. // now reverse gravity, and bodies should attract each other:
  93. simulator.gravity(100);
  94. simulator.step();
  95. t.ok(body1.pos.x > x1, 'Body 1 moved towards body 2');
  96. t.ok(body2.pos.x < x2, 'Body 2 moved towards body 1');
  97. t.end();
  98. });
  99. t.test('Drag', function (t) {
  100. var simulator = createSimulator();
  101. var body1 = new Body(0, 0);
  102. body1.velocity.x = -1; // give it small impulse
  103. simulator.addBody(body1);
  104. simulator.step();
  105. var x1 = body1.velocity.x;
  106. // by default drag force will slow down entire system:
  107. t.ok(x1 > -1, 'Body 1 moves at reduced speed');
  108. // Restore original velocity, but now set drag force to 0
  109. body1.velocity.x = -1;
  110. simulator.dragCoefficient(0);
  111. simulator.step();
  112. t.ok(body1.velocity.x === -1, 'Velocity should remain unchanged');
  113. t.end();
  114. });
  115. t.end();
  116. });
  117. test('Can remove bodies', function (t) {
  118. var simulator = createSimulator();
  119. var body = new Body(0, 0);
  120. simulator.addBody(body);
  121. t.equal(simulator.bodies.length, 1, 'Number of bodies is 1');
  122. var result = simulator.removeBody(body);
  123. t.equal(result, true, 'body successfully removed');
  124. t.equal(simulator.bodies.length, 0, 'Number of bodies is 0');
  125. t.end();
  126. });
  127. test('Updates position for two bodies', function (t) {
  128. var simulator = createSimulator();
  129. var body1 = new Body(-1, 0);
  130. var body2 = new Body(1, 0);
  131. simulator.addBody(body1);
  132. simulator.addBody(body2);
  133. simulator.step();
  134. t.equal(simulator.bodies.length, 2, 'Number of bodies is 2');
  135. t.ok(body1.pos.x !== 0, 'Body1.X has changed');
  136. t.ok(body2.pos.x !== 0, 'Body2.X has changed');
  137. t.equal(body1.pos.y, 0, 'Body1.Y has not changed');
  138. t.equal(body2.pos.y, 0, 'Body2.Y has not changed');
  139. t.end();
  140. });
  141. test('add spring should not add bodies', function (t) {
  142. var simulator = createSimulator();
  143. var body1 = new Body(-1, 0);
  144. var body2 = new Body(1, 0);
  145. simulator.addSpring(body1, body2, 10);
  146. t.equal(simulator.bodies.length, 0, 'Should not add two bodies');
  147. t.equal(simulator.bodies.length, 0, 'Should not add two bodies');
  148. t.equal(simulator.springs.length, 1, 'Should have a spring');
  149. t.end();
  150. });
  151. test('Spring affects bodies positions', function (t) {
  152. var simulator = createSimulator();
  153. var body1 = new Body(-10, 0);
  154. var body2 = new Body(10, 0);
  155. simulator.addBody(body1);
  156. simulator.addBody(body2);
  157. // If you take this out, bodies will repel each other:
  158. simulator.addSpring(body1, body2, 1);
  159. simulator.step();
  160. t.ok(body1.pos.x > -10, 'Body 1 should move towards body 2');
  161. t.ok(body2.pos.x < 10, 'Body 2 should move towards body 1');
  162. t.end();
  163. });
  164. test('Can remove springs', function (t) {
  165. var simulator = createSimulator();
  166. var body1 = new Body(-10, 0);
  167. var body2 = new Body(10, 0);
  168. simulator.addBody(body1);
  169. simulator.addBody(body2);
  170. var spring = simulator.addSpring(body1, body2, 1);
  171. simulator.removeSpring(spring);
  172. simulator.step();
  173. t.ok(body1.pos.x < -10, 'Body 1 should move away from body 2');
  174. t.ok(body2.pos.x > 10, 'Body 2 should move away from body 1');
  175. t.end();
  176. });
  177. test('Get bounding box', function (t) {
  178. var simulator = createSimulator();
  179. var body1 = new Body(0, 0);
  180. var body2 = new Body(10, 10);
  181. simulator.addBody(body1);
  182. simulator.addBody(body2);
  183. simulator.step(); // this will move bodies farther away
  184. var bbox = simulator.getBBox();
  185. t.ok(bbox.min_x <= 0, 'Left is 0');
  186. t.ok(bbox.min_y <= 0, 'Top is 0');
  187. t.ok(bbox.max_x >= 10, 'right is 10');
  188. t.ok(bbox.max_y >= 10, 'bottom is 10');
  189. t.end();
  190. });
  191. test('it updates bounding box', function (t) {
  192. var simulator = createSimulator();
  193. var body1 = new Body(0, 0);
  194. var body2 = new Body(10, 10);
  195. simulator.addBody(body1);
  196. simulator.addBody(body2);
  197. var bbox = simulator.getBBox();
  198. t.ok(bbox.min_x === 0, 'Left is 0');
  199. t.ok(bbox.min_y === 0, 'Top is 0');
  200. t.ok(bbox.max_x === 10, 'right is 10');
  201. t.ok(bbox.max_y === 10, 'bottom is 10');
  202. body1.setPosition(15, 15);
  203. simulator.invalidateBBox();
  204. bbox = simulator.getBBox();
  205. t.ok(bbox.min_x === 10, 'Left is 10');
  206. t.ok(bbox.min_y === 10, 'Top is 10');
  207. t.ok(bbox.max_x === 15, 'right is 15');
  208. t.ok(bbox.max_y === 15, 'bottom is 15');
  209. t.end();
  210. });
  211. test('Get best position', function (t) {
  212. t.test('can get with empty simulator', function (t) {
  213. var simulator = createSimulator();
  214. var empty = simulator.getBestNewBodyPosition([]);
  215. t.ok(typeof empty.x === 'number', 'Has X');
  216. t.ok(typeof empty.y === 'number', 'Has Y');
  217. t.end();
  218. });
  219. t.end();
  220. });
  221. test('it can change settings', function(t) {
  222. var simulator = createSimulator();
  223. var currentTheta = simulator.theta();
  224. t.ok(typeof currentTheta === 'number', 'theta is here');
  225. simulator.theta(1.2);
  226. t.equal(simulator.theta(), 1.2, 'theta is changed');
  227. var currentSpringCoefficient = simulator.springCoefficient();
  228. t.ok(typeof currentSpringCoefficient === 'number', 'springCoefficient is here');
  229. simulator.springCoefficient(0.8);
  230. t.equal(simulator.springCoefficient(), 0.8, 'springCoefficient is changed');
  231. var gravity = simulator.gravity();
  232. t.ok(typeof gravity === 'number', 'gravity is here');
  233. simulator.gravity(-0.8);
  234. t.equal(simulator.gravity(), -0.8, 'gravity is changed');
  235. var springLength = simulator.springLength();
  236. t.ok(typeof springLength === 'number', 'springLength is here');
  237. simulator.springLength(80);
  238. t.equal(simulator.springLength(), 80, 'springLength is changed');
  239. var dragCoefficient = simulator.dragCoefficient();
  240. t.ok(typeof dragCoefficient === 'number', 'dragCoefficient is here');
  241. simulator.dragCoefficient(0.8);
  242. t.equal(simulator.dragCoefficient(), 0.8, 'dragCoefficient is changed');
  243. var timeStep = simulator.timeStep();
  244. t.ok(typeof timeStep === 'number', 'timeStep is here');
  245. simulator.timeStep(8);
  246. t.equal(simulator.timeStep(), 8, 'timeStep is changed');
  247. t.end();
  248. });
  249. test('it can augment string setter values', function (t) {
  250. var simulator = createSimulator({
  251. name: 'John'
  252. });
  253. simulator.name('Alisa');
  254. t.equal(simulator.name(), 'Alisa', 'name is Alisa');
  255. t.end();
  256. });
  257. test('it ignores body that does not exist', function(t) {
  258. var simulator = createSimulator();
  259. var body = new Body(0, 0);
  260. simulator.addBody(body);
  261. simulator.removeBody({});
  262. t.equal(simulator.bodies.length, 1, 'Should ignore body that does not exist');
  263. t.end();
  264. });
  265. test('it throws on springCoeff', function (t) {
  266. t.throws(function () {
  267. createSimulator({springCoeff: 1});
  268. }, 'springCoeff was renamed to springCoefficient');
  269. t.end();
  270. });
  271. test('it throws on dragCoeff', function (t) {
  272. t.throws(function () {
  273. createSimulator({dragCoeff: 1});
  274. }, 'dragCoeff was renamed to dragCoefficient');
  275. t.end();
  276. });