Skip to content

Commit 0209c13

Browse files
authored
feat: Updated Vector3 add and subtract to support partial right hand values (#59)
* Change files * feat: Updated Vector3 add and subtract to support partial right hand values
1 parent ebf9e8a commit 0209c13

6 files changed

+83
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Updated Vector3 add and subtract to support partial right hand values",
4+
"packageName": "@minecraft/math",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

libraries/math/api-report/math.api.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const VECTOR3_ZERO: Vector3;
7979
export class Vector3Builder implements Vector3 {
8080
constructor(vec: Vector3, arg?: never, arg2?: never);
8181
constructor(x: number, y: number, z: number);
82-
add(v: Vector3): this;
82+
add(v: Partial<Vector3>): this;
8383
assign(vec: Vector3): this;
8484
clamp(limits: {
8585
min?: Partial<Vector3>;
@@ -99,7 +99,7 @@ export class Vector3Builder implements Vector3 {
9999
rotateZ(a: number): this;
100100
scale(val: number): this;
101101
slerp(vec: Vector3, t: number): this;
102-
subtract(v: Vector3): this;
102+
subtract(v: Partial<Vector3>): this;
103103
toString(options?: {
104104
decimals?: number;
105105
delimiter?: string;
@@ -114,7 +114,7 @@ export class Vector3Builder implements Vector3 {
114114

115115
// @public
116116
export class Vector3Utils {
117-
static add(v1: Vector3, v2: Vector3): Vector3;
117+
static add(v1: Vector3, v2: Partial<Vector3>): Vector3;
118118
static clamp(v: Vector3, limits?: {
119119
min?: Partial<Vector3>;
120120
max?: Partial<Vector3>;
@@ -133,7 +133,7 @@ export class Vector3Utils {
133133
static rotateZ(v: Vector3, a: number): Vector3;
134134
static scale(v1: Vector3, scale: number): Vector3;
135135
static slerp(a: Vector3, b: Vector3, t: number): Vector3;
136-
static subtract(v1: Vector3, v2: Vector3): Vector3;
136+
static subtract(v1: Vector3, v2: Partial<Vector3>): Vector3;
137137
static toString(v: Vector3, options?: {
138138
decimals?: number;
139139
delimiter?: string;

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

+46
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,50 @@ describe('Vector3 operations', () => {
217217
expect(result.z).toBeCloseTo(0);
218218
});
219219
});
220+
221+
describe('Vector3 add partial', () => {
222+
it(`add a single axis`, () => {
223+
const result = Vector3Utils.add(VECTOR3_UP, { z: 4 });
224+
expect(result.x).toBeCloseTo(0);
225+
expect(result.y).toBeCloseTo(1);
226+
expect(result.z).toBeCloseTo(4);
227+
});
228+
229+
it(`add two axis`, () => {
230+
const result = Vector3Utils.add(VECTOR3_UP, { z: 7, x: 2 });
231+
expect(result.x).toBeCloseTo(2);
232+
expect(result.y).toBeCloseTo(1);
233+
expect(result.z).toBeCloseTo(7);
234+
});
235+
236+
it(`add all three axis`, () => {
237+
const result = Vector3Utils.add(VECTOR3_UP, { x: 8, y: 2, z: 3 });
238+
expect(result.x).toBeCloseTo(8);
239+
expect(result.y).toBeCloseTo(3);
240+
expect(result.z).toBeCloseTo(3);
241+
});
242+
});
243+
244+
describe('Vector3 subtract partial', () => {
245+
it(`subtract a single axis`, () => {
246+
const result = Vector3Utils.subtract(VECTOR3_UP, { z: 4 });
247+
expect(result.x).toBeCloseTo(0);
248+
expect(result.y).toBeCloseTo(1);
249+
expect(result.z).toBeCloseTo(-4);
250+
});
251+
252+
it(`subtract two axis`, () => {
253+
const result = Vector3Utils.subtract(VECTOR3_UP, { z: 7, x: 2 });
254+
expect(result.x).toBeCloseTo(-2);
255+
expect(result.y).toBeCloseTo(1);
256+
expect(result.z).toBeCloseTo(-7);
257+
});
258+
259+
it(`subtract all three axis`, () => {
260+
const result = Vector3Utils.subtract(VECTOR3_UP, { x: 8, y: 2, z: 3 });
261+
expect(result.x).toBeCloseTo(-8);
262+
expect(result.y).toBeCloseTo(-1);
263+
expect(result.z).toBeCloseTo(-3);
264+
});
265+
});
220266
});

libraries/math/src/vector3/coreHelpers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ export class Vector3Utils {
2424
*
2525
* Add two vectors to produce a new vector
2626
*/
27-
static add(v1: Vector3, v2: Vector3): Vector3 {
28-
return { x: v1.x + v2.x, y: v1.y + v2.y, z: v1.z + v2.z };
27+
static add(v1: Vector3, v2: Partial<Vector3>): Vector3 {
28+
return { x: v1.x + (v2.x ?? 0), y: v1.y + (v2.y ?? 0), z: v1.z + (v2.z ?? 0) };
2929
}
3030

3131
/**
3232
* subtract
3333
*
3434
* Subtract two vectors to produce a new vector (v1-v2)
3535
*/
36-
static subtract(v1: Vector3, v2: Vector3): Vector3 {
37-
return { x: v1.x - v2.x, y: v1.y - v2.y, z: v1.z - v2.z };
36+
static subtract(v1: Vector3, v2: Partial<Vector3>): Vector3 {
37+
return { x: v1.x - (v2.x ?? 0), y: v1.y - (v2.y ?? 0), z: v1.z - (v2.z ?? 0) };
3838
}
3939

4040
/** scale

libraries/math/src/vector3/vectorWrapper.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe('Vector3Builder', () => {
5454
expect(resultTwo).toBe(vectorA); // Referential equality must be preserved
5555
});
5656

57+
it('should be able to add a partial vector3 with the same result as the coreHelpers function', () => {
58+
const vectorA = new Vector3Builder(1, 2, 3);
59+
const vectorB: Partial<Vector3> = { x: 4, z: 6 };
60+
const vectorC = Vector3Utils.add(vectorA, vectorB);
61+
62+
const result = vectorA.add(vectorB);
63+
expect(result).toEqual(vectorC);
64+
expect(vectorA).toBe(result); // Referential equality must be preserved
65+
});
66+
5767
it('should be able to subtract a vector3 with the same result as the coreHelpers function', () => {
5868
const vectorA = new Vector3Builder(5, 7, 9);
5969
const vectorB = new Vector3Builder(4, 5, 6);
@@ -70,6 +80,16 @@ describe('Vector3Builder', () => {
7080
expect(resultTwo).toBe(vectorA); // Referential equality must be preserved
7181
});
7282

83+
it('should be able to subtract a partial vector3 with the same result as the coreHelpers function', () => {
84+
const vectorA = new Vector3Builder(1, 2, 3);
85+
const vectorB: Partial<Vector3> = { x: 4, z: 6 };
86+
const vectorC = Vector3Utils.subtract(vectorA, vectorB);
87+
88+
const result = vectorA.subtract(vectorB);
89+
expect(result).toEqual(vectorC);
90+
expect(vectorA).toBe(result); // Referential equality must be preserved
91+
});
92+
7393
it('should be able to scale a vector3 with the same result as the coreHelpers function', () => {
7494
const vectorA = new Vector3Builder(1, 2, 3);
7595
const vectorB = Vector3Utils.scale(vectorA, 3);

libraries/math/src/vector3/vectorWrapper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Vector3Builder implements Vector3 {
5757
*
5858
* Adds the vector v to this, returning itself.
5959
*/
60-
add(v: Vector3): this {
60+
add(v: Partial<Vector3>): this {
6161
return this.assign(Vector3Utils.add(this, v));
6262
}
6363

@@ -66,7 +66,7 @@ export class Vector3Builder implements Vector3 {
6666
*
6767
* Subtracts the vector v from this, returning itself.
6868
*/
69-
subtract(v: Vector3): this {
69+
subtract(v: Partial<Vector3>): this {
7070
return this.assign(Vector3Utils.subtract(this, v));
7171
}
7272

0 commit comments

Comments
 (0)