From 0948251ebce67978f33d56e1867eaf16c04bbab4 Mon Sep 17 00:00:00 2001 From: Reshma R Date: Tue, 11 Nov 2025 14:11:51 +0900 Subject: [PATCH] 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 97b72ab61b..50a3b695d5 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2191,7 +2191,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;