Skip to content
Snippets Groups Projects
Commit e6d4bed7 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Better wobble, note decay, fixed timestep physics

parent 0a236635
No related branches found
No related tags found
No related merge requests found
...@@ -40,11 +40,15 @@ particleNotes = {} ...@@ -40,11 +40,15 @@ particleNotes = {}
particleLifetime = 1.0 particleLifetime = 1.0
brightness = 1.0 brightness = 1.0
spinRate = 1.0 spinRate = 1.0
noteSustain = 1.6
wobbleAmount = 24.0
origin = [0, 0, 0] origin = [0, 0, 0]
# Physics # Physics
numTimesteps = 20 numPhysicsTimesteps = 20
gain = 0.005 frameDelay = 5
timestepSize = 0.010
gain = 0.1
previousNow = 0 previousNow = 0
spinAngle = 0 spinAngle = 0
...@@ -98,11 +102,9 @@ draw = () -> ...@@ -98,11 +102,9 @@ draw = () ->
# Time delta calculations # Time delta calculations
now = clock() now = clock()
timeStep = now - previousNow
previousNow = now
# Global spin update # Global spin update
spinAngle = (spinAngle + timeStep * spinRate) % (Math.PI * 2) spinAngle = (spinAngle + timestepSize * spinRate) % (Math.PI * 2)
# Launch new particles for all active notes # Launch new particles for all active notes
for key, note of particleNotes for key, note of particleNotes
...@@ -133,23 +135,27 @@ draw = () -> ...@@ -133,23 +135,27 @@ draw = () ->
# Intensity mapped to velocity, nonlinear # Intensity mapped to velocity, nonlinear
p.intensity = Math.pow(p.note.velocity / 100, 2.0) * 0.2 * brightness p.intensity = Math.pow(p.note.velocity / 100, 2.0) * 0.2 * brightness
# Fade with age
noteAge = now - p.note.timestamp
p.intensity *= Math.max(0, 1 - (noteAge / noteSustain))
# Falloff gets sharper as the note gets higher # Falloff gets sharper as the note gets higher
p.falloff = 15 * Math.pow(2, (p.note.key - 60) / 6) p.falloff = 15 * Math.pow(2, (p.note.key - 60) / 6)
# Add influence of LFOs # Add influence of LFOs
for key, note of lfoNotes for key, note of lfoNotes
age = now - note.timestamp lfoAge = now - note.timestamp
hz = midiToHz key hz = midiToHz key
lfoAngle = midiToAngle key lfoAngle = midiToAngle key
# Amplitude starts with left hand velocity # Amplitude starts with left hand velocity
wobbleAmp = Math.pow(note.velocity / 100, 2.0) * 12.0 wobbleAmp = Math.pow(note.velocity / 100, 2.0) * wobbleAmount
# Scale based on particle fuzziness # Scale based on particle fuzziness
wobbleAmp /= p.falloff wobbleAmp /= p.falloff
# Fade over time # Fade over time
wobbleAmp /= 1 + age wobbleAmp /= 1 + lfoAge
# Wobble # Wobble
wobbleAmp *= Math.sin(p.life * Math.pow(3, (p.note.key - 35) / 12.0)) wobbleAmp *= Math.sin(p.life * Math.pow(3, (p.note.key - 35) / 12.0))
...@@ -159,16 +165,16 @@ draw = () -> ...@@ -159,16 +165,16 @@ draw = () ->
y += wobbleAmp * Math.sin lfoAngle y += wobbleAmp * Math.sin lfoAngle
# Update velocity; use the XZ plane # Update velocity; use the XZ plane
p.velocity[0] += (x + origin[0] - p.point[0]) * gain p.velocity[0] += (x + origin[0] - p.point[0]) * (gain / numPhysicsTimesteps)
p.velocity[2] += (y + origin[2] - p.point[2]) * gain p.velocity[2] += (y + origin[2] - p.point[2]) * (gain / numPhysicsTimesteps)
# Fixed timestep physics # Fixed timestep physics
for i in [1 .. numTimesteps] for i in [1 .. numPhysicsTimesteps]
p.point[0] += p.velocity[0] p.point[0] += p.velocity[0]
p.point[1] += p.velocity[1] p.point[1] += p.velocity[1]
p.point[2] += p.velocity[2] p.point[2] += p.velocity[2]
p.life -= timeStep / particleLifetime p.life -= timestepSize / particleLifetime
# Filter out dead particles # Filter out dead particles
particles = particles.filter (p) -> p.life > 0 particles = particles.filter (p) -> p.life > 0
...@@ -176,4 +182,4 @@ draw = () -> ...@@ -176,4 +182,4 @@ draw = () ->
# Render particles to the LEDs # Render particles to the LEDs
client.mapParticles particles, model client.mapParticles particles, model
setInterval draw, 5 setInterval draw, frameDelay
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment