start rewriting physics to force based model

This commit is contained in:
nek0 2021-08-29 23:50:44 +02:00
parent 337fcf9cd0
commit f5782fc935
1 changed files with 33 additions and 40 deletions

View File

@ -9,11 +9,11 @@ class Mass m where
-- | The mass of the mass object. -- | The mass of the mass object.
mass :: m -> Double mass :: m -> Double
-- | Retrieve the acceleration of the mass object. -- | Retrieve the position of the mass object.
acceleration :: m -> V2 Double position :: m -> V2 Double
-- | Overwrite the acceleration of the mass object. -- | Overwrite the position of the mass object.
accelerationUpdater :: m -> (V2 Double -> m) positionUpdater :: m -> (V2 Double -> m)
-- | retrieve the velocity of the mass object. -- | retrieve the velocity of the mass object.
velocity :: m -> V2 Double velocity :: m -> V2 Double
@ -21,42 +21,35 @@ class Mass m where
-- | Overwrite the velocity of the mass object. -- | Overwrite the velocity of the mass object.
velocityUpdater :: m -> (V2 Double -> m) velocityUpdater :: m -> (V2 Double -> m)
-- | Retrieve the position of the mass object. -- | Total force acting on a particle in one simulation step
position :: m -> V2 Double forces :: m -> V2 Double
-- | Overwrite the position of the mass object. -- | Overwrite the forces acting on a particle
positionUpdater :: m -> (V2 Double -> m) forcesUpdater :: m -> (V2 Double -> m)
-- | The update function to let a mass react to gravitational pull. -- | Reset forces vector for the begining of a new simulation step
-- Apply all accelerations before calling the default implementation of resetForces :: m -> m
-- this function, since it will add the gravitational pull to already resetForces m = forcesUpdater m (V2 0 0)
-- existing accelerations.
gravitate -- | Calculate the loads or forces acting on the mass object except for
:: V2 Double -- ^ Vector of gravitational acceleration -- collision forces.
-> m -- ^ Original mass object addLoads
-> m -- ^ Updated mass object :: m -- ^ The mass object
gravitate g m = -> V2 Double -- ^ force to be aggregated acting on the particle (e.g.: gravity)
let acc = acceleration m + g -> 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 in
accelerationUpdater m acc positionUpdater (velocityUpdater m nvel) npos
-- | 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)