9
我試圖用Yampa框架模擬一個彈跳球:給定一個初始x位置,高度和速度,球應該根據重力規則彈跳。信號功能需要一個「提示 - 事件」作爲輸入,這個想法是「當球被傾斜時,速度應該翻倍」。爲什麼這Yampa球彈跳進入一個無限循環?
球彈跳得很好,但每次發生小費事件時,該功能都會進入無限循環。我想我可能需要添加一個延遲(dSwitch,pre,notYet?),但我不知道如何。任何幫助,將不勝感激!
{-# LANGUAGE Arrows #-}
module Ball where
import FRP.Yampa
type Position = Double
type Velocity = Double
type Height = Double
data Ball = Ball {
height :: Height,
width :: Position,
vel :: Velocity
} deriving (Show)
type Tip = Event()
fly :: Position -> (Height, Velocity) -> SF Tip (Ball, Event (Height,Velocity))
fly w0 (h0, v0) = proc tipEvent -> do
let tip = (tipEvent == Event())
v <- (v0+) ^<< integral -< -10.0
h <- (h0+) ^<< integral -< v
returnA -< (Ball h w0 v,
if h < 0 then Event (0,(-v*0.6))
else if tip then Event (h, (v*2))
else NoEvent)
bounce w (h,v) = switch (fly w (h,v)) (bounce w)
runBounce w (h,v) = embed (bounce 10 (100.0, 10.0)) (deltaEncode 0.1 [NoEvent, NoEvent, NoEvent, Event(), NoEvent])
編輯:我設法避免通過反饋標誌尖端發生時的無限循環,但仍然不覺得自己做了正確的方式...
fly :: Position -> (Height, Velocity, Bool) -> SF Tip (Ball, Event (Height,Velocity,Bool))
fly w0 (h0, v0, alreadyTipped) = proc tipEvent -> do
let tip = tipEvent == Event() && (not alreadyTipped)
v <- (v0+) ^<< integral -< -10.0
h <- (h0+) ^<< integral -< v
returnA -< (Ball h w0 v,
if h < 0 then Event (0,(-v*0.6), False)
else if tip then Event (h, (v*2), True)
else NoEvent)
bounce w (h,v,alreadyTipped) = switch (fly w (h,v,alreadyTipped)) (bounce w)
iirc的描述在The Yampa Arcade論文中。 – 2011-07-03 12:28:58