debug output of game objects in Haskell/Yampa and HOOD

422 views Asked by At

I'm stuck with generating debug output for my game objects using Haskell/Yampa (=Arrows) (with HOOD).

My engine basically runs a list of game objects which produce Output states (line, circle) which are then rendered.

data Output = Circle Position2 Double | Line Vector2

output :: [Output] -> IO ()
output oos = mapM render oos

render :: Output -> IO ()
render (Circle p r) = drawCircle p r
render (Line   vec) = drawLine (Point2 0 0) vec

The player object just moves to the right and is represented as a (positioned) circle.

playerObject :: SF () Output -- SF is an Arrow run by time
   p <- mover (Point2 0 0) -< (Vector2 10 0)
   returnA -< (Circle p 2.0)

mover is just a simple integrator (acceleration->velocity->position) where I want to observe the velocity and render it as debug output as an (unpositioned) Line.

mover :: Position2 -> SF Vector2 Position2
mover position0 = proc acceleration -> do
    velocity <- integral -< acceleration -- !! I want to observe velocity
    position <- (position0 .+^) ^<< integral -< velocity
    returnA -< position

How can I create additional graphical debug output for internal values of my game object functions?

What actually should happen is in output, first render the actual object (circle) but also render additional debug output (movement vector as line). Probably I can achieve this with HOOD but I'm still not fluent in Haskell and don't know how do adopt the HOOD tutorial for my case.

2

There are 2 answers

1
sauf On

I don't know HOOD but Debug.Trace is easy:

> import Debug.Trace
> mover position0 = proc acceleration -> do
> > velocity <- integral -< acceleration
> > position <- trace ("vel:" ++ show velocity ++ "\n") $
> > > > > > > > > > > (position0 .+^) ^<< integral -< velocity
> > returnA -< position

Note that it shouldn't be put on the line defining velocity.

2
Edward Z. Yang On

What you probably want to do is make mover more flexible, to support adding out-of-band debug information (the value of velocity) to be rendered. I don't think HOOD is relevant for your problem, since you already have the FRP framework for handling continuously changing values. Just arrange for velocity to be output.

Something like:

mover :: Position2 -> SF Vector2 (Position2, Vector2)
mover position0 = proc acceleration -> do
    velocity <- integral -< acceleration -- !! I want to observe velocity
    position <- (position0 .+^) ^<< integral -< velocity
    returnA -< (position, velocity)

playerObject :: SF () [Output]
   (p, v) <- mover (Point2 0 0) -< (Vector2 10 0)
   returnA -< [Circle p 2.0, Line v]