insert.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var test = require('tap').test;
  2. var dimensions = 2;
  3. var createQuadTree = require('../lib/codeGenerators/generateQuadTree')(dimensions);
  4. var Body = require('../lib/codeGenerators/generateCreateBody')(dimensions);
  5. var random = require('ngraph.random').random(42);
  6. test('insert and update update forces', function (t) {
  7. var tree = createQuadTree({}, random);
  8. var body = new Body();
  9. var clone = JSON.parse(JSON.stringify(body));
  10. tree.insertBodies([body]);
  11. tree.updateBodyForce(body);
  12. t.same(body, clone, 'The body should not be changed - there are no forces acting on it');
  13. t.end();
  14. });
  15. test('it can get root', function (t) {
  16. var tree = createQuadTree({}, random);
  17. var body = new Body();
  18. tree.insertBodies([body]);
  19. var root = tree.getRoot();
  20. t.ok(root, 'Root is present');
  21. t.equal(root.body, body, 'Body is initialized');
  22. t.end();
  23. });
  24. test('Two bodies repel each other', function (t) {
  25. var tree = createQuadTree({}, random);
  26. var bodyA = new Body(); bodyA.pos.x = 1; bodyA.pos.y = 0;
  27. var bodyB = new Body(); bodyB.pos.x = 2; bodyB.pos.y = 0;
  28. tree.insertBodies([bodyA, bodyB]);
  29. tree.updateBodyForce(bodyA);
  30. tree.updateBodyForce(bodyB);
  31. // based on our physical model construction forces should be equivalent, with
  32. // opposite sign:
  33. t.ok(bodyA.force.x + bodyB.force.x === 0, 'Forces should be same, with opposite sign');
  34. t.ok(bodyA.force.x !== 0, 'X-force for body A should not be zero');
  35. t.ok(bodyB.force.x !== 0, 'X-force for body B should not be zero');
  36. // On the other hand, our bodies should not move by Y axis:
  37. t.ok(bodyA.force.y === 0, 'Y-force for body A should be zero');
  38. t.ok(bodyB.force.y === 0, 'Y-force for body B should be zero');
  39. t.end();
  40. });
  41. test('Can handle two bodies at the same location', function (t) {
  42. var tree = createQuadTree({}, random);
  43. var bodyA = new Body();
  44. var bodyB = new Body();
  45. tree.insertBodies([bodyA, bodyB]);
  46. tree.updateBodyForce(bodyA);
  47. tree.updateBodyForce(bodyB);
  48. t.end();
  49. });
  50. test('it does not stuck', function(t) {
  51. var count = 60000;
  52. var bodies = [];
  53. for (var i = 0; i < count; ++i) {
  54. bodies.push(new Body(Math.random(), Math.random()));
  55. }
  56. var quadTree = createQuadTree({}, random);
  57. quadTree.insertBodies(bodies);
  58. bodies.forEach(function(body) {
  59. quadTree.updateBodyForce(body);
  60. });
  61. t.ok(1);
  62. t.end();
  63. });