Skip to content

Commit

Permalink
lerpWrap and random generator
Browse files Browse the repository at this point in the history
- added lerpWrap and distanceWrap
- replaced angleLerp with lerpAngle
- added RandomGenerator to replace seeded rand
  • Loading branch information
KilledByAPixel committed Jan 10, 2024
1 parent c577208 commit 062e60e
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 201 deletions.
93 changes: 68 additions & 25 deletions build/littlejs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,48 @@ declare module "littlejs.esm" {
* @return {Number}
* @memberof Utilities */
export function clamp(value: number, min?: number, max?: number): number;
/** Returns what percentage the value is between max and min
/** Returns what percentage the value is between valueA and valueB
* @param {Number} value
* @param {Number} [min=0]
* @param {Number} [max=1]
* @param {Number} valueA
* @param {Number} valueB
* @return {Number}
* @memberof Utilities */
export function percent(value: number, min?: number, max?: number): number;
/** Linearly interpolates the percent value between max and min
export function percent(value: number, valueA: number, valueB: number): number;
/** Returns signed wrapped distance between the two values passed in
* @param {Number} valueA
* @param {Number} valueB
* @param {Number} [wrapSize=1]
* @returns {Number}
* @memberof Utilities */
export function distanceWrap(valueA: number, valueB: number, wrapSize?: number): number;
/** Linearly interpolates between values passed in with wrappping
* @param {Number} percent
* @param {Number} [min=0]
* @param {Number} [max=1]
* @param {Number} valueA
* @param {Number} valueB
* @param {Number} [wrapSize=1]
* @returns {Number}
* @memberof Utilities */
export function lerpWrap(percent: number, valueA: number, valueB: number, wrapSize?: number): number;
/** Returns signed wrapped distance between the two angles passed in
* @param {Number} angleA
* @param {Number} angleB
* @returns {Number}
* @memberof Utilities */
export function distanceAngle(angleA: number, angleB: number): number;
/** Linearly interpolates between the angles passed in with wrappping
* @param {Number} percent
* @param {Number} angleA
* @param {Number} angleB
* @returns {Number}
* @memberof Utilities */
export function lerpAngle(percent: number, angleA: number, angleB: number): number;
/** Linearly interpolates between values passed in using percent
* @param {Number} percent
* @param {Number} valueA
* @param {Number} valueB
* @return {Number}
* @memberof Utilities */
export function lerp(percent: number, min?: number, max?: number): number;
export function lerp(percent: number, valueA: number, valueB: number): number;
/** Applies smoothstep function to the percentage value
* @param {Number} percent
* @return {Number}
Expand Down Expand Up @@ -499,11 +527,11 @@ declare module "littlejs.esm" {
* @memberof Random */
export function rand(valueA?: number, valueB?: number): number;
/** Returns a floored random value the two values passed in
* @param {Number} [valueA=1]
* @param {Number} valueA
* @param {Number} [valueB=0]
* @return {Number}
* @memberof Random */
export function randInt(valueA?: number, valueB?: number): number;
export function randInt(valueA: number, valueB?: number): number;
/** Randomly returns either -1 or 1
* @return {Number}
* @memberof Random */
Expand All @@ -526,21 +554,36 @@ declare module "littlejs.esm" {
* @return {Color}
* @memberof Random */
export function randColor(colorA?: Color, colorB?: Color, linear?: boolean): Color;
/** Seed used by the randSeeded function
* @type {Number}
* @default
* @memberof Random */
export let randSeed: number;
/** Set seed used by the randSeeded function, should not be 0
* @param {Number} seed
* @memberof Random */
export function setRandSeed(seed: number): void;
/** Returns a seeded random value between the two values passed in using randSeed
* @param {Number} [valueA=1]
* @param {Number} [valueB=0]
* @return {Number}
* @memberof Random */
export function randSeeded(valueA?: number, valueB?: number): number;
/**
* Seeded random number generator
* - Can be used to create a deterministic random number sequence
* @example
* let r = new RandomGenerator(123); // random number generator with seed 123
* let a = r.rand(); // random value between 0 and 1
* let b = r.randInt(10); // random integer between 0 and 9
* r.seed = 123; // reset the seed
* let c = r.rand(); // the same value as a
*/
export class RandomGenerator {
/** Create a random number generator with the seed passed in
* @param {Number} seed - Starting seed */
constructor(seed: number);
/** @property {Number} - random seed */
seed: number;
/** Returns a seeded random value between the two values passed in
* @param {Number} [valueA=1]
* @param {Number} [valueB=0]
* @return {Number} */
rand(valueA?: number, valueB?: number): number;
/** Returns a floored seeded random value the two values passed in
* @param {Number} valueA
* @param {Number} [valueB=0]
* @return {Number} */
randInt(valueA: number, valueB?: number): number;
/** Randomly returns either -1 or 1 deterministically
* @return {Number} */
randSign(): number;
}
/**
* 2D Vector object with vector math library
* - Functions do not change this so they can be chained together
Expand Down
132 changes: 94 additions & 38 deletions build/littlejs.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,25 +442,58 @@ function mod(dividend, divisor=1) { return ((dividend % divisor) + divisor) % di
* @param {Number} [max=1]
* @return {Number}
* @memberof Utilities */
function clamp(value, min=0, max=1)
{ return value < min ? min : value > max ? max : value; }
function clamp(value, min=0, max=1) { return value < min ? min : value > max ? max : value; }

/** Returns what percentage the value is between max and min
/** Returns what percentage the value is between valueA and valueB
* @param {Number} value
* @param {Number} [min=0]
* @param {Number} [max=1]
* @param {Number} valueA
* @param {Number} valueB
* @return {Number}
* @memberof Utilities */
function percent(value, min=0, max=1)
{ return max-min ? clamp((value-min) / (max-min)) : 0; }
function percent(value, valueA, valueB)
{ return valueB-valueA ? clamp((value-valueA) / (valueB-valueA)) : 0; }

/** Linearly interpolates the percent value between max and min
/** Linearly interpolates between values passed in using percent
* @param {Number} percent
* @param {Number} [min=0]
* @param {Number} [max=1]
* @param {Number} valueA
* @param {Number} valueB
* @return {Number}
* @memberof Utilities */
function lerp(percent, min=0, max=1){ return min + clamp(percent) * (max-min); }
function lerp(percent, valueA, valueB) { return valueA + clamp(percent) * (valueB-valueA); }

/** Returns signed wrapped distance between the two values passed in
* @param {Number} valueA
* @param {Number} valueB
* @param {Number} [wrapSize=1]
* @returns {Number}
* @memberof Utilities */
function distanceWrap(valueA, valueB, wrapSize=1)
{ const d = (valueA - valueB) % wrapSize; return d*2 % wrapSize - d; }

/** Linearly interpolates between values passed in with wrappping
* @param {Number} percent
* @param {Number} valueA
* @param {Number} valueB
* @param {Number} [wrapSize=1]
* @returns {Number}
* @memberof Utilities */
function lerpWrap(percent, valueA, valueB, wrapSize=1)
{ return valueB + clamp(percent) * distanceWrap(valueA, valueB, wrapSize); }

/** Returns signed wrapped distance between the two angles passed in
* @param {Number} angleA
* @param {Number} angleB
* @returns {Number}
* @memberof Utilities */
function distanceAngle(angleA, angleB) { distanceWrap(angleA, angleB, 2*PI); }

/** Linearly interpolates between the angles passed in with wrappping
* @param {Number} percent
* @param {Number} angleA
* @param {Number} angleB
* @returns {Number}
* @memberof Utilities */
function lerpAngle(percent, angleA, angleB) { return lerpWrap(percent, angleA, angleB, 2*PI); }

/** Applies smoothstep function to the percentage value
* @param {Number} percent
Expand Down Expand Up @@ -515,11 +548,11 @@ function formatTime(t) { return (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0); }
function rand(valueA=1, valueB=0) { return valueB + Math.random() * (valueA-valueB); }

/** Returns a floored random value the two values passed in
* @param {Number} [valueA=1]
* @param {Number} valueA
* @param {Number} [valueB=0]
* @return {Number}
* @memberof Random */
function randInt(valueA=1, valueB=0) { return Math.floor(rand(valueA,valueB)); }
function randInt(valueA, valueB=0) { return Math.floor(rand(valueA,valueB)); }

/** Randomly returns either -1 or 1
* @return {Number}
Expand Down Expand Up @@ -552,29 +585,50 @@ function randColor(colorA=new Color, colorB=new Color(0,0,0,1), linear)
new Color(rand(colorA.r,colorB.r), rand(colorA.g,colorB.g), rand(colorA.b,colorB.b), rand(colorA.a,colorB.a));
}

/** Seed used by the randSeeded function
* @type {Number}
* @default
* @memberof Random */
let randSeed = 1;

/** Set seed used by the randSeeded function, should not be 0
* @param {Number} seed
* @memberof Random */
function setRandSeed(seed) { randSeed = seed; }
///////////////////////////////////////////////////////////////////////////////

/** Returns a seeded random value between the two values passed in using randSeed
* @param {Number} [valueA=1]
* @param {Number} [valueB=0]
* @return {Number}
* @memberof Random */
function randSeeded(valueA=1, valueB=0)
/**
* Seeded random number generator
* - Can be used to create a deterministic random number sequence
* @example
* let r = new RandomGenerator(123); // random number generator with seed 123
* let a = r.rand(); // random value between 0 and 1
* let b = r.randInt(10); // random integer between 0 and 9
* r.seed = 123; // reset the seed
* let c = r.rand(); // the same value as a
*/
class RandomGenerator
{
// xorshift algorithm
randSeed ^= randSeed << 13;
randSeed ^= randSeed >>> 17;
randSeed ^= randSeed << 5;
return valueB + (valueA-valueB) * abs(randSeed % 1e9) / 1e9;
/** Create a random number generator with the seed passed in
* @param {Number} seed - Starting seed */
constructor(seed)
{
/** @property {Number} - random seed */
this.seed = seed;
}

/** Returns a seeded random value between the two values passed in
* @param {Number} [valueA=1]
* @param {Number} [valueB=0]
* @return {Number} */
rand(valueA=1, valueB=0)
{
// xorshift algorithm
this.seed ^= this.seed << 13;
this.seed ^= this.seed >>> 17;
this.seed ^= this.seed << 5;
return valueB + (valueA - valueB) * abs(this.seed % 1e9) / 1e9;
}

/** Returns a floored seeded random value the two values passed in
* @param {Number} valueA
* @param {Number} [valueB=0]
* @return {Number} */
randInt(valueA, valueB=0) { return Math.floor(this.rand(valueA, valueB)); }

/** Randomly returns either -1 or 1 deterministically
* @return {Number} */
randSign() { return this.randInt(2) * 2 - 1; }
}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4366,7 +4420,7 @@ const engineName = 'LittleJS';
* @type {String}
* @default
* @memberof Engine */
const engineVersion = '1.6.92';
const engineVersion = '1.6.93';

/** Frames per second to update objects
* @type {Number}
Expand Down Expand Up @@ -4954,6 +5008,10 @@ export {
mod,
clamp,
percent,
distanceWrap,
lerpWrap,
distanceAngle,
lerpAngle,
lerp,
smoothStep,
nearestPowerOfTwo,
Expand All @@ -4968,11 +5026,9 @@ export {
randInCircle,
randVector,
randColor,
randSeed,
setRandSeed,
randSeeded,

// Utility Classes
RandomGenerator,
Vector2,
Color,
Timer,
Expand Down
2 changes: 1 addition & 1 deletion build/littlejs.esm.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 062e60e

Please sign in to comment.