Skip to content

New API: add a scaled vector #5702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/core/math/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ class Vec2 {
return this;
}

/**
* Adds a 2-dimensional vector scaled by scalar value. Does not modify the vector being added.
*
* @param {Vec2} rhs - The vector to add to the specified vector.
* @param {number} scalar - The number to multiply the added vector with.
* @returns {Vec2} Self for chaining.
* @example
* const vec = new pc.Vec2(1, 2);
*
* vec.addScaled(pc.Vec2.UP, 2);
*
* // Outputs [1, 4]
* console.log("The result of the addition is: " + vec.toString());
*/
addScaled(rhs, scalar) {
this.x += rhs.x * scalar;
this.y += rhs.y * scalar;

return this;
}

/**
* Returns an identical copy of the specified 2-dimensional vector.
*
Expand Down
22 changes: 22 additions & 0 deletions src/core/math/vec3.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ class Vec3 {
return this;
}

/**
* Adds a 3-dimensional vector scaled by scalar value. Does not modify the vector being added.
*
* @param {Vec3} rhs - The vector to add to the specified vector.
* @param {number} scalar - The number to multiply the added vector with.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(1, 2, 3);
*
* vec.addScaled(pc.Vec3.UP, 2);
*
* // Outputs [1, 4, 3]
* console.log("The result of the addition is: " + vec.toString());
*/
addScaled(rhs, scalar) {
this.x += rhs.x * scalar;
this.y += rhs.y * scalar;
this.z += rhs.z * scalar;

return this;
}

/**
* Returns an identical copy of the specified 3-dimensional vector.
*
Expand Down
23 changes: 23 additions & 0 deletions src/core/math/vec4.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ class Vec4 {
return this;
}

/**
* Adds a 4-dimensional vector scaled by scalar value. Does not modify the vector being added.
*
* @param {Vec4} rhs - The vector to add to the specified vector.
* @param {number} scalar - The number to multiply the added vector with.
* @returns {Vec4} Self for chaining.
* @example
* const vec = new pc.Vec4(1, 2, 3, 4);
*
* vec.addScaled(pc.Vec4.ONE, 2);
*
* // Outputs [3, 4, 5, 6]
* console.log("The result of the addition is: " + vec.toString());
*/
addScaled(rhs, scalar) {
this.x += rhs.x * scalar;
this.y += rhs.y * scalar;
this.z += rhs.z * scalar;
this.w += rhs.w * scalar;

return this;
}

/**
* Returns an identical copy of the specified 4-dimensional vector.
*
Expand Down
11 changes: 11 additions & 0 deletions test/core/math/vec2.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ describe('Vec2', function () {

});

describe('#addScaled', function () {

it('adds a scaled vector', function () {
const v = new Vec2(1, 2);
v.addScaled(Vec2.UP, 2);
expect(v.x).to.equal(1);
expect(v.y).to.equal(4);
});

});

describe('#ceil', function () {

it('leaves integers unchanged', function () {
Expand Down
12 changes: 12 additions & 0 deletions test/core/math/vec3.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ describe('Vec3', function () {

});

describe('#addScaled', function () {

it('adds a scaled vector', function () {
const v = new Vec3(1, 2, 3);
v.addScaled(Vec3.UP, 2);
expect(v.x).to.equal(1);
expect(v.y).to.equal(4);
expect(v.z).to.equal(3);
});

});

describe('#ceil', function () {

it('leaves integers unchanged', function () {
Expand Down
13 changes: 13 additions & 0 deletions test/core/math/vec4.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ describe('Vec4', function () {

});

describe('#addScaled', function () {

it('adds a scaled vector', function () {
const v = new Vec4(1, 2, 3, 4);
v.addScaled(Vec4.ONE, 2);
expect(v.x).to.equal(3);
expect(v.y).to.equal(4);
expect(v.z).to.equal(5);
expect(v.w).to.equal(6);
});

});

describe('#ceil', function () {

it('leaves integers unchanged', function () {
Expand Down