Skip to content

Commit b0ef543

Browse files
authored
Merge pull request #8255 from reshma045/fix-issue8215-branch
Fix: restrict setHeading() to 2D vectors and add friendly error for others
2 parents 5a7a0a8 + 4ec5ca7 commit b0ef543

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/math/p5.Vector.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,8 +2043,16 @@ class Vector {
20432043
* }
20442044
*/
20452045
setHeading(a) {
2046-
if (this.isPInst) a = this._toRadians(a);
2047-
let m = this.mag();
2046+
if (this.dimensions < 2 || this._values.slice(2).some(v => v !== 0)) {
2047+
p5._friendlyError(
2048+
'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' +
2049+
'For 3D or higher-dimensional vectors, use rotate() or another ' +
2050+
'appropriate method instead.',
2051+
'p5.Vector.setHeading'
2052+
);
2053+
return this;
2054+
}
2055+
const m = this.mag();
20482056
this.x = m * Math.cos(a);
20492057
this.y = m * Math.sin(a);
20502058
return this;

0 commit comments

Comments
 (0)