Skip to main content
3 of 4
tried to explain the source of the OP problem
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Your code constantly updates because nPts is local to a module enclosing the Manipulate. If the Manipulate enclosed the module, your code wouldn't work as you would like, but it wouldn't be constantly updating.

Here is a much simpler example showing the same behavior.

Module[{n = 1}, Manipulate[Row[{"n = ", n++}], {t, 0, 5}]]

constantly updates, but

Manipulate[Module[{n = 1}, Row[{"n = ", n++}]], {t, 0, 5}]

doesn't.

I would take a different approach. I wouldn't use locators to add more particles to the simulation; rather I would use a ClickPane. Nor would I use a slider to generate update events; rather I would use a Button

Manipulate[
  If[update, 
     update = False; pts = Plus @@@ Transpose[{pts, RandomReal[{-1, 1}, {Length@pts, 2}]}]];
  ClickPane[
    Framed@Graphics[{Blue, PointSize[0.02], Point[pts]}, PlotRange -> {{-5, 5}, {-5, 5}}], 
    AppendTo[pts, #] &],
  {{update, False}, ControlType -> None},
  {{pts, {{0, 0}}}, ControlType -> None},
  Button["Update", update = True]]

manipulate

This is much simpler than your code, but still performs the simulation you trying to implement. Give it a try and see if it satisfies your needs.

m_goldberg
  • 108.1k
  • 16
  • 104
  • 259