module Physics.Hipmunk.Body
(
Body,
newBody,
Mass,
mass,
Moment,
moment,
position,
Velocity,
velocity,
maxVelocity,
Force,
force,
angle,
AngVel,
angVel,
maxAngVel,
Torque,
torque,
slew,
updateVelocity,
updatePosition,
resetForces,
applyForce,
applyOnlyForce,
applyImpulse,
applyDampedSpring,
localToWorld,
worldToLocal
)
where
import Data.StateVar
import Foreign hiding (rotate, new)
import Physics.Hipmunk.Common
import Physics.Hipmunk.Internal
newBody :: Mass -> Moment -> IO Body
newBody mass_ inertia = do
b <- mallocForeignPtrBytes (200)
withForeignPtr b $ \ptr -> do
cpBodyInit ptr mass_ inertia
return (B b)
foreign import ccall unsafe "wrapper.h"
cpBodyInit :: BodyPtr -> CpFloat -> CpFloat -> IO ()
type Mass = CpFloat
mass :: Body -> StateVar Mass
mass (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 8)
setter = withForeignPtr b . flip cpBodySetMass
foreign import ccall unsafe "wrapper.h"
cpBodySetMass :: BodyPtr -> Mass -> IO ()
type Moment = CpFloat
moment :: Body -> StateVar Moment
moment (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 24)
setter = withForeignPtr b . flip cpBodySetMoment
foreign import ccall unsafe "wrapper.h"
cpBodySetMoment :: BodyPtr -> CpFloat -> IO ()
angle :: Body -> StateVar Angle
angle (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 88)
setter = withForeignPtr b . flip cpBodySetAngle
foreign import ccall unsafe "wrapper.h"
cpBodySetAngle :: BodyPtr -> CpFloat -> IO ()
position :: Body -> StateVar Position
position (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 40)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 40)
type Velocity = Vector
velocity :: Body -> StateVar Velocity
velocity (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 56)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 56)
maxVelocity :: Body -> StateVar CpFloat
maxVelocity (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 132)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 132)
type Force = Vector
force :: Body -> StateVar Force
force (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 72)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 72)
type AngVel = CpFloat
angVel :: Body -> StateVar AngVel
angVel (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 96)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 96)
maxAngVel :: Body -> StateVar CpFloat
maxAngVel (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 140)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 140)
type Torque = CpFloat
torque :: Body -> StateVar Torque
torque (B b) = makeStateVar getter setter
where
getter = withForeignPtr b (\hsc_ptr -> peekByteOff hsc_ptr 104)
setter = withForeignPtr b . flip (\hsc_ptr -> pokeByteOff hsc_ptr 104)
slew :: Body -> Position -> Time -> IO ()
slew (B b) newpos dt = do
withForeignPtr b $ \ptr -> do
p <- (\hsc_ptr -> peekByteOff hsc_ptr 40) ptr
(\hsc_ptr -> pokeByteOff hsc_ptr 56) ptr $ (newpos p) `scale` (recip dt)
updateVelocity :: Body -> Vector -> Damping -> Time -> IO ()
updateVelocity (B b) g d dt =
withForeignPtr b $ \b_ptr ->
with g $ \g_ptr -> do
wrBodyUpdateVelocity b_ptr g_ptr d dt
foreign import ccall unsafe "wrapper.h"
wrBodyUpdateVelocity :: BodyPtr -> VectorPtr
-> CpFloat -> Time -> IO ()
updatePosition :: Body -> Time -> IO ()
updatePosition (B b) dt = do
withForeignPtr b $ \ptr -> do
cpBodyUpdatePosition ptr dt
foreign import ccall unsafe "wrapper.h"
cpBodyUpdatePosition :: BodyPtr -> Time -> IO ()
resetForces :: Body -> IO ()
resetForces b = do
force b $= 0
torque b $= 0
applyForce :: Body -> Vector -> Position -> IO ()
applyForce (B b) f p =
withForeignPtr b $ \b_ptr ->
with f $ \f_ptr ->
with p $ \p_ptr -> do
wrBodyApplyForce b_ptr f_ptr p_ptr
foreign import ccall unsafe "wrapper.h"
wrBodyApplyForce :: BodyPtr -> VectorPtr -> VectorPtr -> IO ()
applyOnlyForce :: Body -> Vector -> Position -> IO ()
applyOnlyForce b f p = do
force b $= f
torque b $= p `cross` f
applyImpulse :: Body -> Vector -> Position -> IO ()
applyImpulse (B b) j r =
withForeignPtr b $ \b_ptr ->
with j $ \j_ptr ->
with r $ \r_ptr -> do
wrBodyApplyImpulse b_ptr j_ptr r_ptr
foreign import ccall unsafe "wrapper.h"
wrBodyApplyImpulse :: BodyPtr -> VectorPtr -> VectorPtr -> IO ()
applyDampedSpring :: (Body,Position) -> (Body,Position) -> Distance
-> CpFloat -> Damping -> Time -> IO ()
applyDampedSpring (B b1,a1) (B b2, a2) rlen k dmp dt =
withForeignPtr b1 $ \b1_ptr ->
withForeignPtr b2 $ \b2_ptr ->
with a1 $ \a1_ptr ->
with a2 $ \a2_ptr -> do
wrApplyDampedSpring b1_ptr b2_ptr a1_ptr a2_ptr rlen k dmp dt
foreign import ccall unsafe "wrapper.h"
wrApplyDampedSpring :: BodyPtr -> BodyPtr -> VectorPtr -> VectorPtr
-> CpFloat -> CpFloat -> CpFloat -> Time -> IO ()
localToWorld :: Body -> Position -> IO Position
localToWorld (B b) p =
withForeignPtr b $ \b_ptr ->
with p $ \p_ptr -> do
wrBodyLocal2World b_ptr p_ptr
peek p_ptr
foreign import ccall unsafe "wrapper.h"
wrBodyLocal2World :: BodyPtr -> VectorPtr -> IO ()
worldToLocal :: Body -> Position -> IO Position
worldToLocal (B b) p =
withForeignPtr b $ \b_ptr ->
with p $ \p_ptr -> do
wrBodyWorld2Local b_ptr p_ptr
peek p_ptr
foreign import ccall unsafe "wrapper.h"
wrBodyWorld2Local :: BodyPtr -> VectorPtr -> IO ()