Doing some basic calculus using Reactive Banana

后端 未结 2 1466
醉酒成梦
醉酒成梦 2021-01-18 17:19

Setup:

I am using Reactive Banana along with OpenGL and I have a gear that I want to spin. I have the following signals:

bTime :: Be         


        
2条回答
  •  遥遥无期
    2021-01-18 17:36

    A simple approach is to assume that eDisplay happens at regular time intervals, and consider bAngularVelocity to be a relative rather than absolute measure, whch would give you the really rather short solution below. [Note that this is no good if eDisplay is out of your control, or if it fires visibly irregularly, or varies in regularity because it will cause your gear to rotate at different speeds as the interval of your eDisplay changes. You'd need my other (longer) approach if that's the case.]

    eDeltaAngle :: Event t (Double -> Double)
    eDeltaAngle = (+) <$> bAngularVelocity <@ eDisplay
    

    i.e. turn the bAngularVelocity into an adder Event that fires when you eDisplay, so then

    eAngle :: Event t Double
    eAngle = accumE 0.0 eDeltaAngle
    

    and finally

    reactimate $ (draw gears) <$> eAngle
    

    Yes, approximating the integral as a sum is appropriate, and here I'm further approximating by making possibly slightly inaccurate assumtions about the step width, but it's clear and should be smooth as long as your eDisplay is more-or-less regular.

提交回复
热议问题