App.java 6.2 KB
Newer Older
1
/*
2
 * The MIT License
I
Ilkka Seppälä 已提交
3
 * Copyright © 2014-2019 Ilkka Seppälä
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
23

24 25
package com.iluwatar.spatialpartition;

S
Subhrodip Mohanta 已提交
26
import java.util.HashMap;
27
import java.util.Random;
28 29
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
30 31

/**
32 33 34 35 36 37 38 39
 * <p>The idea behind the <b>Spatial Partition</b> design pattern is to enable efficient location
 * of objects by storing them in a data structure that is organised by their positions. This is
 * especially useful in the gaming world, where one may need to look up all the objects within a
 * certain boundary, or near a certain other object, repeatedly. The data structure can be used to
 * store moving and static objects, though in order to keep track of the moving objects, their
 * positions will have to be reset each time they move. This would mean having to create a new
 * instance of the data structure each frame, which would use up additional memory, and so this
 * pattern should only be used if one does not mind trading memory for speed and the number of
40
 * objects to keep track of is large to justify the use of the extra space.</p>
41 42 43 44 45 46
 * <p>In our example, we use <b>{@link QuadTree} data structure</b> which divides into 4 (quad)
 * sub-sections when the number of objects added to it exceeds a certain number (int field
 * capacity). There is also a
 * <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class
 * <b>{@link Point}</b>
 * with x and y coordinate fields and also an id field so that it can easily be put and looked up in
S
Subhrodip Mohanta 已提交
47
 * the hashmap. This class has abstract methods to define how the object moves (move()), when to
48 49 50
 * check for collision with any object (touches(obj)) and how to handle collision
 * (handleCollision(obj)), and will be extended by any object whose position has to be kept track of
 * in the quadtree. The <b>{@link SpatialPartitionGeneric}</b> abstract class has 2 fields - a
S
Subhrodip Mohanta 已提交
51
 * hashmap containing all objects (we use hashmap for faster lookups, insertion and deletion)
52 53 54 55 56 57
 * and a quadtree, and contains an abstract method which defines how to handle interactions between
 * objects using the quadtree.</p>
 * <p>Using the quadtree data structure will reduce the time complexity of finding the objects
 * within a certain range from <b>O(n^2) to O(nlogn)</b>, increasing the speed of computations
 * immensely in case of large number of objects, which will have a positive effect on the rendering
 * speed of the game.</p>
58 59 60
 */

public class App {
61
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
62
  private static final String BUBBLE = "Bubble ";
63

S
Subhrodip Mohanta 已提交
64
  static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> bubbles) {
65 66
    //all bubbles have to be checked for collision for all bubbles
    var bubblesToCheck = bubbles.values();
67

68
    //will run numOfMovement times or till all bubbles have popped
69
    while (numOfMovements > 0 && !bubbles.isEmpty()) {
70
      bubbles.forEach((i, bubble) -> {
71 72
        // bubble moves, new position gets updated
        // and collisions are checked with all bubbles in bubblesToCheck
73 74 75 76
        bubble.move();
        bubbles.replace(i, bubble);
        bubble.handleCollision(bubblesToCheck, bubbles);
      });
77 78
      numOfMovements--;
    }
79
    //bubbles not popped
80
    bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
81 82
  }

83
  static void withSpatialPartition(
S
Subhrodip Mohanta 已提交
84
      int height, int width, int numOfMovements, HashMap<Integer, Bubble> bubbles) {
85
    //creating quadtree
S
Subhrodip Mohanta 已提交
86
    var rect = new Rect(width / 2D, height / 2D, width, height);
87
    var quadTree = new QuadTree(rect, 4);
88

89 90 91
    //will run numOfMovement times or till all bubbles have popped
    while (numOfMovements > 0 && !bubbles.isEmpty()) {
      //quadtree updated each time
92 93
      bubbles.values().forEach(quadTree::insert);
      bubbles.forEach((i, bubble) -> {
94
        //bubble moves, new position gets updated, quadtree used to reduce computations
95 96 97 98 99
        bubble.move();
        bubbles.replace(i, bubble);
        var sp = new SpatialPartitionBubbles(bubbles, quadTree);
        sp.handleCollisionsUsingQt(bubble);
      });
100 101
      numOfMovements--;
    }
102
    //bubbles not popped
103
    bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
104
  }
105

106 107 108 109 110 111 112
  /**
   * Program entry point.
   *
   * @param args command line args
   */

  public static void main(String[] args) {
S
Subhrodip Mohanta 已提交
113 114
    var bubbles1 = new HashMap<Integer, Bubble>();
    var bubbles2 = new HashMap<Integer, Bubble>();
115
    var rand = new Random();
116
    for (int i = 0; i < 10000; i++) {
117
      var b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1);
118 119
      bubbles1.put(i, b);
      bubbles2.put(i, b);
S
Subhrodip Mohanta 已提交
120 121
      LOGGER.info(BUBBLE, i, " with radius ", b.radius,
          " added at (", b.coordinateX, ",", b.coordinateY + ")");
122 123
    }

124 125 126 127
    var start1 = System.currentTimeMillis();
    App.noSpatialPartition(20, bubbles1);
    var end1 = System.currentTimeMillis();
    var start2 = System.currentTimeMillis();
128
    App.withSpatialPartition(300, 300, 20, bubbles2);
129
    var end2 = System.currentTimeMillis();
S
Subhrodip Mohanta 已提交
130 131
    LOGGER.info("Without spatial partition takes ", (end1 - start1), "ms");
    LOGGER.info("With spatial partition takes ", (end2 - start2), "ms");
132 133 134
  }
}