[Haskell-cafe] Design of a Physics Interface

Luke Palmer lrpalmer at gmail.com
Fri Nov 30 09:23:03 EST 2007

I'm currently working on idioms for game programming using FRP.  After
going through several representations of physics as arrows[1] I
decided that physics objects must not be implemented as arrows,
because introducing new arrows in the middle of a computation[2] leads
to ugly pain.

So far the best approach I have is to represent the physics world as a
single object World, with a function

> integrate :: TimeStep -> World -> World

But I can't figure out a good way to represent bodies in this world.
I considered:

> newBody :: (Position,Velocity) -> World -> (Body,World)

Where Body is an ADT with an internal representation of an Integer or
something.  The problem with this is that (1) there is no way to
guarantee that a Body actually exists in a World (which is a minor but
still annoying issue), and (2) that there's a possibility that you
could make a Body in one world and use it in another and there would
be no way to detect the error.

(2) could be solved using an internal representation of Data.Unique,
but (1) still remains a problem.  And as long as the function

> deleteBody :: Body -> World -> World

exists, it will always remain a problem.  Are there any clever idioms
I can use for this interface or implementation?  Is there a way to
store the data for bodies along with the Body object instead of with
the world in a way that the relationships between them respect
different generations of the World (through integrate)?  Any other


[1]  Once as SF () (Position,Velocity), and again as SF PhysIn PhysOut
where PhysIn = (Impulse,Momentum) and PhysOut = (Position,Velocity).

[2] Using the arrow joinSF :: SF (Event (SF [a] a)) [a], and other
similar style functions taking streams of SF events...

More information about the Haskell-Cafe mailing list