module HGE2D.Physical where
import HGE2D.Types
import HGE2D.Datas
import HGE2D.Classes
import HGE2D.Instances
applyPhysics :: (IsPhysicalObject a) => Millisecond -> a -> a
applyPhysics ms x = setPhys newPo x
where
newPo = (applyVel . applyAcc . applyRotVel . applyRotAcc . applyDrag . applyRotDrag) (getPhys x)
applyVel po = moveBy (dX, dY) po
where
dX = (fromIntegral ms) * (fst $ physicalVel po)
dY = (fromIntegral ms) * (snd $ physicalVel po)
applyAcc po = po { physicalVel = (vX, vY) }
where
vX = (fst $ physicalVel po) + (fromIntegral ms) * (fst $ physicalAcc po)
vY = (snd $ physicalVel po) + (fromIntegral ms) * (snd $ physicalAcc po)
applyRotVel po = po { physicalRot = fixedRot }
where
fixedRot | newRot > 2.0 * pi = newRot 2.0 * pi
| newRot < 0 = newRot + 2.0 * pi
| otherwise = newRot
newRot = (physicalRot po) + dRot
dRot = (fromIntegral ms) * (physicalRotSpeed po)
applyRotAcc po = po { physicalRotSpeed = newRotSpeed }
where
newRotSpeed = (physicalRotAcceleration po) + dRotSpeed
dRotSpeed = (fromIntegral ms) * (physicalRotAcceleration po)
applyDrag = id ---TODO
applyRotDrag = id ---TODO