Skip to content

[p5.js 2.0+ Bug Report]: Vector.cross() assumes the vectors have three dimensions #8925

@sidwellr

Description

@sidwellr

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.3.0

Web browser and version

Brave 1.90.122

Operating system

Linux Mint 22.1

Steps to reproduce this

Steps:

  1. Create two vectors with more than three dimensions
  2. Use Vector.cross() to compute their cross product
  3. Observe no errors and a (meaningless) 3D result

Snippet:

function setup() {
  let v1 = createVector(1, 0, 0, 2);
  let v2 = createVector(0, 0, 1, -1);
  let cp = v1.cross(v2);
  
  print(cp.toString());
}

Discussion

The vector cross product is only defined for 3D vectors. I think Vector.cross() should give an error if either vector is not 3D.

Perhaps an special case should be made for 2D vectors, setting their "z" value to zero for the cross product computation. This is in fact what is currently done since the code uses Vector.z, which returns 0 for 2D vectors. Also, the cross() documentation, after stating "This method should only be used with 3D vectors", uses 2D vectors in the examples! Those examples would break if a strict check for three dimensions is implemented. So would the current implementations of angleBetween() and slerp().

This issue applies to both the instance and static methods.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions