Skip to content

Commit bec30be

Browse files
authored
Check if position is in bound of the grid (#717)
* Check position is in bound of the grid * Chage signature of function
1 parent 2e0985c commit bec30be

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

chunky/src/java/se/llbit/math/Grid.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ public EmitterPosition sampleEmitterPosition(int x, int y, int z, Random random)
129129
int gridX = x / cellSize;
130130
int gridY = y / cellSize;
131131
int gridZ = z / cellSize;
132+
133+
if(isOutOfBounds(gridX, gridY, gridZ))
134+
return null;
135+
132136
int index = cellIndex(gridX, gridY, gridZ);
133137

134138
int start = constructedGrid[2*index];
@@ -141,17 +145,28 @@ public EmitterPosition sampleEmitterPosition(int x, int y, int z, Random random)
141145
return emitterPositions.get(emitterIndex);
142146
}
143147

148+
private boolean isOutOfBounds(int x, int y, int z)
149+
{
150+
return x < 0 || x >= gridSize
151+
|| y < 0 || y >= gridSize
152+
|| z < 0 || z >= gridSize;
153+
}
154+
144155
/**
145156
* Get the list of emitters position close from a given point
146157
*/
147158
public List<EmitterPosition> getEmitterPositions(int x, int y, int z) {
148159
int gridX = x / cellSize;
149160
int gridY = y / cellSize;
150161
int gridZ = z / cellSize;
162+
163+
List<EmitterPosition> pos = new ArrayList<>();
164+
if(isOutOfBounds(gridX, gridY, gridZ))
165+
return pos;
166+
151167
int index = cellIndex(gridX, gridY, gridZ);
152168
int start = constructedGrid[2*index];
153169
int size = constructedGrid[2*index+1];
154-
List<EmitterPosition> pos = new ArrayList<>();
155170
for(int i = 0; i < size; ++i) {
156171
pos.add(emitterPositions.get(positionIndexes[start+i]));
157172
}

0 commit comments

Comments
 (0)