Skip to content

Commit

Permalink
add angleDistance and angleLerp util functions (#65)
Browse files Browse the repository at this point in the history
thanks! i am going to rework this into more generic forms "wrappedDistance and wrappedLerp"
  • Loading branch information
ClementCariou authored Jan 10, 2024
1 parent cc9d632 commit c577208
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/engineExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export {
mod,
clamp,
percent,
angleDistance,
angleLerp,
lerp,
smoothStep,
nearestPowerOfTwo,
Expand Down
21 changes: 21 additions & 0 deletions src/engineUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ function clamp(value, min=0, max=1)
function percent(value, min=0, max=1)
{ return max-min ? clamp((value-min) / (max-min)) : 0; }

/** Returns signed distance between the two angles passed in
* @param {Number} angle1
* @param {Number} angle2
* @returns {Number}
* @memberof Utilities */
function angleDistance(angle1, angle2) {
var max = 2 * PI;
var da = (angle1 - angle2) % max;
return ((2 * da) % max) - da;
}

/** Linearly interpolates between the angles passed in
* @param {Number} Percent
* @param {Number} angle1
* @param {Number} angle2
* @returns {Number}
* @memberof Utilities */
function angleLerp(p, angle1, angle2) {
return angle1 + p * angleDistance(angle1, angle2);
}

/** Linearly interpolates the percent value between max and min
* @param {Number} percent
* @param {Number} [min=0]
Expand Down

0 comments on commit c577208

Please sign in to comment.