Skip to content

Commit 7896bb4

Browse files
committed
Added new methods.
* `Vector3.setFromMatrixPosition` is a new method that will set the components of the Vector3 based on the position of the given Matrix4. * `Vector3.setFromMatrixColumn` is a new method that will set the components of the Vector3 based on the specified Matrix4 column. * `Vector3.fromArray` is a new method that will set the components of the Vector3 based on the values in the given array, at the given offset. * `Vector3.min` is a new method that will set the components of the Vector3 based on the `Main.min` between it and the given Vector3. * `Vector3.max` is a new method that will set the components of the Vector3 based on the `Main.max` between it and the given Vector3. * `Vector3.addVectors` is a new method that will set the components of the Vector3 based on the addition of the two Vector3s given. * `Vector3.addScalar` is a new method that will multiply the components of the Vector3 by the scale value given. * `Vector3.applyMatrix3` is a new method that will take a Matrix3 and apply it to the Vector3. * `Vector3.applyMatrix4` is a new method that will take a Matrix4 and apply it to the Vector3.
1 parent c299c9e commit 7896bb4

1 file changed

Lines changed: 185 additions & 1 deletion

File tree

src/math/Vector3.js

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,44 @@ var Vector3 = new Class({
9393
return this;
9494
},
9595

96+
/**
97+
* Sets the components of this Vector to be the `Math.min` result from the given vector.
98+
*
99+
* @method Phaser.Math.Vector3#min
100+
* @since 3.50.0
101+
*
102+
* @param {Phaser.Math.Vector3} v - The Vector3 to check the minimum values against.
103+
*
104+
* @return {Phaser.Math.Vector3} This Vector3.
105+
*/
106+
min: function (v)
107+
{
108+
this.x = Math.min(this.x, v.x);
109+
this.y = Math.min(this.y, v.y);
110+
this.z = Math.min(this.z, v.z);
111+
112+
return this;
113+
},
114+
115+
/**
116+
* Sets the components of this Vector to be the `Math.max` result from the given vector.
117+
*
118+
* @method Phaser.Math.Vector3#max
119+
* @since 3.50.0
120+
*
121+
* @param {Phaser.Math.Vector3} v - The Vector3 to check the maximum values against.
122+
*
123+
* @return {Phaser.Math.Vector3} This Vector3.
124+
*/
125+
max: function (v)
126+
{
127+
this.x = Math.max(this.x, v.x);
128+
this.y = Math.max(this.y, v.y);
129+
this.z = Math.max(this.z, v.z);
130+
131+
return this;
132+
},
133+
96134
/**
97135
* Make a clone of this Vector3.
98136
*
@@ -106,6 +144,26 @@ var Vector3 = new Class({
106144
return new Vector3(this.x, this.y, this.z);
107145
},
108146

147+
/**
148+
* Adds the two given Vector3s and sets the results into this Vector3.
149+
*
150+
* @method Phaser.Math.Vector3#addVectors
151+
* @since 3.50.0
152+
*
153+
* @param {Phaser.Math.Vector3} a - The first Vector to add.
154+
* @param {Phaser.Math.Vector3} b - The second Vector to add.
155+
*
156+
* @return {Phaser.Math.Vector3} This Vector3.
157+
*/
158+
addVectors: function (a, b)
159+
{
160+
this.x = a.x + b.x;
161+
this.y = a.y + b.y;
162+
this.z = a.z + b.z;
163+
164+
return this;
165+
},
166+
109167
/**
110168
* Calculate the cross (vector) product of two given Vectors.
111169
*
@@ -199,6 +257,63 @@ var Vector3 = new Class({
199257
return this;
200258
},
201259

260+
/**
261+
* Sets the components of this Vector3 from the position of the given Matrix4.
262+
*
263+
* @method Phaser.Math.Vector3#setFromMatrixPosition
264+
* @since 3.50.0
265+
*
266+
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to get the position from.
267+
*
268+
* @return {Phaser.Math.Vector3} This Vector3.
269+
*/
270+
setFromMatrixPosition: function (m)
271+
{
272+
return this.fromArray(m.val, 12);
273+
},
274+
275+
/**
276+
* Sets the components of this Vector3 from the Matrix4 column specified.
277+
*
278+
* @method Phaser.Math.Vector3#setFromMatrixColumn
279+
* @since 3.50.0
280+
*
281+
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to get the column from.
282+
* @param {number} index - The column index.
283+
*
284+
* @return {Phaser.Math.Vector3} This Vector3.
285+
*/
286+
setFromMatrixColumn: function (mat4, index)
287+
{
288+
return this.fromArray(mat4.val, index * 4);
289+
},
290+
291+
/**
292+
* Sets the components of this Vector3 from the given array, based on the offset.
293+
*
294+
* Vector3.x = array[offset]
295+
* Vector3.y = array[offset + 1]
296+
* Vector3.z = array[offset + 2]
297+
*
298+
* @method Phaser.Math.Vector3#fromArray
299+
* @since 3.50.0
300+
*
301+
* @param {number[]} array - The array of values to get this Vector from.
302+
* @param {number} [offset=0] - The offset index into the array.
303+
*
304+
* @return {Phaser.Math.Vector3} This Vector3.
305+
*/
306+
fromArray: function (array, offset)
307+
{
308+
if (offset === undefined) { offset = 0; }
309+
310+
this.x = array[offset];
311+
this.y = array[offset + 1];
312+
this.z = array[offset + 2];
313+
314+
return this;
315+
},
316+
202317
/**
203318
* Add a given Vector to this Vector. Addition is component-wise.
204319
*
@@ -218,6 +333,25 @@ var Vector3 = new Class({
218333
return this;
219334
},
220335

336+
/**
337+
* Add the given value to each component of this Vector.
338+
*
339+
* @method Phaser.Math.Vector3#addScalar
340+
* @since 3.50.0
341+
*
342+
* @param {number} s - The amount to add to this Vector.
343+
*
344+
* @return {Phaser.Math.Vector3} This Vector3.
345+
*/
346+
addScalar: function (s)
347+
{
348+
this.x += s;
349+
this.y += s;
350+
this.z += s;
351+
352+
return this;
353+
},
354+
221355
/**
222356
* Add and scale a given Vector to this Vector. Addition is component-wise.
223357
*
@@ -514,6 +648,56 @@ var Vector3 = new Class({
514648
return this;
515649
},
516650

651+
/**
652+
* Takes a Matrix3 and applies it to this Vector3.
653+
*
654+
* @method Phaser.Math.Vector3#applyMatrix3
655+
* @since 3.50.0
656+
*
657+
* @param {Phaser.Math.Matrix3} mat3 - The Matrix3 to apply to this Vector3.
658+
*
659+
* @return {Phaser.Math.Vector3} This Vector3.
660+
*/
661+
applyMatrix3: function (mat3)
662+
{
663+
var x = this.x;
664+
var y = this.y;
665+
var z = this.z;
666+
var m = mat3.val;
667+
668+
this.x = m[0] * x + m[3] * y + m[6] * z;
669+
this.y = m[1] * x + m[4] * y + m[7] * z;
670+
this.z = m[2] * x + m[5] * y + m[8] * z;
671+
672+
return this;
673+
},
674+
675+
/**
676+
* Takes a Matrix4 and applies it to this Vector3.
677+
*
678+
* @method Phaser.Math.Vector3#applyMatrix4
679+
* @since 3.50.0
680+
*
681+
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to apply to this Vector3.
682+
*
683+
* @return {Phaser.Math.Vector3} This Vector3.
684+
*/
685+
applyMatrix4: function (mat4)
686+
{
687+
var x = this.x;
688+
var y = this.y;
689+
var z = this.z;
690+
var m = mat4.val;
691+
692+
var w = 1 / (m[3] * x + m[7] * y + m[11] * z + m[15]);
693+
694+
this.x = (m[0] * x + m[4] * y + m[8] * z + m[12]) * w;
695+
this.y = (m[1] * x + m[5] * y + m[9] * z + m[13]) * w;
696+
this.z = (m[2] * x + m[6] * y + m[10] * z + m[14]) * w;
697+
698+
return this;
699+
},
700+
517701
/**
518702
* Transform this Vector with the given Matrix.
519703
*
@@ -539,7 +723,7 @@ var Vector3 = new Class({
539723
},
540724

541725
/**
542-
* Transform this Vector with the given Matrix.
726+
* Transform this Vector with the given Matrix4.
543727
*
544728
* @method Phaser.Math.Vector3#transformMat4
545729
* @since 3.0.0

0 commit comments

Comments
 (0)