Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Fadecandy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
scanlime
Fadecandy
Commits
e6d4bed7
Commit
e6d4bed7
authored
11 years ago
by
Micah Elizabeth Scott
Browse files
Options
Downloads
Patches
Plain Diff
Better wobble, note decay, fixed timestep physics
parent
0a236635
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/node/midi_particles.coffee
+19
-13
19 additions, 13 deletions
examples/node/midi_particles.coffee
with
19 additions
and
13 deletions
examples/node/midi_particles.coffee
+
19
−
13
View file @
e6d4bed7
...
@@ -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
+
time
S
tep
*
spinRate
)
%
(
Math
.
PI
*
2
)
spinAngle
=
(
spinAngle
+
time
s
tep
Size
*
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
a
ge
=
now
-
note
.
timestamp
lfoA
ge
=
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
+
a
ge
wobbleAmp
/=
1
+
lfoA
ge
# 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
..
num
Physics
Timesteps
]
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
-=
time
S
tep
/
particleLifetime
p
.
life
-=
time
s
tep
Size
/
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment