From 1b59ab3db2d1ff63beafdb60d71a97cf42f90685 Mon Sep 17 00:00:00 2001 From: Reshma R Date: Tue, 11 Nov 2025 14:11:51 +0900 Subject: [PATCH 1/2] fixes bug in #8215 --- src/math/p5.Vector.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 3f4adde462..9dccb25a68 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2189,7 +2189,21 @@ class Vector { */ setHeading(a) { if (this.isPInst) a = this._toRadians(a); - let m = this.mag(); + if (this.z !== 0) { + p5._friendlyError( + 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' + + 'For 3D or higher-dimensional vectors, use rotate() or another ' + + 'appropriate method instead.', + 'p5.Vector.setHeading' + ); + return this; + } + const m = this.mag(); + if (m === 0) { + this.x = 0; + this.y = 0; + return this; + } this.x = m * Math.cos(a); this.y = m * Math.sin(a); return this; From bd3cb736234f698c46f24ffc0ee0a4ff84eebe59 Mon Sep 17 00:00:00 2001 From: Reshma R Date: Wed, 31 Dec 2025 19:12:28 +0900 Subject: [PATCH 2/2] fix --- src/math/p5.Vector.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 9dccb25a68..2ba6305211 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2189,21 +2189,19 @@ class Vector { */ setHeading(a) { if (this.isPInst) a = this._toRadians(a); - if (this.z !== 0) { - p5._friendlyError( - 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' + - 'For 3D or higher-dimensional vectors, use rotate() or another ' + - 'appropriate method instead.', - 'p5.Vector.setHeading' - ); + if (this.y === undefined || (this.z !== undefined && this.z !== 0)) { + const p5inst = this.isPInst ? this._pInst : (typeof p5 !== 'undefined' ? p5 : null); + if (p5inst && p5inst._friendlyError) { + p5inst._friendlyError( + 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' + + 'For 3D or higher-dimensional vectors, use rotate() or another ' + + 'appropriate method instead.', + 'p5.Vector.setHeading' + ); + } return this; } const m = this.mag(); - if (m === 0) { - this.x = 0; - this.y = 0; - return this; - } this.x = m * Math.cos(a); this.y = m * Math.sin(a); return this;