davidedc / livecodelab

a web based livecoding environment
http://livecodelab.net/
MIT License
329 stars 63 forks source link

if/then/else mangles the "implicit" functions mechanism, see example below #202

Closed davidedc closed 11 years ago

davidedc commented 11 years ago

this code and its variations should be made to work:

rotate
if random() > 0.5 then ball else peg
noio commented 11 years ago

This relates to your other issue about persistent variables. I take it that this code (random()) would randomly pick a ball or a peg each frame? (intense flickering?) Is that what a user expects?

I was thinking of this in relation to particle emitters, imagine being able to have particle emitters as a block, like:

10 particles:
    ball

However, for these particles to be emitted (move) they would need a persistent (random) direction and speed. The current LCL syntax is kind of a memoryless draw loop, which I really like (except for doOnce and window.x tricks). Perhaps some kind of deterministic pseudo-randomness might make it easier to ... let's say preserve the "Markov property", i.e. not have to remember a bunch of state information of previous frames.

Ok I realize this actually has nothing to do with this issue, feel free to ignore ;) .

davidedc commented 11 years ago

Yes in the example above random() generates a number between 0 and 1, and the if structure around it creates intense flickering.

noise() - which is perlin noise - gives you random numbers in a sequence that can be as "smooth" as you want - there is an example here https://github.com/davidedc/livecodelab/commit/3376857719cc3779fee1bf3bcbd672bda3e34c0d and you can get multiple "streams" of numbers by either using a different "time" point as the argument, or using a second parameter, as noise() can generate noise in multiple dimensions.

I'm pretty sure one can fake particles by just moving a number of primitives just based on time and frame and the "index" of the primitive, avoiding the state, I've seen a few of those in processing.

Or, as you mention, a "window.positions" array could hold the state of some particles. The doOnce and window. are "tricks" but I think with some little cosmetics can be made legitimate.

Beyond that, I'm not familiar with particles "abstractions" enough to actually propose abstract APIs that could keep all sorts of states behind the courtains...

davidedc commented 11 years ago

fixed with https://github.com/davidedc/livecodelab/commit/da68f6b65deafb2d9dc19920301d9a4fed139dcf

() can be now omitted pretty much anywhere, the system adds them automatically.

I can do for example

if random > 0.5 then ball else peg wave

and the transformer transforms into

if random() > 0.5 then ball() else peg wave()

which is then transformed by coffeescript into

if (random() > 0.5) {
  ball();
} else {
  peg(wave());
}