|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.spatialpartition; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Enumeration; |
| 28 | +import java.util.Hashtable; |
| 29 | +import java.util.Random; |
| 30 | + |
| 31 | +/** |
| 32 | + * <p>The idea behind the <b>Spatial Partition</b> design pattern is to enable efficient location of objects |
| 33 | + * by storing them in a data structure that is organised by their positions. This is especially useful in the |
| 34 | + * gaming world, where one may need to look up all the objects within a certain boundary, or near a certain |
| 35 | + * other object, repeatedly. The data structure can be used to store moving and static objects, though in order |
| 36 | + * to keep track of the moving objects, their positions will have to be reset each time they move. This would |
| 37 | + * mean having to create a new instance of the data structure each frame, which would use up additional memory, |
| 38 | + * and so this pattern should only be used if one does not mind trading memory for speed and the number of |
| 39 | + * objects to keep track of is large to justify the use of the extra space.</p> |
| 40 | + * <p>In our example, we use <b>{@link QuadTree} data structure</b> which divides into 4 (quad) sub-sections when |
| 41 | + * the number of objects added to it exceeds a certain number (int field capacity). There is also a |
| 42 | + * <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class <b>{@link Point}</b> |
| 43 | + * with x and y coordinate fields and also an id field so that it can easily be put and looked up in the hashtable. |
| 44 | + * This class has abstract methods to define how the object moves (move()), when to check for collision with any |
| 45 | + * object (touches(obj)) and how to handle collision (handleCollision(obj)), and will be extended by any object |
| 46 | + * whose position has to be kept track of in the quadtree. The <b>{@link SpatialPartitionGeneric}</b> abstract class |
| 47 | + * has 2 fields - a hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion) |
| 48 | + * and a quadtree, and contains an abstract method which defines how to handle interactions between objects using |
| 49 | + * the quadtree.</p> |
| 50 | + * <p>Using the quadtree data structure will reduce the time complexity of finding the objects within a |
| 51 | + * certain range from <b>O(n^2) to O(nlogn)</b>, increasing the speed of computations immensely in case of |
| 52 | + * large number of objects, which will have a positive effect on the rendering speed of the game.</p> |
| 53 | + */ |
| 54 | + |
| 55 | +public class App { |
| 56 | + |
| 57 | + static void noSpatialPartition(int height, int width, |
| 58 | + int numOfMovements, Hashtable<Integer, Bubble> bubbles) { |
| 59 | + ArrayList<Point> bubblesToCheck = new ArrayList<Point>(); |
| 60 | + for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |
| 61 | + bubblesToCheck.add(bubbles.get(e.nextElement())); //all bubbles have to be checked for collision for all bubbles |
| 62 | + } |
| 63 | + |
| 64 | + //will run numOfMovement times or till all bubbles have popped |
| 65 | + while (numOfMovements > 0 && !bubbles.isEmpty()) { |
| 66 | + for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |
| 67 | + Integer i = e.nextElement(); |
| 68 | + //bubble moves, new position gets updated, collisions checked with all bubbles in bubblesToCheck |
| 69 | + bubbles.get(i).move(); |
| 70 | + bubbles.replace(i, bubbles.get(i)); |
| 71 | + bubbles.get(i).handleCollision(bubblesToCheck, bubbles); |
| 72 | + } |
| 73 | + numOfMovements--; |
| 74 | + } |
| 75 | + for (Integer key : bubbles.keySet()) { |
| 76 | + //bubbles not popped |
| 77 | + System.out.println("Bubble " + key + " not popped"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static void withSpatialPartition(int height, int width, |
| 82 | + int numOfMovements, Hashtable<Integer, Bubble> bubbles) { |
| 83 | + //creating quadtree |
| 84 | + Rect rect = new Rect(width / 2,height / 2,width,height); |
| 85 | + QuadTree qTree = new QuadTree(rect, 4); |
| 86 | + |
| 87 | + //will run numOfMovement times or till all bubbles have popped |
| 88 | + while (numOfMovements > 0 && !bubbles.isEmpty()) { |
| 89 | + //quadtree updated each time |
| 90 | + for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |
| 91 | + qTree.insert(bubbles.get(e.nextElement())); |
| 92 | + } |
| 93 | + for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |
| 94 | + Integer i = e.nextElement(); |
| 95 | + //bubble moves, new position gets updated, quadtree used to reduce computations |
| 96 | + bubbles.get(i).move(); |
| 97 | + bubbles.replace(i, bubbles.get(i)); |
| 98 | + SpatialPartitionBubbles sp = new SpatialPartitionBubbles(bubbles, qTree); |
| 99 | + sp.handleCollisionsUsingQt(bubbles.get(i)); |
| 100 | + } |
| 101 | + numOfMovements--; |
| 102 | + } |
| 103 | + for (Integer key : bubbles.keySet()) { |
| 104 | + //bubbles not popped |
| 105 | + System.out.println("Bubble " + key + " not popped"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Program entry point. |
| 111 | + * |
| 112 | + * @param args command line args |
| 113 | + */ |
| 114 | + |
| 115 | + public static void main(String[] args) { |
| 116 | + Hashtable<Integer, Bubble> bubbles1 = new Hashtable<Integer, Bubble>(); |
| 117 | + Hashtable<Integer, Bubble> bubbles2 = new Hashtable<Integer, Bubble>(); |
| 118 | + Random rand = new Random(); |
| 119 | + for (int i = 0; i < 10000; i++) { |
| 120 | + Bubble b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1); |
| 121 | + bubbles1.put(i, b); |
| 122 | + bubbles2.put(i, b); |
| 123 | + System.out.println("Bubble " + i + " with radius " + b.radius + " added at (" + b.x + "," + b.y + ")"); |
| 124 | + } |
| 125 | + |
| 126 | + long start1 = System.currentTimeMillis(); |
| 127 | + App.noSpatialPartition(300,300,20,bubbles1); |
| 128 | + long end1 = System.currentTimeMillis(); |
| 129 | + long start2 = System.currentTimeMillis(); |
| 130 | + App.withSpatialPartition(300,300,20,bubbles2); |
| 131 | + long end2 = System.currentTimeMillis(); |
| 132 | + System.out.println("Without spatial partition takes " + (end1 - start1) + "ms"); |
| 133 | + System.out.println("With spatial partition takes " + (end2 - start2) + "ms"); |
| 134 | + } |
| 135 | +} |
| 136 | + |
0 commit comments