Behavior that is both recursive and dependent on other behavior

My network looks like this:

ePhysics :: Event t () bPlayerForce :: Behavior t (Double,Double) bPlayerPosition :: Behavior t (Double, Double) 

ePhysics starts several times with a timer.
I'm having problems defining bPlayerPosition . I intend that it starts with (0,0) , and when ePhysics is launched, bPlayerPosition will be recalculated using bPlayerForce as a parameter.

The problem is that I need to use accumB / stepper to indicate the initial value, but they only work with events, and I cannot get the strength value from bPlayerForce , since only the behavior can get the value of another behavior (using <*> ) .

An alternative would be to simply use <*> , but I don’t have an initial value, so it becomes a meaningless infinite recursion:

 let bPlayerPosition = pure calcPosition <*> bPlayerForce <*> bPlayerPosition 

I have 3 questions:

  • Is there a way to get value from behavior without <*> ? For example, when is reactimate ing or displaying an event? the question is that I did not have the opportunity to constantly hang from the very beginning.
  • Will there be a more functional / frp way to do physics? (in general, and those related to the issue)
  • How can I solve the presented problem?
+6
source share
1 answer
  • The apply combinator, also called <@> , and its variant <@ provide a way to fetch Behaviors whenever an event occurs.
  • Looks nice. The Animation.hs example does something similar. You must approximate the integral by summing over the discrete time steps that are given by your ePhysics event.
  • Something along the lines

     let bPlayerPosition = stepper (0,0) $ (calcPosition <*> bPlayerForce <*> bPlayerPosition) <@ ePhysics 
+5
source

Source: https://habr.com/ru/post/973673/


All Articles