Skip to content

p5.Vector reflect() unexpectedly modifies surface normal argument #7088

@nbogie

Description

@nbogie

Most appropriate sub-area of p5.js?

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

p5.js version

1.9.4

Web browser and version

all

Operating system

all

Steps to reproduce this

Snippet:

function setup() {
	instanceDemo();
	staticDemo();
}

function instanceDemo() {
	const surfaceNormal = createVector(1, 0);
	const incidentVector = createVector(1, 1);
	
	incidentVector.reflect(surfaceNormal);

	//We'd hope surfaceNormal remains [1, 0, 0], but it'll be [2, 0, 0];
	console.log(surfaceNormal.toString());
}

function staticDemo() {
	const surfaceNormal = createVector(1, 0);
	const incidentVector = createVector(1, 1);
	
	p5.Vector.reflect(incidentVector, surfaceNormal);

	//We'd hope surfaceNormal remains [1, 0, 0], but it'll be [2, 0, 0];
	console.log(surfaceNormal.toString());
}

Expected behaviour:

I'd expect the surface normal argument to be unchanged by the calculation, in both instance and static cases.

Actual behaviour:

The surface normal argument is mutated during the calculation, in both instance and static cases.

Misc:

Here's a (simplification of a) real usage where the bug caught me out: https://openprocessing.org/sketch/2295422

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions