0

我正在尝试使用 OpenGL 将挑战 3(https://ocharles.org.uk/blog/posts/2013-08-01-getting-started-with-netwire-and-sdl.html)从 netwire 4.0 转换为 netwire 5.0 . 不幸的是,盒子不能弹跳。整个代码如下。在我看来,功能速度不起作用。当盒子与墙壁碰撞时,它不会反弹而是停止。如何更正我的程序?提前致谢。

{-# LANGUAGE Arrows #-}
import Graphics.Rendering.OpenGL 
import Graphics.UI.GLFW 
import Data.IORef 
import Prelude hiding ((.)) 
import Control.Monad.Fix (MonadFix)
import Control.Wire 
import FRP.Netwire 


isKeyDown :: (Enum k, Monoid e) => k -> Wire s e IO a e 
isKeyDown k = mkGen_ $ \_ -> do 
  s <- getKey k 
  return $ case s of 
    Press -> Right mempty     
    Release -> Left mempty


acceleration :: (Monoid e) => Wire s e IO a Double
acceleration =  pure ( 0)   . isKeyDown (CharKey 'A') . isKeyDown (CharKey 'D') 
            <|> pure (-0.5) . isKeyDown (CharKey 'A') 
            <|> pure ( 0.5) . isKeyDown (CharKey 'D') 
            <|> pure ( 0) 


velocity ::  (Monad m, HasTime t s, Monoid e) => Wire s e m (Double, Bool) Double
velocity = integralWith bounce 0
             where bounce c v 
                     | c         = (-v)
                     | otherwise =  v

collided :: (Ord a, Fractional a) => (a, a) -> a -> (a, Bool)
collided (a, b) x
  | x < a = (a, True)
  | x > b = (b, True)
  | otherwise = (x, False)


position' :: (Monad m, HasTime t s) => Wire s e m Double (Double, Bool)   
position' = integral 0 >>> (arr $ collided (-0.8, 0.8))

challenge3 :: (HasTime t s) => Wire s () IO a Double 
challenge3 = proc _ -> do
  rec a <- acceleration -< ()
      v <- velocity -< (a, c)
      (p, c) <- position' -< v
  returnA -< p


s :: Double 
s = 0.05 
y :: Double 
y = 0.0 
renderPoint :: (Double, Double) -> IO () 
renderPoint (x, y) = vertex $ Vertex2 (realToFrac x :: GLfloat) (realToFrac y :: GLfloat)

generatePoints :: Double -> Double -> Double -> [(Double, Double)] 
generatePoints x y s = 
  [ (x - s, y - s) 
  , (x + s, y - s) 
  , (x + s, y + s) 
  , (x - s, y + s) ] 


runNetwork :: (HasTime t s) => IORef Bool -> Session IO s -> Wire s e IO a Double -> IO () 
runNetwork closedRef session wire = do 
  pollEvents 
  closed <- readIORef closedRef 
  if closed 
    then return () 
    else do
      (st , session') <- stepSession session 
      (wt', wire' ) <- stepWire wire st $ Right undefined 
      case wt' of 
        Left _ -> return () 
        Right x -> do 
          clear [ColorBuffer] 
          renderPrimitive Quads $ 
            mapM_ renderPoint $ generatePoints x y s 
          swapBuffers 
          runNetwork closedRef session' wire' 



main :: IO () 
main = do
  initialize 
  openWindow (Size 1024 512) [DisplayRGBBits 8 8 8, DisplayAlphaBits 8, DisplayDepthBits 24] Window

   closedRef <- newIORef False 
  windowCloseCallback $= do 
    writeIORef closedRef True 
    return True 
  runNetwork closedRef clockSession_ challenge3
  closeWindow
4

1 回答 1

0

根据经验,我认为这里的诀窍在于,从技术上讲,您必须在实际碰撞之前反弹几个像素,因为如果您在碰撞发生时检测到它,那么惯性会使您的正方形有点“进入”墙壁,并且因此速度不断反转,导致您的方格被阻挡。

Ocharles 实际上在博客文章中对此表示赞同:

如果这个位置在世界范围之外,我们调整正方形(用一个小的 epsilon 来阻止它卡在墙上)并返回碰撞信息。

祝 Netwire 5 好运,我也在玩它,我刚刚开始喜欢它。;)

于 2015-05-16T20:23:19.217 回答