63 lines
1.9 KiB
Haskell
63 lines
1.9 KiB
Haskell
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 position of the mass object.
|
|
position :: m -> V2 Double
|
|
|
|
-- | Overwrite the position of the mass object.
|
|
positionUpdater :: 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)
|
|
|
|
-- | Total force acting on a particle in one simulation step
|
|
forces :: m -> V2 Double
|
|
|
|
-- | Overwrite the forces acting on a particle
|
|
forcesUpdater :: m -> (V2 Double -> m)
|
|
|
|
-- | Reset forces vector for the begining of a new simulation step
|
|
resetForces :: m -> m
|
|
resetForces m = forcesUpdater m (V2 0 0)
|
|
|
|
-- | Calculate the loads or forces acting on the mass object except for
|
|
-- collision forces.
|
|
addLoads
|
|
:: m -- ^ The mass object
|
|
-> V2 Double -- ^ force to be aggregated acting on the particle (e.g.: gravity)
|
|
-> m -- ^ Resulting mass object
|
|
addLoads m vec =
|
|
forcesUpdater m (forces m + vec)
|
|
|
|
-- | Euler integration updates
|
|
updateByEuler
|
|
:: m -- ^ The mass object
|
|
-> Double -- ^ Time step in fraction of a second
|
|
-> m -- ^ Resulting mass object
|
|
updateByEuler m dt =
|
|
let acc = (/ mass m) <$> forces m
|
|
dvel = (* dt) <$> acc
|
|
nvel = velocity m + dvel
|
|
dpos = (* dt) <$> nvel
|
|
npos = position m + dpos
|
|
in
|
|
resetForces $ positionUpdater (velocityUpdater m nvel) npos
|
|
|
|
calculateLoads
|
|
:: m -- ^ Original mass object
|
|
-> V2 Double -- ^ Gravitational force vector
|
|
-> m -- ^ Updated mass object
|
|
calculateLoads =
|
|
addLoads
|