2020-12-27 01:31:10 +00:00
|
|
|
module Classes.Physics.Mass where
|
|
|
|
|
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-01-02 12:32:20 +00:00
|
|
|
-- | 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 elocity 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)
|
|
|
|
|
|
|
|
-- | 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
|
2021-01-03 00:43:37 +00:00
|
|
|
-- Changes in position smaller than around half a pixel per second are ignored.
|
2021-01-02 12:32:20 +00:00
|
|
|
move
|
|
|
|
:: Double -- ^ Time step duration
|
|
|
|
-> m -- ^ Original mass object
|
|
|
|
-> m -- ^ Updated mass object
|
|
|
|
move dt m =
|
2021-01-17 01:30:45 +00:00
|
|
|
let dpos@(V2 dx dy) = (dt *) <$> velocity m
|
2021-01-02 12:32:20 +00:00
|
|
|
in
|
2021-01-19 11:50:15 +00:00
|
|
|
(positionUpdater m) (position m + dpos)
|