I'm trying to understand how this functional reactive programming works, and I have run into a problem. I am trying to create a boid simulation, but I'm starting out slowly, and I have for now defined a boid as a function taking a starting position and creating a signal function from a position to a position, where the input is the point it is moving towards and the output is the current position:
type Position2 = Vector2 Float
boid :: Position2 -> SF Position2 Position2
boid s = loop (arr (uncurry seek) >>> integral >>> arr (^+^ s) >>> arr dup)
the seek function takes two inputs (because of the loop). The current position and the target position. Then it just creates a vector pointing from the current position towards the target position with a magnitude of 50 i.e. the velocity. Then the velocity is integrated and the starting position is added. In the end the signal is split into two, so one can become the output and the other can loop back into the seek function.
Now i can define boids like this:
aBoid = constant (vector2 500 500) >>> (boid (vector2 600 60))
bBoid = aBoid >>> (boid (vector2 3 4))
Here aBoid seeks towards the point (500, 500) and bBoid seeks towards aBoid.
My problem is, when i want the two boids to seek toward each other. When i do this:
aBoid = bBoid >>> (boid (vector2 600 60))
bBoid = aBoid >>> (boid (vector2 3 4))
The program just prints: ProgramName: <<loop>>
which I assume means that it goes into an endless loop.
I have also tried to use the par
function like this:
sim :: SF a [Position2]
sim = loop (arr snd >>> par route [boid (vector2 10 10), boid (vector2 100 100)] >>> arr dup)
here the route
function just maps the output of each boid to the input of another one (like zip
, but with an offset of 1)
This also gives the <<loop>>
message.
I would think that having objects depend on the state of each other should be a pretty common problem when dealing with reactive systems, so I hope there is some elegant solution.
I should add that I find this FRP thing very hard and often confusing, so I'm not really sure if i even make sense at all ;)
So with a little help from ehird i came up with something that works.
The code is as follows(here using 3 boids instead of 2):
Here the route function works the same as the one i described in the question.
By using
loopPre
instead ofloop
i can give each boid a starting position, which solves the problem. I think the reason ehird's solution did not work, was that some plumbing is required when running several signal functions in parallel, which thepar
function takes care of.An answer from someone who is 100% sure whats going on would be appreciated :)
Edit
This implementation of
sim
is a little prettier:Where
randomBoids num
generatesnum
random boids.