haskell - How to create an Event that contains the difference between the value at current tick and the value at the value at previous tick? -


i'm using reactive-banana , sdl2 (using this glue library) game-like project. behavior created "absolute mouse location", behavior "relative mouse location" (a.k.a. mouse movement). when not using frp works well, frp "relative mouse location" becomes problem: seems small amount of data comes through. suspect happens because underlying "sdl events" (that represent behavior) not line nicely tick events.

so want calculate own mouse movement, comparing mouse location @ current tick location @ previous tick. i'm not sure if solve problems, have hope :)

first of i'm lost on how approach it: state monad, or ioref, or reactive-banana provide means?

i'll give small excerpt of code have:

makenetwork :: graphicsdata -> sdleventsource -> momentio () makenetwork gd sdleventsource = mdo   ticke          <- tickevent sdleventsource   mousemovementb <- frompoll sdl.getrelativemouselocation   mousepositionb <- frompoll sdl.getabsolutemouselocation    let mousepositione = mousepositionb <@ ticke       mousemovemente = mousemovementb <@ ticke  -- yields flaky data    -- ... rest of network description left out ... 

as explained above i'd express mousemovemente in terms of mousepositionb @ current ticke (known mousepositione) , mousepositione value @ previous ticke.

any appreciated!

you looking accume builds events streams of events. highly recommend reading recursion section of documentation describes how it's implemented in terms of stepper , apply.

accume :: monadmoment m => -> event (a -> a) -> m (event a) --        starting value --^           |             | --  stream of events modify  --^             | --  resulting events                               --^ 

to compute difference between 2 points accume we'll need keep track of previous point. we'll keep track of current point. keep little two-item history of recent events.

  (point v2 cint , point v2 cint) -- previous value, current value  edges :: monadmoment m => -> event -> m (event (a, a)) edges initial later = accume (initial, initial) (shift <$> later)             shift x2 (x0, x1) = (x1, x2) 

to difference we'll subtract previous 1 current one. give complete network like

makenetwork :: graphicsdata -> sdleventsource -> momentio () makenetwork gd sdleventsource = mdo   ticke          <- tickevent sdleventsource   mousepositionb <- frompoll sdl.getabsolutemouselocation    let mousepositione = mousepositionb <@ ticke    mousehistorye <- edges 0 mousepositione    let mousemovemente = (\(x0, x1) -> x1 ^-^ x0) <$> mousehistorye    -- ... 

zero , ^-^ come linear.vector


Comments