PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/QuadTree.js.html

https://gitlab.com/lobsterhands/phaser
HTML | 918 lines | 702 code | 213 blank | 3 comment | 0 complexity | 9db412155c8e7a397630350c855c9d7f MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Phaser Source: math/QuadTree.js</title>
  6. <!--[if lt IE 9]>
  7. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  8. <![endif]-->
  9. <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
  10. <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
  11. </head>
  12. <body>
  13. <div class="container-fluid">
  14. <div class="navbar navbar-fixed-top navbar-inverse">
  15. <div class="navbar-inner">
  16. <a class="brand" href="index.html">Phaser</a>
  17. <ul class="nav">
  18. <li class="dropdown">
  19. <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
  20. class="caret"></b></a>
  21. <ul class="dropdown-menu ">
  22. <li class="class-depth-0">
  23. <a href="Phaser.html">Phaser</a>
  24. </li>
  25. </ul>
  26. </li>
  27. <li class="dropdown">
  28. <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
  29. class="caret"></b></a>
  30. <ul class="dropdown-menu ">
  31. <li class="class-depth-1">
  32. <a href="Phaser.Animation.html">Animation</a>
  33. </li>
  34. <li class="class-depth-1">
  35. <a href="Phaser.AnimationManager.html">AnimationManager</a>
  36. </li>
  37. <li class="class-depth-1">
  38. <a href="Phaser.AnimationParser.html">AnimationParser</a>
  39. </li>
  40. <li class="class-depth-1">
  41. <a href="Phaser.BitmapData.html">BitmapData</a>
  42. </li>
  43. <li class="class-depth-1">
  44. <a href="Phaser.BitmapText.html">BitmapText</a>
  45. </li>
  46. <li class="class-depth-1">
  47. <a href="Phaser.Button.html">Button</a>
  48. </li>
  49. <li class="class-depth-1">
  50. <a href="Phaser.Cache.html">Cache</a>
  51. </li>
  52. <li class="class-depth-1">
  53. <a href="Phaser.Camera.html">Camera</a>
  54. </li>
  55. <li class="class-depth-1">
  56. <a href="Phaser.Canvas.html">Canvas</a>
  57. </li>
  58. <li class="class-depth-1">
  59. <a href="Phaser.Circle.html">Circle</a>
  60. </li>
  61. <li class="class-depth-1">
  62. <a href="Phaser.Color.html">Color</a>
  63. </li>
  64. <li class="class-depth-1">
  65. <a href="Phaser.Device.html">Device</a>
  66. </li>
  67. <li class="class-depth-1">
  68. <a href="Phaser.Easing.html">Easing</a>
  69. </li>
  70. <li class="class-depth-2">
  71. <a href="Phaser.Easing.Back.html">Back</a>
  72. </li>
  73. <li class="class-depth-2">
  74. <a href="Phaser.Easing.Bounce.html">Bounce</a>
  75. </li>
  76. <li class="class-depth-2">
  77. <a href="Phaser.Easing.Circular.html">Circular</a>
  78. </li>
  79. <li class="class-depth-2">
  80. <a href="Phaser.Easing.Cubic.html">Cubic</a>
  81. </li>
  82. <li class="class-depth-2">
  83. <a href="Phaser.Easing.Elastic.html">Elastic</a>
  84. </li>
  85. <li class="class-depth-2">
  86. <a href="Phaser.Easing.Exponential.html">Exponential</a>
  87. </li>
  88. <li class="class-depth-2">
  89. <a href="Phaser.Easing.Linear.html">Linear</a>
  90. </li>
  91. <li class="class-depth-2">
  92. <a href="Phaser.Easing.Quadratic.html">Quadratic</a>
  93. </li>
  94. <li class="class-depth-2">
  95. <a href="Phaser.Easing.Quartic.html">Quartic</a>
  96. </li>
  97. <li class="class-depth-2">
  98. <a href="Phaser.Easing.Quintic.html">Quintic</a>
  99. </li>
  100. <li class="class-depth-2">
  101. <a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
  102. </li>
  103. <li class="class-depth-1">
  104. <a href="Phaser.Ellipse.html">Ellipse</a>
  105. </li>
  106. <li class="class-depth-1">
  107. <a href="Phaser.Events.html">Events</a>
  108. </li>
  109. <li class="class-depth-1">
  110. <a href="Phaser.Filter.html">Filter</a>
  111. </li>
  112. <li class="class-depth-1">
  113. <a href="Phaser.Frame.html">Frame</a>
  114. </li>
  115. <li class="class-depth-1">
  116. <a href="Phaser.FrameData.html">FrameData</a>
  117. </li>
  118. <li class="class-depth-1">
  119. <a href="Phaser.Game.html">Game</a>
  120. </li>
  121. <li class="class-depth-1">
  122. <a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
  123. </li>
  124. <li class="class-depth-1">
  125. <a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
  126. </li>
  127. <li class="class-depth-1">
  128. <a href="Phaser.Gamepad.html">Gamepad</a>
  129. </li>
  130. <li class="class-depth-1">
  131. <a href="Phaser.GamepadButton.html">GamepadButton</a>
  132. </li>
  133. <li class="class-depth-1">
  134. <a href="Phaser.Graphics.html">Graphics</a>
  135. </li>
  136. <li class="class-depth-1">
  137. <a href="Phaser.Group.html">Group</a>
  138. </li>
  139. <li class="class-depth-1">
  140. <a href="Phaser.Image.html">Image</a>
  141. </li>
  142. <li class="class-depth-1">
  143. <a href="Phaser.Input.html">Input</a>
  144. </li>
  145. <li class="class-depth-1">
  146. <a href="Phaser.InputHandler.html">InputHandler</a>
  147. </li>
  148. <li class="class-depth-1">
  149. <a href="Phaser.Key.html">Key</a>
  150. </li>
  151. <li class="class-depth-1">
  152. <a href="Phaser.Keyboard.html">Keyboard</a>
  153. </li>
  154. <li class="class-depth-1">
  155. <a href="Phaser.Line.html">Line</a>
  156. </li>
  157. <li class="class-depth-1">
  158. <a href="Phaser.LinkedList.html">LinkedList</a>
  159. </li>
  160. <li class="class-depth-1">
  161. <a href="Phaser.Loader.html">Loader</a>
  162. </li>
  163. <li class="class-depth-1">
  164. <a href="Phaser.LoaderParser.html">LoaderParser</a>
  165. </li>
  166. <li class="class-depth-1">
  167. <a href="Phaser.Math.html">Math</a>
  168. </li>
  169. <li class="class-depth-1">
  170. <a href="Phaser.Mouse.html">Mouse</a>
  171. </li>
  172. <li class="class-depth-1">
  173. <a href="Phaser.MSPointer.html">MSPointer</a>
  174. </li>
  175. <li class="class-depth-1">
  176. <a href="Phaser.Net.html">Net</a>
  177. </li>
  178. <li class="class-depth-1">
  179. <a href="Phaser.Particle.html">Particle</a>
  180. </li>
  181. <li class="class-depth-1">
  182. <a href="Phaser.Particles.html">Particles</a>
  183. </li>
  184. <li class="class-depth-3">
  185. <a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
  186. </li>
  187. <li class="class-depth-1">
  188. <a href="Phaser.Physics.html">Physics</a>
  189. </li>
  190. <li class="class-depth-2">
  191. <a href="Phaser.Physics.Arcade.html">Arcade</a>
  192. </li>
  193. <li class="class-depth-3">
  194. <a href="Phaser.Physics.Arcade.Body.html">Body</a>
  195. </li>
  196. <li class="class-depth-2">
  197. <a href="Phaser.Physics.Ninja.html">Ninja</a>
  198. </li>
  199. <li class="class-depth-3">
  200. <a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
  201. </li>
  202. <li class="class-depth-3">
  203. <a href="Phaser.Physics.Ninja.Body.html">Body</a>
  204. </li>
  205. <li class="class-depth-3">
  206. <a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
  207. </li>
  208. <li class="class-depth-3">
  209. <a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
  210. </li>
  211. <li class="class-depth-2">
  212. <a href="Phaser.Physics.P2.html">P2</a>
  213. </li>
  214. <li class="class-depth-3">
  215. <a href="Phaser.Physics.P2.Body.html">Body</a>
  216. </li>
  217. <li class="class-depth-3">
  218. <a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
  219. </li>
  220. <li class="class-depth-3">
  221. <a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
  222. </li>
  223. <li class="class-depth-3">
  224. <a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
  225. </li>
  226. <li class="class-depth-3">
  227. <a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
  228. </li>
  229. <li class="class-depth-3">
  230. <a href="Phaser.Physics.P2.FixtureList.html">FixtureList</a>
  231. </li>
  232. <li class="class-depth-3">
  233. <a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
  234. </li>
  235. <li class="class-depth-3">
  236. <a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
  237. </li>
  238. <li class="class-depth-3">
  239. <a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
  240. </li>
  241. <li class="class-depth-3">
  242. <a href="Phaser.Physics.P2.Material.html">Material</a>
  243. </li>
  244. <li class="class-depth-3">
  245. <a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
  246. </li>
  247. <li class="class-depth-3">
  248. <a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
  249. </li>
  250. <li class="class-depth-3">
  251. <a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
  252. </li>
  253. <li class="class-depth-3">
  254. <a href="Phaser.Physics.P2.Spring.html">Spring</a>
  255. </li>
  256. <li class="class-depth-1">
  257. <a href="Phaser.Plugin.html">Plugin</a>
  258. </li>
  259. <li class="class-depth-1">
  260. <a href="Phaser.PluginManager.html">PluginManager</a>
  261. </li>
  262. <li class="class-depth-1">
  263. <a href="Phaser.Point.html">Point</a>
  264. </li>
  265. <li class="class-depth-1">
  266. <a href="Phaser.Pointer.html">Pointer</a>
  267. </li>
  268. <li class="class-depth-1">
  269. <a href="Phaser.Polygon.html">Polygon</a>
  270. </li>
  271. <li class="class-depth-1">
  272. <a href="Phaser.QuadTree.html">QuadTree</a>
  273. </li>
  274. <li class="class-depth-1">
  275. <a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
  276. </li>
  277. <li class="class-depth-1">
  278. <a href="Phaser.Rectangle.html">Rectangle</a>
  279. </li>
  280. <li class="class-depth-1">
  281. <a href="Phaser.RenderTexture.html">RenderTexture</a>
  282. </li>
  283. <li class="class-depth-1">
  284. <a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
  285. </li>
  286. <li class="class-depth-1">
  287. <a href="Phaser.RetroFont.html">RetroFont</a>
  288. </li>
  289. <li class="class-depth-1">
  290. <a href="Phaser.ScaleManager.html">ScaleManager</a>
  291. </li>
  292. <li class="class-depth-1">
  293. <a href="Phaser.Signal.html">Signal</a>
  294. </li>
  295. <li class="class-depth-1">
  296. <a href="Phaser.SignalBinding.html">SignalBinding</a>
  297. </li>
  298. <li class="class-depth-1">
  299. <a href="Phaser.SinglePad.html">SinglePad</a>
  300. </li>
  301. <li class="class-depth-1">
  302. <a href="Phaser.Sound.html">Sound</a>
  303. </li>
  304. <li class="class-depth-1">
  305. <a href="Phaser.SoundManager.html">SoundManager</a>
  306. </li>
  307. <li class="class-depth-1">
  308. <a href="Phaser.Sprite.html">Sprite</a>
  309. </li>
  310. <li class="class-depth-1">
  311. <a href="Phaser.SpriteBatch.html">SpriteBatch</a>
  312. </li>
  313. <li class="class-depth-1">
  314. <a href="Phaser.Stage.html">Stage</a>
  315. </li>
  316. <li class="class-depth-1">
  317. <a href="Phaser.State.html">State</a>
  318. </li>
  319. <li class="class-depth-1">
  320. <a href="Phaser.StateManager.html">StateManager</a>
  321. </li>
  322. <li class="class-depth-1">
  323. <a href="Phaser.Text.html">Text</a>
  324. </li>
  325. <li class="class-depth-1">
  326. <a href="Phaser.Tile.html">Tile</a>
  327. </li>
  328. <li class="class-depth-1">
  329. <a href="Phaser.Tilemap.html">Tilemap</a>
  330. </li>
  331. <li class="class-depth-1">
  332. <a href="Phaser.TilemapLayer.html">TilemapLayer</a>
  333. </li>
  334. <li class="class-depth-1">
  335. <a href="Phaser.TilemapParser.html">TilemapParser</a>
  336. </li>
  337. <li class="class-depth-1">
  338. <a href="Phaser.Tileset.html">Tileset</a>
  339. </li>
  340. <li class="class-depth-1">
  341. <a href="Phaser.TileSprite.html">TileSprite</a>
  342. </li>
  343. <li class="class-depth-1">
  344. <a href="Phaser.Time.html">Time</a>
  345. </li>
  346. <li class="class-depth-1">
  347. <a href="Phaser.Timer.html">Timer</a>
  348. </li>
  349. <li class="class-depth-1">
  350. <a href="Phaser.TimerEvent.html">TimerEvent</a>
  351. </li>
  352. <li class="class-depth-1">
  353. <a href="Phaser.Touch.html">Touch</a>
  354. </li>
  355. <li class="class-depth-1">
  356. <a href="Phaser.Tween.html">Tween</a>
  357. </li>
  358. <li class="class-depth-1">
  359. <a href="Phaser.TweenManager.html">TweenManager</a>
  360. </li>
  361. <li class="class-depth-1">
  362. <a href="Phaser.Utils.html">Utils</a>
  363. </li>
  364. <li class="class-depth-2">
  365. <a href="Phaser.Utils.Debug.html">Debug</a>
  366. </li>
  367. <li class="class-depth-1">
  368. <a href="Phaser.World.html">World</a>
  369. </li>
  370. </ul>
  371. </li>
  372. </ul>
  373. </div>
  374. </div>
  375. <div class="row-fluid">
  376. <div class="span12">
  377. <div id="main">
  378. <h1 class="page-title">Source: math/QuadTree.js</h1>
  379. <section>
  380. <article>
  381. <pre class="sunlight-highlight-javascript linenums">/**
  382. * @author Richard Davey &lt;rich@photonstorm.com>
  383. * @copyright 2014 Photon Storm Ltd.
  384. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
  385. */
  386. /**
  387. * Javascript QuadTree
  388. * @version 1.0
  389. * @author Timo Hausmann
  390. *
  391. * @version 1.3, March 11th 2014
  392. * @author Richard Davey
  393. * The original code was a conversion of the Java code posted to GameDevTuts. However I've tweaked
  394. * it massively to add node indexing, removed lots of temp. var creation and significantly
  395. * increased performance as a result.
  396. *
  397. * Original version at https://github.com/timohausmann/quadtree-js/
  398. */
  399. /**
  400. * @copyright © 2012 Timo Hausmann
  401. *
  402. * Permission is hereby granted, free of charge, to any person obtaining
  403. * a copy of this software and associated documentation files (the
  404. * "Software"), to deal in the Software without restriction, including
  405. * without limitation the rights to use, copy, modify, merge, publish,
  406. * distribute, sublicense, and/or sell copies of the Software, and to
  407. * permit persons to whom the Software is furnished to do so, subject to
  408. * the following conditions:
  409. *
  410. * The above copyright notice and this permission notice shall be
  411. * included in all copies or substantial portions of the Software.
  412. *
  413. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  414. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  415. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  416. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  417. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  418. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  419. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  420. */
  421. /**
  422. * QuadTree Constructor
  423. *
  424. * @class Phaser.QuadTree
  425. * @classdesc A QuadTree implementation. The original code was a conversion of the Java code posted to GameDevTuts.
  426. * However I've tweaked it massively to add node indexing, removed lots of temp. var creation and significantly increased performance as a result.
  427. * Original version at https://github.com/timohausmann/quadtree-js/
  428. * @constructor
  429. * @param {number} x - The top left coordinate of the quadtree.
  430. * @param {number} y - The top left coordinate of the quadtree.
  431. * @param {number} width - The width of the quadtree in pixels.
  432. * @param {number} height - The height of the quadtree in pixels.
  433. * @param {number} [maxObjects=10] - The maximum number of objects per node.
  434. * @param {number} [maxLevels=4] - The maximum number of levels to iterate to.
  435. * @param {number} [level=0] - Which level is this?
  436. */
  437. Phaser.QuadTree = function(x, y, width, height, maxObjects, maxLevels, level) {
  438. /**
  439. * @property {number} maxObjects - The maximum number of objects per node.
  440. * @default
  441. */
  442. this.maxObjects = 10;
  443. /**
  444. * @property {number} maxLevels - The maximum number of levels to break down to.
  445. * @default
  446. */
  447. this.maxLevels = 4;
  448. /**
  449. * @property {number} level - The current level.
  450. */
  451. this.level = 0;
  452. /**
  453. * @property {object} bounds - Object that contains the quadtree bounds.
  454. */
  455. this.bounds = {};
  456. /**
  457. * @property {array} objects - Array of quadtree children.
  458. */
  459. this.objects = [];
  460. /**
  461. * @property {array} nodes - Array of associated child nodes.
  462. */
  463. this.nodes = [];
  464. this.reset(x, y, width, height, maxObjects, maxLevels, level);
  465. };
  466. Phaser.QuadTree.prototype = {
  467. /**
  468. * Resets the QuadTree.
  469. *
  470. * @method Phaser.QuadTree#reset
  471. * @param {number} x - The top left coordinate of the quadtree.
  472. * @param {number} y - The top left coordinate of the quadtree.
  473. * @param {number} width - The width of the quadtree in pixels.
  474. * @param {number} height - The height of the quadtree in pixels.
  475. * @param {number} [maxObjects=10] - The maximum number of objects per node.
  476. * @param {number} [maxLevels=4] - The maximum number of levels to iterate to.
  477. * @param {number} [level=0] - Which level is this?
  478. */
  479. reset: function (x, y, width, height, maxObjects, maxLevels, level) {
  480. this.maxObjects = maxObjects || 10;
  481. this.maxLevels = maxLevels || 4;
  482. this.level = level || 0;
  483. this.bounds = {
  484. x: Math.round(x),
  485. y: Math.round(y),
  486. width: width,
  487. height: height,
  488. subWidth: Math.floor(width / 2),
  489. subHeight: Math.floor(height / 2),
  490. right: Math.round(x) + Math.floor(width / 2),
  491. bottom: Math.round(y) + Math.floor(height / 2)
  492. };
  493. this.objects.length = 0;
  494. this.nodes.length = 0;
  495. },
  496. /**
  497. * Populates this quadtree with the children of the given Group. In order to be added the child must exist and have a body property.
  498. *
  499. * @method Phaser.QuadTree#populate
  500. * @param {Phaser.Group} group - The Group to add to the quadtree.
  501. */
  502. populate: function (group) {
  503. group.forEach(this.populateHandler, this, true);
  504. },
  505. /**
  506. * Handler for the populate method.
  507. *
  508. * @method Phaser.QuadTree#populateHandler
  509. * @param {Phaser.Sprite|object} sprite - The Sprite to check.
  510. */
  511. populateHandler: function (sprite) {
  512. if (sprite.body && sprite.exists)
  513. {
  514. this.insert(sprite.body);
  515. }
  516. },
  517. /**
  518. * Split the node into 4 subnodes
  519. *
  520. * @method Phaser.QuadTree#split
  521. */
  522. split: function () {
  523. this.level++;
  524. // top right node
  525. this.nodes[0] = new Phaser.QuadTree(this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
  526. // top left node
  527. this.nodes[1] = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
  528. // bottom left node
  529. this.nodes[2] = new Phaser.QuadTree(this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
  530. // bottom right node
  531. this.nodes[3] = new Phaser.QuadTree(this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
  532. },
  533. /**
  534. * Insert the object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.
  535. *
  536. * @method Phaser.QuadTree#insert
  537. * @param {Phaser.Physics.Arcade.Body|object} body - The Body object to insert into the quadtree. Can be any object so long as it exposes x, y, right and bottom properties.
  538. */
  539. insert: function (body) {
  540. var i = 0;
  541. var index;
  542. // if we have subnodes ...
  543. if (this.nodes[0] != null)
  544. {
  545. index = this.getIndex(body);
  546. if (index !== -1)
  547. {
  548. this.nodes[index].insert(body);
  549. return;
  550. }
  551. }
  552. this.objects.push(body);
  553. if (this.objects.length > this.maxObjects && this.level &lt; this.maxLevels)
  554. {
  555. // Split if we don't already have subnodes
  556. if (this.nodes[0] == null)
  557. {
  558. this.split();
  559. }
  560. // Add objects to subnodes
  561. while (i &lt; this.objects.length)
  562. {
  563. index = this.getIndex(this.objects[i]);
  564. if (index !== -1)
  565. {
  566. // this is expensive - see what we can do about it
  567. this.nodes[index].insert(this.objects.splice(i, 1)[0]);
  568. }
  569. else
  570. {
  571. i++;
  572. }
  573. }
  574. }
  575. },
  576. /**
  577. * Determine which node the object belongs to.
  578. *
  579. * @method Phaser.QuadTree#getIndex
  580. * @param {Phaser.Rectangle|object} rect - The bounds in which to check.
  581. * @return {number} index - Index of the subnode (0-3), or -1 if rect cannot completely fit within a subnode and is part of the parent node.
  582. */
  583. getIndex: function (rect) {
  584. // default is that rect doesn't fit, i.e. it straddles the internal quadrants
  585. var index = -1;
  586. if (rect.x &lt; this.bounds.right && rect.right &lt; this.bounds.right)
  587. {
  588. if (rect.y &lt; this.bounds.bottom && rect.bottom &lt; this.bounds.bottom)
  589. {
  590. // rect fits within the top-left quadrant of this quadtree
  591. index = 1;
  592. }
  593. else if (rect.y > this.bounds.bottom)
  594. {
  595. // rect fits within the bottom-left quadrant of this quadtree
  596. index = 2;
  597. }
  598. }
  599. else if (rect.x > this.bounds.right)
  600. {
  601. // rect can completely fit within the right quadrants
  602. if (rect.y &lt; this.bounds.bottom && rect.bottom &lt; this.bounds.bottom)
  603. {
  604. // rect fits within the top-right quadrant of this quadtree
  605. index = 0;
  606. }
  607. else if (rect.y > this.bounds.bottom)
  608. {
  609. // rect fits within the bottom-right quadrant of this quadtree
  610. index = 3;
  611. }
  612. }
  613. return index;
  614. },
  615. /**
  616. * Return all objects that could collide with the given Sprite.
  617. *
  618. * @method Phaser.QuadTree#retrieve
  619. * @param {Phaser.Sprite} sprite - The sprite to check against.
  620. * @return {array} - Array with all detected objects.
  621. */
  622. retrieve: function (sprite) {
  623. var returnObjects = this.objects;
  624. // sprite.body.quadTreeIndex = this.getIndex(sprite.body);
  625. var index = this.getIndex(sprite.body);
  626. if (this.nodes[0])
  627. {
  628. // If rect fits into a subnode ..
  629. if (index !== -1)
  630. {
  631. returnObjects = returnObjects.concat(this.nodes[index].retrieve(sprite));
  632. }
  633. else
  634. {
  635. // If rect does not fit into a subnode, check it against all subnodes (unrolled for speed)
  636. returnObjects = returnObjects.concat(this.nodes[0].retrieve(sprite));
  637. returnObjects = returnObjects.concat(this.nodes[1].retrieve(sprite));
  638. returnObjects = returnObjects.concat(this.nodes[2].retrieve(sprite));
  639. returnObjects = returnObjects.concat(this.nodes[3].retrieve(sprite));
  640. }
  641. }
  642. return returnObjects;
  643. },
  644. /**
  645. * Clear the quadtree.
  646. * @method Phaser.QuadTree#clear
  647. */
  648. clear: function () {
  649. this.objects.length = 0;
  650. var i = this.nodes.length;
  651. while (i--)
  652. {
  653. this.nodes[i].clear();
  654. this.nodes.splice(i, 1);
  655. }
  656. this.nodes.length = 0;
  657. }
  658. };
  659. Phaser.QuadTree.prototype.constructor = Phaser.QuadTree;
  660. </pre>
  661. </article>
  662. </section>
  663. </div>
  664. <div class="clearfix"></div>
  665. <footer>
  666. <span class="copyright">
  667. Phaser Copyright © 2012-2014 Photon Storm Ltd.
  668. </span>
  669. <br />
  670. <span class="jsdoc-message">
  671. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
  672. on Fri Apr 11 2014 13:25:03 GMT+0100 (BST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
  673. </span>
  674. </footer>
  675. </div>
  676. <br clear="both">
  677. </div>
  678. </div>
  679. <script src="scripts/sunlight.js"></script>
  680. <script src="scripts/sunlight.javascript.js"></script>
  681. <script src="scripts/sunlight-plugin.doclinks.js"></script>
  682. <script src="scripts/sunlight-plugin.linenumbers.js"></script>
  683. <script src="scripts/sunlight-plugin.menu.js"></script>
  684. <script src="scripts/jquery.min.js"></script>
  685. <script src="scripts/jquery.scrollTo.js"></script>
  686. <script src="scripts/jquery.localScroll.js"></script>
  687. <script src="scripts/bootstrap-dropdown.js"></script>
  688. <script src="scripts/toc.js"></script>
  689. <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
  690. <script>
  691. $( function () {
  692. $( "#toc" ).toc( {
  693. anchorName : function(i, heading, prefix) {
  694. return $(heading).attr("id") || ( prefix + i );
  695. },
  696. selectors : "h1,h2,h3,h4",
  697. showAndHide : false,
  698. scrollTo : 60
  699. } );
  700. $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
  701. $( "#main span[id^='toc']" ).addClass( "toc-shim" );
  702. } );
  703. </script>
  704. </body>
  705. </html>