Skip to content

Commit fb735a2

Browse files
committed
Added a method for getting the grid dimensions. Fixed the
GridCell containment to be exclusive on the upper bounds so that an object on the line isn't in two cells at once. (oops)
1 parent fb143c9 commit fb735a2

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/main/java/com/simsilica/mathd/Grid.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
public class Grid {
5151

5252
private final Vec3i gridSpacing;
53+
private final int dimensions;
5354

5455
private final int shift;
5556
private final long mask;
@@ -97,6 +98,7 @@ public Grid( Vec3i gridSpacing ) {
9798
if( gridSpacing.z != 0 ) {
9899
axes++;
99100
}
101+
this.dimensions = axes;
100102
int bits = 64 / axes;
101103
shift = bits;
102104

@@ -122,6 +124,10 @@ public Grid( Vec3i gridSpacing ) {
122124
public final Vec3i getSpacing() {
123125
return gridSpacing;
124126
}
127+
128+
public final int getDimensions() {
129+
return dimensions;
130+
}
125131

126132
private int worldToCell( int i, int size ) {
127133
if( size == 0 ) {

src/main/java/com/simsilica/mathd/GridCell.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,28 @@ public final Vec3i getWorldOrigin() {
7272
public final boolean contains( Vec3d world ) {
7373
return contains(world.x, world.y, world.z);
7474
}
75-
75+
76+
/**
77+
* Returns true if this GridCell contains the specified world location
78+
* where containment is in the range of min inclusive, max exclusive.
79+
*/
7680
public final boolean contains( double x, double y, double z ) {
7781
Vec3i spacing = grid.getSpacing();
7882
double xLocal = x - worldOrigin.x;
7983
double yLocal = y - worldOrigin.y;
8084
double zLocal = z - worldOrigin.z;
8185
if( spacing.x != 0 ) {
82-
if( xLocal < 0 || xLocal > spacing.x ) {
86+
if( xLocal < 0 || xLocal >= spacing.x ) {
8387
return false;
8488
}
8589
}
8690
if( spacing.y != 0 ) {
87-
if( yLocal < 0 || yLocal > spacing.y ) {
91+
if( yLocal < 0 || yLocal >= spacing.y ) {
8892
return false;
8993
}
9094
}
9195
if( spacing.z != 0 ) {
92-
if( zLocal < 0 || zLocal > spacing.z ) {
96+
if( zLocal < 0 || zLocal >= spacing.z ) {
9397
return false;
9498
}
9599
}

0 commit comments

Comments
 (0)