-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7093 from rohanjulka19/roll-fn
Implemented roll function
- Loading branch information
Showing
5 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @module Math | ||
* @submodule Quaternion | ||
*/ | ||
|
||
import p5 from '../core/main'; | ||
|
||
/** | ||
* A class to describe a Quaternion | ||
* for vector rotations in the p5js webgl renderer. | ||
* Please refer the following link for details on the implementation | ||
* https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html | ||
* @class p5.Quat | ||
* @constructor | ||
* @param {Number} [w] Scalar part of the quaternion | ||
* @param {Number} [x] x component of imaginary part of quaternion | ||
* @param {Number} [y] y component of imaginary part of quaternion | ||
* @param {Number} [z] z component of imaginary part of quaternion | ||
* @private | ||
*/ | ||
p5.Quat = class { | ||
constructor(w, x, y, z) { | ||
this.w = w; | ||
this.vec = new p5.Vector(x, y, z); | ||
} | ||
|
||
/** | ||
* Returns a Quaternion for the | ||
* axis angle representation of the rotation | ||
* | ||
* @method fromAxisAngle | ||
* @param {Number} [angle] Angle with which the points needs to be rotated | ||
* @param {Number} [x] x component of the axis vector | ||
* @param {Number} [y] y component of the axis vector | ||
* @param {Number} [z] z component of the axis vector | ||
* @chainable | ||
*/ | ||
static fromAxisAngle(angle, x, y, z) { | ||
const w = Math.cos(angle/2); | ||
const vec = new p5.Vector(x, y, z).normalize().mult(Math.sin(angle/2)); | ||
return new p5.Quat(w, vec.x, vec.y, vec.z); | ||
} | ||
|
||
conjugate() { | ||
return new p5.Quat(this.w, -this.vec.x, -this.vec.y, -this.vec.z); | ||
} | ||
|
||
/** | ||
* Multiplies a quaternion with other quaternion. | ||
* @method mult | ||
* @param {p5.Quat} [quat] quaternion to multiply with the quaternion calling the method. | ||
* @chainable | ||
*/ | ||
multiply(quat) { | ||
/* eslint-disable max-len */ | ||
return new p5.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 | ||
); | ||
/* eslint-enable max-len */ | ||
} | ||
|
||
/** | ||
* This is similar to quaternion multiplication | ||
* but when multipying vector with quaternion | ||
* the multiplication can be simplified to the below formula. | ||
* This was taken from the below stackexchange link | ||
* https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion/50545#50545 | ||
* @param {p5.Vector} [p] vector to rotate on the axis quaternion | ||
* @returns | ||
*/ | ||
rotateVector(p) { | ||
return new p5.Vector.mult( p, this.w*this.w - this.vec.dot(this.vec) ) | ||
.add( p5.Vector.mult( this.vec, 2 * p.dot(this.vec) ) ) | ||
.add( p5.Vector.mult( this.vec, 2 * this.w ).cross( p ) ) | ||
.clampToZero(); | ||
} | ||
|
||
/** | ||
* Rotates the Quaternion by the quaternion passed | ||
* which contains the axis of roation and angle of rotation | ||
* | ||
* @method rotateBy | ||
* @param {p5.Quat} [axesQuat] axis quaternion which contains | ||
* the axis of rotation and angle of rotation | ||
* @chainable | ||
*/ | ||
rotateBy(axesQuat) { | ||
return axesQuat.multiply(this).multiply(axesQuat.conjugate()). | ||
vec.clampToZero(); | ||
} | ||
}; | ||
|
||
export default p5.Quat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters