Skip to content

Commit

Permalink
feat(flowField): BlackHole->FlowField forceMag
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokua committed Nov 19, 2024
1 parent 7fd0952 commit 75c13cd
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 15 deletions.
32 changes: 32 additions & 0 deletions src/sketches/flowField/BlackHole.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { interpolators } from '../../lib/scaling.mjs'
import { inheritStaticProperties, callAtInterval } from '../../util.mjs'
import EntityTypes from './EntityTypes.mjs'
import Attractor from './Attractor.mjs'
Expand All @@ -14,6 +15,7 @@ export default class BlackHole extends Attractor {
super({ radius, ...rest })
this.addInteraction(EntityTypes.FLOW_PARTICLE, this.pullParticle)
this.addInteraction(EntityTypes.POLLINATOR, this.pullPollinator)
this.addInteraction(EntityTypes.FLOW_FIELD, this.muteFlowFieldMagnitude)
}

display() {
Expand Down Expand Up @@ -65,4 +67,34 @@ export default class BlackHole extends Attractor {
},
})
}

muteFlowFieldMagnitude(flowField) {
flowField.updateQuirkFromSource({
quirk: Quirks.BLACK_HOLED,
mode: QuirkModes.ADD_UPDATE_NO_REMOVE,
shouldHaveQuirk: this.active,
source: this,
context: {
progress: 0,
forceMagnitude: 0.01,
},
update: (context) => {
if (!this.active && context.forceMagnitude < flowField.forceMagnitude) {
context.progress = Math.min(context.progress + 0.005, 1)
context.forceMagnitude = this.p.map(
interpolators.exponential(context.progress, 5),
0,
1,
context.forceMagnitude,
flowField.forceMagnitude,
)
} else if (
flowField.hasQuirk(Quirks.BLACK_HOLED) &&
context.forceMagnitude === flowField.forceMagnitude
) {
flowField.removeQuirk(Quirks.BLACK_HOLED)
}
},
})
}
}
8 changes: 4 additions & 4 deletions src/sketches/flowField/Entity.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export default class Entity {
this.hasQuirk(quirk) &&
(shouldHaveQuirk || mode === QuirkModes.ADD_UPDATE_NO_REMOVE)
) {
this.quirks.get(quirk)?.update?.(this, {
source,
...context,
})
const storedQuirk = this.quirks.get(quirk)
if (storedQuirk?.update) {
storedQuirk.update.call(this, storedQuirk.context)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/sketches/flowField/EntityTypes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const EntityTypes = {
DEFAULT: 'DEFAULT',
FLOW_FIELD: 'FLOW_FIELD',
PARTICLE: 'PARTICLE',
FLOW_PARTICLE: 'FLOW_PARTICLE',
ATTRACTOR: 'ATTRACTOR',
Expand Down
16 changes: 14 additions & 2 deletions src/sketches/flowField/FlowField.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import chroma from 'chroma-js'
import { callAtInterval } from '../../util.mjs'
import Entity from './Entity.mjs'
import EntityTypes from './EntityTypes.mjs'
import Quirks from './Quirks.mjs'

export default class FlowField extends Entity {
static entityType = EntityTypes.FLOW_FIELD

export default class FlowField {
static Modes = {
ALGORITHMIC: 'ALGORITHMIC',
GRID: 'GRID',
Expand All @@ -23,6 +29,7 @@ export default class FlowField {
mode = FlowField.Modes.ALGORITHMIC,
visualize = false,
}) {
super()
this.p = p
this.w = w
this.h = h
Expand Down Expand Up @@ -148,7 +155,12 @@ export default class FlowField {
this.p.sin(this.p.radians(this.angleOffset))

outputVector.set(this.p.cos(angle), this.p.sin(angle))
outputVector.setMag(this.forceMagnitude)

outputVector.setMag(
this.hasQuirk(Quirks.BLACK_HOLED)
? this.quirks.get(Quirks.BLACK_HOLED).context.forceMagnitude
: this.forceMagnitude,
)

return outputVector
}
Expand Down
1 change: 1 addition & 0 deletions src/sketches/flowField/FlowSystem.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export default class FlowSystem {
visualize: this.state.showField,
angleOffset: this.state.angleOffset,
})
this.blackHole.interactWith(this.flowField)
this.flowField.update()
}

Expand Down
10 changes: 3 additions & 7 deletions src/sketches/flowField/createControlPanel.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createControlPanel } from '../../lib/ControlPanel/index.mjs'
import FlowField from './FlowField.mjs'
import Particle from './FlowParticle.mjs'

export default (p, metadata) =>
Expand Down Expand Up @@ -101,13 +102,8 @@ export default (p, metadata) =>
{
type: 'Select',
name: 'forceMode',
value: 'grid',
options: [
'grid',
'algorithmic',
'combinedAdditive',
'combinedAveraged',
],
value: FlowField.Modes.ALGORITHMIC,
options: Object.keys(FlowField.Modes),
},
{
type: 'Checkbox',
Expand Down
9 changes: 7 additions & 2 deletions src/sketches/flowField/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default function (p) {
let system

const colorScale = chroma.scale(['navy', 'turquoise', 'purple', 'yellow'])
const attractorColorScale = chroma.scale(['white', 'azure', 'silver'])
const attractorColorScale = chroma.scale([
'azure',
'mintcream',
chroma('palegreen').desaturate(3),
'mistyrose',
])
const ah = new AnimationHelper({ p, frameRate: metadata.frameRate, bpm: 130 })
const controlPanel = createControlPanel(p, metadata)

Expand Down Expand Up @@ -54,7 +59,7 @@ export default function (p) {
system.updateState({
...state,
particleCount: state.count,
zOffset: ah.getTotalBeatsElapsed() * state.zOffsetMultiplier,
zOffset: ah.accumulateValue(state.zOffsetMultiplier, 0.125),
})
system.update()
system.display()
Expand Down

0 comments on commit 75c13cd

Please sign in to comment.