# How can I create a fountain effect?

Inspired by this (please use Chrome or Firefox), I tried to simulate it, but I couldn't do it. I'm not familiar with Dynamic. Here is my simple code:

Dynamic[
Graphics[{Hue[Random[]], PointSize@Large,
Point[{0, Dynamic[Clock[{0, 5, 0.5}]]}]}, Axes -> 1, PlotRange -> 5]
]

-
Have a look at the confetti question. – cormullion Jan 26 '13 at 11:50
Wolfram demo here: demonstrations.wolfram.com/ParticleSystemFountain – Chris Degnen Jan 26 '13 at 14:34

Here you have a toy to start playing with:

Edit preventing the animation running at different speeds in different machines by using Clock[] and DynamicWrapper[] (due credit to @jVincent)

n = 500; (*number of managed  particles*)
x[i_][t_] := (vx0[i] (t - delay[i])) UnitStep[t - delay[i]];
y[i_][t_] := Module[{k},  If[(k = (-#^2 + vy0@i #) UnitStep@#) < 0 &[t - delay@i],
delay[i] = t + RandomReal[{0, 5}], k]];
vcols = RandomReal[{0, 1}, n];
Table[vx0@i = RandomReal[{-1, 1}]; vy0@i = RandomReal[{10, 12}];
delay@i = RandomReal[{0, 12}], {i, n}];

points[s_] := Table[{x[i][s], y[i][s]}, {i, n}];

DynamicModule[{t}, DynamicWrapper[
Framed@Graphics[Dynamic@Point[points[t], VertexColors -> Hue /@ vcols],
PlotRange -> {{-10, 10}, {0, 40}}, ImageSize -> 200],
t = Clock[10^6]]]


-
Wow!.. this is cool.. +1 – Murta Jan 26 '13 at 13:34
GroosooooooooOO – Rojo Jan 26 '13 at 14:25
hm, I should have been faster :) +1 – acl Jan 26 '13 at 17:00
@Mr.Wizard, sure – Rojo Jan 26 '13 at 19:41
@cormullion - you mean "if you don't like" ;-). – stevenvh Jan 26 '13 at 20:00

Here is a modification @belisarius asked me to post. I was playing around and made a 3D version. Putting Dynamic inside Graphics3D lets you rotate the fountain.

n = 100;
width = 10;
x[i_][t_] := (x0[i] + vx0[i] (t - delay[i])) UnitStep[t - delay[i]];
y[i_][t_] := (y0[i] + vy0[i] (t - delay[i])) UnitStep[t - delay[i]];
z[i_][t_] := (k \[Function]
If[k < 0,
If[Abs[x[i][t]] > width || Abs[y[i][t]] > width, (* wait until over the edge *)
delay[i] = t + RandomReal[{0, 12}], 0.01],
k])[(-#^2 + vz0@i # + z0[i]) UnitStep@# &@(t - delay[i])];
Table[x0@i = y0@i = z0@i = 0;
vx0@i = RandomReal[{-1, 1}];
vy0@i = RandomReal[{-1, 1}];
vz0@i = RandomReal[{10, 12}];
delay@i = RandomReal[{0, 12}], {i, n}];
points[s_] :=
Table[{x[i][s], y[i][s], z[i][s]}, {i, n}]~Join~
Table[{x[i][s], y[i][s], 0}, {i, n}];
{Graphics3D[
GraphicsComplex[
Dynamic@points[Clock[\[Infinity]]], {Point[Range[n],
VertexColors -> Hue /@ (Range[n]/n)], LightGray,
Point@Range[n + 1, 2 n]}],
PlotRange -> {{-width, width}, {-width, width}, {0, 40}}],
Dynamic[Clock[\[Infinity]]]}


Edit: Garden Hose

Here's a variation that uses ViewPoint and ViewVector to keep gravity pointing screen-down no matter how the graphics are rotated. The hose is gratuitous; but I ran into a bug with BezierCurve, so I used BezierFunction instead. The hose droops, too, according to the current gravity.

droplets = 200;
dist = 25;
hoseHeight = 5 - dist;

pos[t_, p0_, v0_, gravity_, delay_, droplets_] :=
Table[p0[[i]] + (gravity #^2 + v0 [[i]] #) UnitStep@# &@(t - delay[[i]]), {i, droplets}];
p0 = ConstantArray[{0., 0., hoseHeight + 3.}, droplets];
v0 = MapThread[{#1 Cos[#2], #1 Sin[#2], #3} &,
{RandomReal[{0, 1}, droplets], RandomReal[{0, 2 \[Pi]}, droplets], RandomReal[{10, 12}, droplets]}];
delay = RandomReal[{0, 13}, droplets];
points[s_] := (gravity = Normalize[(vv\[Cross]vp)\[Cross]vp];
MapIndexed[(If[Max[Abs[#]] > dist,
delay[[First[#2]]] = s + RandomReal[{0, 4}]]; #) &,
pos[s, p0, v0, gravity, delay, droplets]]);
hoseFn :=
With[{offset =
5 Normalize[{gravity[[1]], gravity[[2]] + 1.*^-6, 0}]},
BezierFunction[{offset + {0, 0, hoseHeight - 5} + 2 dist gravity,
offset + {0, 0, hoseHeight - 5}, {0, 0, hoseHeight - 5}, {0, 0,
hoseHeight}}]];
vp = {1.8, -2.8, 0}; vv = {0., 0., 1.}; (* ViewPoint, ViewVertical *)

Graphics3D[{
GraphicsComplex[
Dynamic@points[Clock[\[Infinity]]], {Specularity[White, 10],
{Hue[0.45 + #/(4 droplets), 0.7, 1], Sphere[#, 0.25]} & /@ Range[droplets]
}],
{Thickness[0.0075], LightGray,
Line[{{0, 0, hoseHeight}, {0, 0, hoseHeight + 3.1}}], Darker@Green,
Dynamic@Line[Table[hoseFn[t], {t, 0, 1, 0.05}]]}
},
PlotRange -> dist, PlotRangePadding -> {{0, 0}, {0, 0}, {0, 0}},
ViewPoint -> Dynamic@vp, ViewVertical -> Dynamic@vv,
SphericalRegion -> True, ImageSize -> 400, Background -> Black]
`

In one respect it is not like a garden hose. When the graphics are rotated, the water droplets change their trajectories. In real life, you could only rotate the hose.

-
Nice use of GraphicsComplex! – Dr. belisarius Jan 27 '13 at 2:50
mmmm... skittles! – R. M. Jan 27 '13 at 4:08