Skip to content

[2.x] Fix dimensional quirk in the heading() method of p5.Vector #8214

Description

@GregStanton

Problem

The documentation defines the concept of heading for 2D vectors specifically.

  • In 1.x, there are no true 2D vectors, in that z is always defined. As a result, heading() produces output for fully 3D vectors, even when z is nonzero. It also produces output for “1D” vectors, since createVector(1) could be used as a shortcut for createVector(1, 0).
  • In 2.x, we have an opportunity to resolve these unintended quirks.

Solution

Supporting only 2D input and adding errors for other dimensions would allow the simplest and clearest feature specification, and it’s how users expect this feature to be used. This could be considered a bug fix: users who accidentally provide a 3D vector can now be notified of this mistake, rather than silently allowing it. In practice, it appears that this change would cause zero (or near zero) disruption.

Disruption estimate: 0%

I used the Google search phrase site:https://editor.p5js.org/ "createVector" "heading" to find 50 sketches using the heading() method. Zero of them would break if we were to implement the proposed solution.

(I did exclude a few sketches, meaning I didn’t count them toward the numerator or denominator. One case used 4D vectors in 1.x, which is not supported, and the sketch appeared to be broken already. I also excluded three sketches that used createVector() with zero arguments somewhere in their heading calculations, since it’s not 100% clear what effect this will have in 2.x. But the goal is to prevent errors from arising in this case.)

Blocking issues (foundational issues for all of p5.Vector)

If you're interested in helping to move the current issue along, you can add a comment on the current issue to indicate whether you support the proposed solution. After that, you could add a comment on the following issues to indicate whether you support the proposals they contain, along with a brief rationale.

  • #8153: Consensus would clarify the meaning of 2D. In 1.x, this meant $z=0$, but according to the proposal, in 2.x it'd mean the vector has exactly two defined elements (this would resolve an inconsistency with the current implementations of .z and toString()).
  • #8155: Consensus on a user-facing API for checking dimension size would enable a more stable implementation of heading(). Adding a comment on the current leading proposal (.shape), either in support of the proposal or against it, would be helpful. The exact format of this API depends on the outcome of an ongoing review of getter/setter patterns in p5, so any comments can focus on the "shape" concept.

