Skip to content

Commit 586ce9d

Browse files
committed
Made getBlockVolume have an epsilon tolerance
1 parent 5f636eb commit 586ce9d

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

libraries/math/src/aabb/coreHelpers.test.ts

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import type { Vector3 } from '@minecraft/server';
99
import { VECTOR3_FORWARD, VECTOR3_ONE, VECTOR3_ZERO, Vector3Utils } from '../vector3/coreHelpers.js';
1010
import { AABB, AABBUtils } from './coreHelpers.js';
1111

12+
function expectVector3(expected: Vector3, actual: Vector3) {
13+
return expect(
14+
Vector3Utils.equals(expected, actual),
15+
`Expected ${Vector3Utils.toString(expected)} but actual is ${Vector3Utils.toString(actual)}`
16+
);
17+
}
18+
1219
describe('AABB factories', () => {
1320
it('successfully reports invalid AABB when created from identical corner points', () => {
1421
const aabb = AABBUtils.createFromCornerPoints(VECTOR3_ONE, VECTOR3_ONE);
@@ -158,28 +165,70 @@ describe('AABB BlockVolume operations', () => {
158165
it('successfully creates a BlockVolume when AABB extents are VECTOR3_ONE', () => {
159166
const aabb: AABB = { center: VECTOR3_ZERO, extents: VECTOR3_ONE };
160167
const blockVolume = AABBUtils.getBlockVolume(aabb);
161-
expect(Vector3Utils.equals(blockVolume.from, { x: -1.0, y: -1.0, z: -1.0 })).toBe(true);
162-
expect(Vector3Utils.equals(blockVolume.to, { x: 1.0, y: 1.0, z: 1.0 })).toBe(true);
168+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
169+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
170+
});
171+
172+
it('successfully creates a BlockVolume when AABB extents coords are 0.5', () => {
173+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.5, y: 0.5, z: 0.5 } };
174+
const blockVolume = AABBUtils.getBlockVolume(aabb);
175+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
176+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
177+
});
178+
179+
it('successfully creates a BlockVolume when AABB center and extent coords are 0.5', () => {
180+
const aabb: AABB = { center: { x: 0.5, y: 0.5, z: 0.5 }, extents: { x: 0.5, y: 0.5, z: 0.5 } };
181+
const blockVolume = AABBUtils.getBlockVolume(aabb);
182+
expectVector3({ x: 0.0, y: 0.0, z: 0.0 }, blockVolume.from).toBe(true);
183+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
184+
});
185+
186+
it('successfully creates a BlockVolume when AABB center coords are -0.5 and extent coords are 0.5', () => {
187+
const aabb: AABB = { center: { x: -0.5, y: -0.5, z: -0.5 }, extents: { x: 0.5, y: 0.5, z: 0.5 } };
188+
const blockVolume = AABBUtils.getBlockVolume(aabb);
189+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
190+
expectVector3({ x: 0.0, y: 0.0, z: 0.0 }, blockVolume.to).toBe(true);
163191
});
164192

165-
it('successfully creates a BlockVolume when AABB extents are close but less than VECTOR3_ONE', () => {
193+
it('successfully creates a BlockVolume when AABB extents are less than VECTOR3_ONE within epsilon', () => {
166194
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.99999, y: 0.99999, z: 0.99999 } };
167195
const blockVolume = AABBUtils.getBlockVolume(aabb);
168-
expect(Vector3Utils.equals(blockVolume.from, { x: -1.0, y: -1.0, z: -1.0 })).toBe(true);
169-
expect(Vector3Utils.equals(blockVolume.to, { x: 1.0, y: 1.0, z: 1.0 })).toBe(true);
196+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
197+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
170198
});
171199

172-
it('successfully creates a BlockVolume when AABB extents are close but greater than VECTOR3_ZERO', () => {
200+
it('successfully creates a BlockVolume when AABB extents are less than VECTOR3_ONE exceeding epsilon', () => {
201+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.99998, y: 0.99998, z: 0.99998 } };
202+
const blockVolume = AABBUtils.getBlockVolume(aabb);
203+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
204+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
205+
});
206+
207+
it('successfully creates a BlockVolume when AABB extents are greater than VECTOR3_ZERO within epsilon', () => {
173208
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.00001, y: 0.00001, z: 0.00001 } };
174209
const blockVolume = AABBUtils.getBlockVolume(aabb);
175-
expect(Vector3Utils.equals(blockVolume.from, { x: -1.0, y: -1.0, z: -1.0 })).toBe(true);
176-
expect(Vector3Utils.equals(blockVolume.to, { x: 1.0, y: 1.0, z: 1.0 })).toBe(true);
210+
expectVector3({ x: 0.0, y: 0.0, z: 0.0 }, blockVolume.from).toBe(true);
211+
expectVector3({ x: 0.0, y: 0.0, z: 0.0 }, blockVolume.to).toBe(true);
177212
});
178213

179-
it('successfully creates a BlockVolume when AABB extents are close but greater than VECTOR3_ONE', () => {
214+
it('successfully creates a BlockVolume when AABB extents are greater than VECTOR3_ZERO exceeding epsilon', () => {
215+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.00002, y: 0.00002, z: 0.00002 } };
216+
const blockVolume = AABBUtils.getBlockVolume(aabb);
217+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
218+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
219+
});
220+
221+
it('successfully creates a BlockVolume when AABB extents are greater than VECTOR3_ONE within epsilon', () => {
180222
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 1.00001, y: 1.00001, z: 1.00001 } };
181223
const blockVolume = AABBUtils.getBlockVolume(aabb);
182-
expect(Vector3Utils.equals(blockVolume.from, { x: -2.0, y: -2.0, z: -2.0 })).toBe(true);
183-
expect(Vector3Utils.equals(blockVolume.to, { x: 2.0, y: 2.0, z: 2.0 })).toBe(true);
224+
expectVector3({ x: -1.0, y: -1.0, z: -1.0 }, blockVolume.from).toBe(true);
225+
expectVector3({ x: 1.0, y: 1.0, z: 1.0 }, blockVolume.to).toBe(true);
226+
});
227+
228+
it('successfully creates a BlockVolume when AABB extents are greater than VECTOR3_ONE exceeding epsilon', () => {
229+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 1.00002, y: 1.00002, z: 1.00002 } };
230+
const blockVolume = AABBUtils.getBlockVolume(aabb);
231+
expectVector3({ x: -2.0, y: -2.0, z: -2.0 }, blockVolume.from).toBe(true);
232+
expectVector3({ x: 2.0, y: 2.0, z: 2.0 }, blockVolume.to).toBe(true);
184233
});
185234
});

libraries/math/src/aabb/coreHelpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ export class AABBUtils {
111111
* @returns - The BlockVolume containing the source AABB.
112112
*/
113113
static getBlockVolume(aabb: AABB): BlockVolume {
114-
const from = Vector3Utils.floor(this.getMin(aabb));
115-
const to = Vector3Utils.ceil(this.getMax(aabb));
114+
const epsilon = 0.00001;
115+
const epsilonVec: Vector3 = { x: epsilon, y: epsilon, z: epsilon };
116+
const from = Vector3Utils.floor(Vector3Utils.add(this.getMin(aabb), epsilonVec));
117+
const to = Vector3Utils.ceil(Vector3Utils.subtract(this.getMax(aabb), epsilonVec));
116118
return new BlockVolume(from, to);
117119
}
118120

0 commit comments

Comments
 (0)