module Physics.Classes.Mass where import Linear -- | This typeclass is a centerpiece of the physics implementation and is used -- to implement the basic mass properties of a (very simplified) physical body. class Mass m where -- | The mass of the mass object. mass :: m -> Double -- | Retrieve the acceleration of the mass object. acceleration :: m -> V2 Double -- | Overwrite the acceleration of the mass object. accelerationUpdater :: m -> (V2 Double -> m) -- | retrieve the velocity of the mass object. velocity :: m -> V2 Double -- | Overwrite the velocity of the mass object. velocityUpdater :: m -> (V2 Double -> m) -- | Retrieve the position of the mass object. position :: m -> V2 Double -- | Overwrite the position of the mass object. positionUpdater :: m -> (V2 Double -> m) -- | The update function to let a mass react to gravitational pull. -- Apply all accelerations before calling the default implementation of -- this function, since it will add the gravitational pull to already -- existing accelerations. gravitate :: V2 Double -- ^ Vector of gravitational acceleration -> m -- ^ Original mass object -> m -- ^ Updated mass object gravitate g m = let acc = acceleration m + g in accelerationUpdater m acc -- | Apply acceleration to mass object und thus change its velocity accelerate :: Double -- ^ Time step duration -> m -- ^ Original mass object -> m -- ^ Updated mass object accelerate dt m = let vel = velocity m + ((dt *) <$> acceleration m) in velocityUpdater m vel -- | Apply velocity to mass object and thus change its position -- Changes in position smaller than around half a pixel per second are ignored. move :: Double -- ^ Time step duration -> m -- ^ Original mass object -> m -- ^ Updated mass object move dt m = let dpos = (dt *) <$> velocity m in positionUpdater m (position m + dpos)