API Docs for: 0.6.1
Show:

File: src/collision/NaiveBroadphase.js

  1. module.exports = NaiveBroadphase;
  2.  
  3. var Broadphase = require('./Broadphase');
  4. var AABB = require('./AABB');
  5.  
  6. /**
  7. * Naive broadphase implementation, used in lack of better ones.
  8. * @class NaiveBroadphase
  9. * @constructor
  10. * @description The naive broadphase looks at all possible pairs without restriction, therefore it has complexity N^2 (which is bad)
  11. * @extends Broadphase
  12. */
  13. function NaiveBroadphase(){
  14. Broadphase.apply(this);
  15. }
  16. NaiveBroadphase.prototype = new Broadphase();
  17. NaiveBroadphase.prototype.constructor = NaiveBroadphase;
  18.  
  19. /**
  20. * Get all the collision pairs in the physics world
  21. * @method collisionPairs
  22. * @param {World} world
  23. * @param {Array} pairs1
  24. * @param {Array} pairs2
  25. */
  26. NaiveBroadphase.prototype.collisionPairs = function(world,pairs1,pairs2){
  27. var bodies = world.bodies,
  28. n = bodies.length,
  29. i,j,bi,bj;
  30.  
  31. // Naive N^2 ftw!
  32. for(i=0; i!==n; i++){
  33. for(j=0; j!==i; j++){
  34.  
  35. bi = bodies[i];
  36. bj = bodies[j];
  37.  
  38. if(!this.needBroadphaseCollision(bi,bj)){
  39. continue;
  40. }
  41.  
  42. this.intersectionTest(bi,bj,pairs1,pairs2);
  43. }
  44. }
  45. };
  46.  
  47. var tmpAABB = new AABB();
  48.  
  49. /**
  50. * Returns all the bodies within an AABB.
  51. * @method aabbQuery
  52. * @param {World} world
  53. * @param {AABB} aabb
  54. * @param {array} result An array to store resulting bodies in.
  55. * @return {array}
  56. */
  57. NaiveBroadphase.prototype.aabbQuery = function(world, aabb, result){
  58. result = result || [];
  59.  
  60. for(var i = 0; i < world.bodies.length; i++){
  61. var b = world.bodies[i];
  62.  
  63. if(b.aabbNeedsUpdate){
  64. b.computeAABB();
  65. }
  66.  
  67. // Ugly hack until Body gets aabb
  68. if(b.aabb.overlaps(aabb)){
  69. result.push(b);
  70. }
  71. }
  72.  
  73. return result;
  74. };