Using netwire-4.0.7
As the question title says, I'm trying to create a wire that produces positions (moving the position with a certain velocity in each step), "bouncing" off other objects. The simplest example I could think of was moving within a box, like in some screen savers.
I wrote this function as an attempt to do just that (for one axis only):
import Control.Wire
bouncyWire :: Double -> Double -> Double -> Double -> Wire () IO a Double
bouncyWire startPosition startVelocity lowerBound upperBound = proc _ -> do
rec
v <- delay startVelocity -< if p < lowerBound || p > upperBound
then -v else v
p <- integral_ startPosition -< v
returnA -< p
(I actually use it with a different monad, but this code doesn't actually use it and it would overcomplicate the issue here).
Stepping it with a counterSession $ 1/60
however, I get a problem - instead of "bouncing" off the wall, it gets stuck there. I think what happens is that it keeps flipping the velocity v
over and over, but I'm not sure why. I think I might be using delay wrong or something.