2021-04-29 18:28:52 +00:00
|
|
|
module Physics.Classes.Mass where
|
2020-12-27 01:31:10 +00:00
|
|
|
|
2020-12-27 04:30:57 +00:00
|
|
|
import Linear
|
|
|
|
|
2020-12-27 01:31:10 +00:00
|
|
|
-- | 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
|
|
|
|
|
2021-01-02 12:32:20 +00:00
|
|
|
-- | The mass of the mass object.
|
2020-12-27 01:31:10 +00:00
|
|
|
mass :: m -> Double
|
|
|
|
|
2021-08-29 21:50:44 +00:00
|
|
|
-- | Retrieve the position of the mass object.
|
|
|
|
position :: m -> V2 Double
|
2021-01-02 12:32:20 +00:00
|
|
|
|
2021-08-29 21:50:44 +00:00
|
|
|
-- | Overwrite the position of the mass object.
|
|
|
|
positionUpdater :: m -> (V2 Double -> m)
|
2021-01-02 12:32:20 +00:00
|
|
|
|
2021-07-08 16:39:31 +00:00
|
|
|
-- | retrieve the velocity of the mass object.
|
2020-12-27 04:25:54 +00:00
|
|
|
velocity :: m -> V2 Double
|
|
|
|
|
2021-01-02 12:32:20 +00:00
|
|
|
-- | Overwrite the velocity of the mass object.
|
|
|
|
velocityUpdater :: m -> (V2 Double -> m)
|
|
|
|
|
2021-08-29 21:50:44 +00:00
|
|
|
-- | 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
|
2021-01-02 12:32:20 +00:00
|
|
|
in
|
2021-08-29 21:50:44 +00:00
|
|
|
positionUpdater (velocityUpdater m nvel) npos
|