Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crash on collision #9

Open
hengchu opened this issue Mar 23, 2015 · 4 comments
Open

crash on collision #9

hengchu opened this issue Mar 23, 2015 · 4 comments

Comments

@hengchu
Copy link

hengchu commented Mar 23, 2015

I'm using Hipmunk together with the Arrow-based FRP GUI package UISF: https://github.com/dwincort/UISF

Basically UISF is used to draw stuff onto the screen while Hipmunk does the number crunching. However, whenever a collision happens in the sample code below, Hipmunk crashes because a pointer passed to cpCollideShapes is not valid.

I have verified that HipmunkPlayground works fine in my environment (OSX 10.10.3, GHC 7.8.4), so it shouldn't be a bad installation.

You should be able to compile and run the code below, and if you remove the line that adds the static line segment, the program will not crash:

{-# LANGUAGE Arrows #-}

import Data.StateVar
import qualified Physics.Hipmunk as H
import FRP.UISF.SOE
import FRP.UISF
import FRP.UISF.UISF
import FRP.UISF.UITypes
import FRP.UISF.Widget (mkWidget)

main :: IO ()
main = do sp <- H.newSpace 
          body <- H.newBody 1.0 5.0
          H.position body $= H.Vector 0 0
          H.velocity body $= H.Vector 0 (-25)
          shape <- H.newShape body (H.Circle 5) (H.Vector 0 0)
          H.spaceAdd sp shape
          H.spaceAdd sp body

          static <- H.newBody H.infinity H.infinity
          seg <- H.newShape static (H.LineSegment (H.Vector 0 $ -20) (H.Vector 100 $ -20) 1) 0 
          H.spaceAdd sp (H.Static seg)

          runUI' $ Main.loop sp body

loop :: H.Space -> H.Body -> UISF () ()
loop sp body = proc _ -> do
  rec (next, dt) <- animation <<< delay 0 -< next
  pos  <- (arrowSimulation sp body) <<< delay 0 -< dt
  mwidget (0, 0) -< convert pos


animation :: UISF Time (Time, Time)
animation = proc i -> do
  dt <- delta -< i
  returnA -< (i+dt, dt)

delta :: UISF Time Time
delta = proc lasttime -> do
  n <- getTime -< ()
  returnA -< n - lasttime

monadicSimulation :: H.Space -> H.Body -> H.Time -> IO (H.Position)
monadicSimulation space body dt = do H.step space dt
                                     get . H.position $ body

arrowSimulation :: H.Space -> H.Body -> UISF (H.Time) (H.Position)
arrowSimulation space body = uisfPipe (monadicSimulation space body)

mwLayout :: Layout
mwLayout = makeLayout (Stretchy 100) (Stretchy 100)

type MWState = (Int, Int) -- (x, y) of the circle

mwCompute :: MWState -> MWState -> Rect -> UIEvent -> ((), MWState, DirtyBit)
mwCompute s _ _ _ = ((), s, True)

mwDraw :: Rect -> Bool -> MWState -> Graphic
mwDraw _ _ (x, y) =
  withColor Blue $ ellipse (x, y) (x+30, y+30)

mwidget :: MWState -> UISF MWState ()
mwidget initials = mkWidget initials mwLayout mwCompute mwDraw

convert :: H.Vector -> MWState
convert (H.Vector x y) = (round x, -round y)
@hengchu
Copy link
Author

hengchu commented Mar 23, 2015

I'm actually working on upgrading Hipmunk to Chipmunk 6.2.2. After some initial testing, the above code actually works with 6.2.2. Hopefully I'll be able to submit a pull request soon.

@suzumiyasmith
Copy link

Is this project still active? Chipmunk is already upgraded to 7.0 version.

@meteficha
Copy link
Owner

Not really, sorry. It's a lot of effort to maintain it and I don't have interest in spending the time anymore.

@suzumiyasmith
Copy link

Well. It still works well with lts-8.18.
For the newer version, chipmunk api has some break changes. (I'm not good at C)
All in all, thanks for your devotion to hipmunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants