Skip to content

Commit 5f636eb

Browse files
committed
Added Vector3Utils.ceil. Finished getBlockVolume tests but need to confirm expected results of different scenarios with design.
1 parent 11ad062 commit 5f636eb

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,31 @@ describe('AABB operations', () => {
155155
});
156156

157157
describe('AABB BlockVolume operations', () => {
158-
it('successfully creates a BlockVolume containing a valid AABB', () => {
158+
it('successfully creates a BlockVolume when AABB extents are VECTOR3_ONE', () => {
159159
const aabb: AABB = { center: VECTOR3_ZERO, extents: VECTOR3_ONE };
160160
const blockVolume = AABBUtils.getBlockVolume(aabb);
161161
expect(Vector3Utils.equals(blockVolume.from, { x: -1.0, y: -1.0, z: -1.0 })).toBe(true);
162162
expect(Vector3Utils.equals(blockVolume.to, { x: 1.0, y: 1.0, z: 1.0 })).toBe(true);
163163
});
164+
165+
it('successfully creates a BlockVolume when AABB extents are close but less than VECTOR3_ONE', () => {
166+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.99999, y: 0.99999, z: 0.99999 } };
167+
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);
170+
});
171+
172+
it('successfully creates a BlockVolume when AABB extents are close but greater than VECTOR3_ZERO', () => {
173+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 0.00001, y: 0.00001, z: 0.00001 } };
174+
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);
177+
});
178+
179+
it('successfully creates a BlockVolume when AABB extents are close but greater than VECTOR3_ONE', () => {
180+
const aabb: AABB = { center: VECTOR3_ZERO, extents: { x: 1.00001, y: 1.00001, z: 1.00001 } };
181+
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);
184+
});
164185
});

libraries/math/src/aabb/coreHelpers.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,9 @@ export class AABBUtils {
111111
* @returns - The BlockVolume containing the source AABB.
112112
*/
113113
static getBlockVolume(aabb: AABB): BlockVolume {
114-
const from = this.getMin(aabb);
115-
from.x = Math.floor(from.x);
116-
from.y = Math.floor(from.y);
117-
from.z = Math.floor(from.z);
118-
119-
// Could be added to Vector3Utils
120-
const to = this.getMax(aabb);
121-
to.x = Math.floor(to.x);
122-
to.y = Math.floor(to.y);
123-
to.z = Math.floor(to.z);
124-
114+
const from = Vector3Utils.floor(this.getMin(aabb));
115+
const to = Vector3Utils.ceil(this.getMax(aabb));
125116
return new BlockVolume(from, to);
126-
127-
/*
128-
GetBlockVolume(box: AABB): BlockVolumeBase
129-
We get the smallest block volume that includes all of the bounding box
130-
The collision code uses (floor(min - 1), floor(max + 1), we should probably continue using this calculation
131-
We could also use (floor(min), ceil(max))
132-
There might be some floating point errors like <1.0, 1.0>, <4.000001, 4.00001> and <1.0, 1.0>, <3.99999, 3.99999> returning different Block Volumes
133-
Perhaps handle that specific edge case by doing a nearEqual comparison (0.00001 diff)
134-
ToBeCloseTo check this out later as a reference of what already exists for checking if a float is close to a target number --> (vitest/packages/expect/src/jest-expect.ts at 34f67546df3848c66bcdfa48f5221717d498b965 · vitest-dev/vitest · GitHub), and use math.round()
135-
*/
136117
}
137118

138119
/**

libraries/math/src/vector3/coreHelpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ export class Vector3Utils {
100100
return { x: Math.floor(v.x), y: Math.floor(v.y), z: Math.floor(v.z) };
101101
}
102102

103+
/**
104+
* ceil
105+
*
106+
* Ceil the components of a vector to produce a new vector
107+
*/
108+
static ceil(v: Vector3): Vector3 {
109+
return { x: Math.ceil(v.x), y: Math.ceil(v.y), z: Math.ceil(v.z) };
110+
}
111+
103112
/**
104113
* toString
105114
*

0 commit comments

Comments
 (0)