Activity

  1. added this to the 2.x Anytime milestone on Oct 28, 2025
  2. added theissue type on Oct 28, 2025
  3. reshma045 commented on Oct 28, 2025

    @reshma045
    Contributor

    Hi @GregStanton, I would like to work on this. Can you please assign this issue to me?

  4. justAnotherAnotherUser commented on Oct 28, 2025

    @justAnotherAnotherUser

    Hi @GregStanton I would like to start work on this as mentioned in the parent issue

  5. GregStanton commented on Oct 28, 2025

    @GregStanton
    CollaboratorAuthor

    Hi @reshma045 and @justAnotherAnotherUser! Thank you both for volunteering. Please see the comment I just added to the setHeading() issue about how we can organize the work on this.

  6. adity1raut commented on Nov 12, 2025

    @adity1raut

    /assign

  7. 8 remaining items

  8. ksen0 commented on Jun 17, 2026

    @ksen0
    Member

    Thans for bumping this @sidwellr . @limzykenneth and I have checked this and this one is open to a PR that:

    • emits nonblocking FES warning when not 2d vector
    • no change in behavior

    @avinxshKD yours if you want it!

    EDIT: sorry I misread the queue. But this is a very small one, apologies, I hope it's not too frustrating.

  9. adityachoudhury29 commented on Oct 23, 2026

    @adityachoudhury29

    @ksen0 Is this still open? Following your June comment, the scope I have in mind is a nonblocking FES warning when the vector is not 2D, with heading() still returning atan2(y, x) as it does today.

    I checked on main at 656b415. createVector(1).heading() returns 0 and createVector(1, 2, 3).heading() returns 1.107, neither of them emitting a warning, so nothing has landed for this yet and heading() in src/math/p5.Vector.js still has no dimension check.

    My plan is to add the check there and cover 1D, 3D and 4D vectors plus the static p5.Vector.heading(v) form in tests. #8944 does the same thing for cross() on non-3D vectors, so I would follow whatever shape that one settles on, for consistency. Happy to take it if nobody else is on it.

  10. limzykenneth commented on Nov 14, 2026

    @limzykenneth
    Member

    @adityachoudhury29 Sorry for the late reply. Just to clarify the proposed implementation, why is the proposal to check for 1D, 3D and 4D vectors specifically?

  11. PRIYANKA-NEGI-28 commented on Nov 25, 2026

    @PRIYANKA-NEGI-28

    To follow up on @limzykenneth's question: looking at src/math/p5.Vector.js, p5.Vector already exposes get dimensions() { return this.values.length; } (line 119), while heading() (lines 1929–1933) doesn't check dimensions at all.

    Checking this.dimensions !== 2 directly avoids hardcoding 1D/3D/4D and covers any arbitrary N-dimensional vector (0D, 1D, 3D, 4D, 5D, etc.) uniformly while keeping Math.atan2(this.y, this.x) unchanged.

    One edge case worth confirming before implementing: what is this.dimensions for a zero-argument createVector()? If createVector() defaults to [0, 0, 0] (3D) or [0, 0] (2D) or [] (0D), we should make sure that matches the intended Friendly Error System warning behavior — and verify that p5.disableFriendlyErrors = true and standalone new p5.Vector(...) calls behave cleanly. Happy to help with a PR and unit tests if @adityachoudhury29 isn't actively working on it!

  12. adityachoudhury29 commented on Dec 3, 2026

    @adityachoudhury29

    Hey @limzykenneth , sorry for the late reply!
    The check itself would be one general condition on the existing dimensions getter (this.values.length), which covers every non-2D size:

    heading() {
      if (this.dimensions !== 2) {
        this._friendlyError(
          'heading() is only defined for 2D vectors. This vector has ' +
            this.dimensions + ' dimension(s), so only its x and y values are used.',
          'p5.Vector.heading'
        );
      }
      const h = Math.atan2(this.y, this.x);
      if (this.isPInst) return this._fromRadians(h);
      return h;
    }
    

    As discussed above, the result doesn't change. It's still atan2(y, x), so this only adds a non-blocking warning. The static p5.Vector.heading(v) just calls v.heading(), so it gets the warning too.

    I picked those test cases because each one fails in a different way today:
    1D: y is missing and the getter falls back to 0, so createVector(5).heading() quietly returns 0.
    3D: probably the most common misuse. z is silently ignored (createVector(1, 2, 3).heading() returns about 1.107).
    4D: shows that the warning comes from the general !== 2 condition and isn't a special case for 3D.

    Also, I'd follow the existing setHeading() pattern instead, a friendly error tagged with the method name, using this._friendlyError so it's a no-op when p5.Vector is used on its own. Does that approach sound good?

  13. limzykenneth commented on Dec 3, 2026

    @limzykenneth
    Member

    @adityachoudhury29 You can go ahead with the proposed implementation with one change, the this._friendlyError() is now superceded by p5.FES.log, if possible please try to use that instead, you can find usage pattern for it elsewhere in the codebase, if it is not clear do ask me.

  14. adityachoudhury29 commented on Dec 3, 2026

    @adityachoudhury29

    Sure @limzykenneth , will start on this now.
    Kindly assign the issue to me. Thanks!

  15. adityachoudhury29 commented on Dec 3, 2026

    @adityachoudhury29

    @limzykenneth I do have 2 doubts:

    1. rotate() calls this.heading(), so createVector(1, 0, 0).rotate(0.1) prints "heading() is only defined for 2D vectors" even though user never called heading(). Should I have rotate() compute Math.atan2(this.y, this.x) directly so it doesn't warn, or should rotate() have its own non-2D warning? Or is this out of scope for this issue?
    2. The warning prints on every call. heading() is often called inside draw(), so a 3D vector would log the warning every frame. Should it warn only once or is warning on every call fine here?

    Also, a heads up -> FES.log returns undefined when p5.disableFriendlyErrors is true, so calling the result directly throws. I am planning on guarding the call with Vector.friendlyErrorsDisabled(). Same as the constructor.

  16. limzykenneth commented on Dec 3, 2026

    @limzykenneth
    Member

    For rotate it should print the message as well since it also don't have an implementation beyond 2D. You can make the message more generic.

    It is fine the message print on every call for now, the reason is the same as why I would not suggest dong the Vector.friendlyErrorsDisabled() method, if we handle it this way it means that every single part of the code base that uses FES need to have their implementation to handle these. It is much better for FES or core to take care of limiting repeated messages and cases where FES is disabled. We don't have to take care of the repeated print for now. The FES log return undefined I can make a fix and push to your PR.

  17. adityachoudhury29 commented on Dec 3, 2026

    @adityachoudhury29

    Sounds good.
    I have pushed my changes. PTAL-> #9442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions