module Physics.Hipmunk.Body
(
Body,
newBody,
Mass,
getMass,
setMass,
Moment,
getMoment,
setMoment,
getPosition,
setPosition,
Velocity,
getVelocity,
setVelocity,
Force,
getForce,
setForce,
getAngle,
setAngle,
AngVel,
getAngVel,
setAngVel,
Torque,
getTorque,
setTorque,
slew,
updateVelocity,
updatePosition,
resetForces,
applyForce,
applyOnlyForce,
applyImpulse,
applyDampedSpring,
localToWorld,
worldToLocal
)
where
import Foreign hiding (rotate, new)
import Physics.Hipmunk.Common
import Physics.Hipmunk.Internal
newBody :: Mass -> Moment -> IO Body
newBody mass inertia = do
b <- mallocForeignPtrBytes (156)
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
getMass :: Body -> IO Mass
getMass (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
setMass :: Body -> Mass -> IO ()
setMass (B b) m = do
withForeignPtr b $ \ptr -> do
cpBodySetMass ptr m
foreign import ccall unsafe "wrapper.h"
cpBodySetMass :: BodyPtr -> Mass -> IO ()
type Moment = CpFloat
getMoment :: Body -> IO Moment
getMoment (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 24) ptr
setMoment :: Body -> Moment -> IO ()
setMoment (B b) i = do
withForeignPtr b $ \ptr -> do
cpBodySetMoment ptr i
foreign import ccall unsafe "wrapper.h"
cpBodySetMoment :: BodyPtr -> CpFloat -> IO ()
getAngle :: Body -> IO Angle
getAngle (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 88) ptr
setAngle :: Body -> Angle -> IO ()
setAngle (B b) a = do
withForeignPtr b $ \ptr -> do
cpBodySetAngle ptr a
foreign import ccall unsafe "wrapper.h"
cpBodySetAngle :: BodyPtr -> CpFloat -> IO ()
getPosition :: Body -> IO Position
getPosition (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 40) ptr
setPosition :: Body -> Position -> IO ()
setPosition (B b) pos = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> pokeByteOff hsc_ptr 40) ptr pos
type Velocity = Vector
getVelocity :: Body -> IO Velocity
getVelocity (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 56) ptr
setVelocity :: Body -> Velocity -> IO ()
setVelocity (B b) v = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> pokeByteOff hsc_ptr 56) ptr v
type Force = Vector
getForce :: Body -> IO Force
getForce (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 72) ptr
setForce :: Body -> Force -> IO ()
setForce (B b) f = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> pokeByteOff hsc_ptr 72) ptr f
type AngVel = CpFloat
getAngVel :: Body -> IO AngVel
getAngVel (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 96) ptr
setAngVel :: Body -> AngVel -> IO ()
setAngVel (B b) w = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> pokeByteOff hsc_ptr 96) ptr w
type Torque = CpFloat
getTorque :: Body -> IO Torque
getTorque (B b) = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> peekByteOff hsc_ptr 104) ptr
setTorque :: Body -> Torque -> IO ()
setTorque (B b) t = do
withForeignPtr b $ \ptr -> do
(\hsc_ptr -> pokeByteOff hsc_ptr 104) ptr t
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
setForce b 0
setTorque 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
setForce b f
setTorque 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 ()