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 は、ブログ投稿で実際にうなずきます。

この位置が世界の境界の外にある場合は、四角形を調整し (小さいイプシロンを使用して壁に引っかかるのを防ぎます)、衝突情報を返します。

Netwire 5 で頑張ってください。私もそれで遊んでいて、気に入り始めたところです。;)

于 2015-05-16T20:23:19.217 に答える