Starsector API
Loading...
Searching...
No Matches
CollisionGridUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2import java.util.ArrayList;
3import java.util.Iterator;
4import java.util.LinkedHashSet;
5import java.util.List;
6import java.util.Set;
7
8import org.lwjgl.util.vector.Vector2f;
9
10import com.fs.starfarer.api.combat.CollisionGridAPI;
11
12
13public class CollisionGridUtil implements CollisionGridAPI {
14
15 protected class BucketIterator implements Iterator<Object> {
16 protected Iterator<Object> curr = null;
18 protected BucketIterator() {
19 }
20 protected BucketIterator(int startX, int endX, int startY, int endY) {
22 if (startX < 0) startX = 0;
23 if (endX >= width) endX = width - 1;
24 if (startY < 0) startY = 0;
25 if (endY >= height) endY = height - 1;
26
27 for (int i = startX; i <= endX; i++) {
28 for (int j = startY; j <= endY; j++) {
29 if (buckets[i][j] == null) continue;
30 objects.addAll(buckets[i][j]);
31 }
32 }
33 curr = objects.iterator();
34 }
37 copy.objects = objects;
38 copy.curr = copy.objects.iterator();
39 return copy;
40 }
41 public boolean hasNext() {
42 return curr.hasNext();
43 }
44 public Object next() {
45 return curr.next();
46 }
47 public void remove() {
49 }
50 }
51
52
53 protected float cellSize;
54 protected List<Object>[][] buckets;
55
56 protected int width, height, leftOf, rightOf, below, above;
57
58 @SuppressWarnings("unchecked")
59 public CollisionGridUtil(float minX, float maxX, float minY, float maxY, float cellSize) {
60 this.cellSize = cellSize;
61
62 leftOf = -(int) Math.floor(minX / cellSize);
63 rightOf = (int) Math.ceil(maxX / cellSize);
64 below = -(int) Math.floor(minY / cellSize);
65 above = (int) Math.ceil(maxY / cellSize);
66
68 height = below + above;
69 buckets = new List[width][height];
70 }
71
72 public void addObject(Object object, Vector2f loc, float objWidth, float objHeight) {
73 int startX = (int) (leftOf + ((loc.x - objWidth/2f) / cellSize));
74 int endX = (int) (leftOf + (loc.x + objWidth/2f) / cellSize);
75
76 int startY = (int) (below + ((loc.y - objHeight/2f) / cellSize));
77 int endY = (int) (below + (loc.y + objHeight/2f) / cellSize);
78
79 int limit = Integer.MAX_VALUE / 2;
80 if (startX > limit) startX = limit;
81 if (endX > limit) endX = limit;
82 if (startY > limit) startY = limit;
83 if (endY > limit) endY = limit;
84
85 if (startX < -limit) startX = -limit;
86 if (endX < -limit) endX = -limit;
87 if (startY < -limit) startY = -limit;
88 if (endY < -limit) endY = -limit;
89
90 for (int i = startX; i <= endX; i++) {
91 for (int j = startY; j <= endY; j++) {
92 addToBucket(i, j, object);
93 }
94 }
95 }
96
97 public void removeObject(Object object, Vector2f loc, float objWidth, float objHeight) {
98 int startX = (int) (leftOf + ((loc.x - objWidth/2f) / cellSize));
99 int endX = (int) (leftOf + (loc.x + objWidth/2f) / cellSize);
100
101 int startY = (int) (below + ((loc.y - objHeight/2f) / cellSize));
102 int endY = (int) (below + (loc.y + objHeight/2f) / cellSize);
103 for (int i = startX; i <= endX; i++) {
104 for (int j = startY; j <= endY; j++) {
105 removeFromBucket(i, j, object);
106 }
107 }
108 }
109
110 protected void addToBucket(int cellX, int cellY, Object object) {
112 if(buckets[cellX][cellY] == null) {
114 }
115 if (!buckets[cellX][cellY].contains(object)) {
116 buckets[cellX][cellY].add(object);
117 }
118 }
119
120 protected void removeFromBucket(int cellX, int cellY, Object object) {
122 if(buckets[cellX][cellY] == null) return;
123 buckets[cellX][cellY].remove(object);
124 }
125
127 int startX = (int) (leftOf + ((loc.x - objWidth/2f) / cellSize));
128 //int endX = (int) (leftOf + Math.ceil((loc.x + objWidth/2f) / cellSize));
129 int endX = (int) (leftOf + (loc.x + objWidth/2f) / cellSize);
130
131 int startY = (int) (below + ((loc.y - objHeight/2f) / cellSize));
132 //int endY = (int) (below + Math.ceil((loc.y + objHeight/2f) / cellSize));
133 int endY = (int) (below + (loc.y + objHeight/2f) / cellSize);
134
136 return result;
137 }
138
139
140}
BucketIterator(int startX, int endX, int startY, int endY)
Iterator< Object > getCheckIterator(Vector2f loc, float objWidth, float objHeight)
void removeFromBucket(int cellX, int cellY, Object object)
void addToBucket(int cellX, int cellY, Object object)
void addObject(Object object, Vector2f loc, float objWidth, float objHeight)
void removeObject(Object object, Vector2f loc, float objWidth, float objHeight)