Skip to content

[p5.js 2.0+ Bug Report]: p5.Quat.multiply is buggy #9069

Description

@inaridarkfox4231

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.1

Web browser and version

Chrome

Operating system

Windows

Steps to reproduce this

Steps:

  1. create q0 = new p5.Quat(1,2,3,4);
  2. create q1 = new p5.Quat(3,1,4,8);
  3. create q2 = q0.multiply(q1);
  4. q2.w is incorrect.

Snippet:

function setup() {
  createCanvas(400, 400);
  const q0 = new p5.Quat(1,2,3,4);
  const q1 = new p5.Quat(3,1,4,8);
  const q2 = q0.multiply(q1);
  console.log(q2.w); // -23
  console.log(q0.w * q1.w - q0.vec.dot(q1.vec)); // -43
  console.log(
    q0.w*q1.w - q0.vec.x*q1.vec.x - q0.vec.y*q1.vec.y - q0.vec.z - q1.vec.z
  ); // -23
}

It seems that the multiplication operation in p5.Quat is incorrect.

ver: 2.3.1

    /**
     * Multiplies a quaternion with other quaternion.
     * @method mult
     * @param  {p5.Quat} [quat] quaternion to multiply with the quaternion calling the method.
     * @chainable
     */
    multiply(quat) {

      return new Quat(
        this.w * quat.w - this.vec.x * quat.vec.x - this.vec.y * quat.vec.y - this.vec.z   -   quat.vec.z, // incorrect
        this.w * quat.vec.x + this.vec.x * quat.w + this.vec.y * quat.vec.z - this.vec.z * quat.vec.y,
        this.w * quat.vec.y - this.vec.x * quat.vec.z + this.vec.y * quat.w + this.vec.z * quat.vec.x,
        this.w * quat.vec.z + this.vec.x * quat.vec.y - this.vec.y * quat.vec.x + this.vec.z * quat.w
      );

    }

The correct version is as follows.

      return new Quat(
        this.w * quat.w - this.vec.x * quat.vec.x - this.vec.y * quat.vec.y - this.vec.z   *   quat.vec.z,
        this.w * quat.vec.x + this.vec.x * quat.w + this.vec.y * quat.vec.z - this.vec.z * quat.vec.y,
        this.w * quat.vec.y - this.vec.x * quat.vec.z + this.vec.y * quat.w + this.vec.z * quat.vec.x,
        this.w * quat.vec.z + this.vec.x * quat.vec.y - this.vec.y * quat.vec.x + this.vec.z * quat.w
      );

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status
    Completed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions