2017-11-07 21:22:46 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Affection
|
|
|
|
|
|
|
|
import SDL (($=))
|
|
|
|
import qualified SDL
|
|
|
|
|
|
|
|
import qualified Graphics.Rendering.OpenGL as GL
|
|
|
|
import qualified Graphics.GLUtil as GLU
|
|
|
|
|
|
|
|
import Physics.Bullet.Raw
|
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
import Control.Concurrent.STM (atomically)
|
|
|
|
import Control.Concurrent.STM.TVar
|
|
|
|
|
2017-11-07 21:22:46 +00:00
|
|
|
import Control.Monad (when)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
|
|
|
|
import Linear as L
|
|
|
|
|
|
|
|
import System.Random (randomRIO)
|
|
|
|
|
|
|
|
import SpatialMath
|
|
|
|
|
|
|
|
import Init
|
|
|
|
import Types
|
|
|
|
|
|
|
|
import Debug.Trace as T
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main =
|
2021-09-10 22:15:31 +00:00
|
|
|
withAffection (AffectionConfig
|
2017-11-07 21:22:46 +00:00
|
|
|
{ initComponents = All
|
|
|
|
, windowTitle = "hw - example 02"
|
2020-01-05 14:07:27 +00:00
|
|
|
, windowConfigs =
|
|
|
|
[ ( 0
|
|
|
|
, SDL.defaultWindow
|
|
|
|
{ SDL.windowInitialSize = SDL.V2 1920 1080
|
|
|
|
, SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL
|
|
|
|
{ SDL.glProfile = SDL.Core SDL.Normal 3 2
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 22:15:31 +00:00
|
|
|
, SDL.FullscreenDesktop
|
2020-01-05 14:07:27 +00:00
|
|
|
)
|
|
|
|
]
|
2021-09-10 22:15:31 +00:00
|
|
|
} :: AffectionConfig StateData)
|
|
|
|
|
|
|
|
instance Affectionate StateData where
|
|
|
|
preLoop = const (return ())
|
|
|
|
handleEvents sd = mapM_(handle sd)
|
|
|
|
update = Main.update
|
|
|
|
draw = Main.draw
|
|
|
|
loadState = load
|
|
|
|
cleanUp = const (return ())
|
|
|
|
hasNextStep = liftIO . readTVarIO . nextStep
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
quit = liftIO . atomically . flip writeTVar False . nextStep
|
2018-05-17 21:42:07 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
update :: StateData -> Double -> Affection ()
|
|
|
|
update sd dt = do
|
|
|
|
let g = 0.0667300
|
|
|
|
(phys, physos) <- liftIO $ do
|
|
|
|
p <- readTVarIO (physics sd)
|
|
|
|
po <- readTVarIO (physicsObjects sd)
|
|
|
|
return (p, po)
|
2017-11-07 22:04:43 +00:00
|
|
|
mapM_ (\smallBall -> do
|
|
|
|
ms1 <- liftIO $ getMotionState (bodyRigidBody smallBall)
|
|
|
|
ms2 <- liftIO $ getMotionState (bodyRigidBody $ poBigBall physos)
|
|
|
|
r1 <- liftIO $ return . fmap realToFrac =<< getPosition ms1
|
|
|
|
r2 <- liftIO $ return . fmap realToFrac =<< getPosition ms2
|
2018-05-17 21:42:07 +00:00
|
|
|
let m1 = bodyMass smallBall
|
2017-11-07 22:04:43 +00:00
|
|
|
-- m2 = bodyMass (poBigBall physos)
|
2018-05-17 21:42:07 +00:00
|
|
|
-- m2 = 1000000000000000
|
|
|
|
m2 = 1000000
|
2017-11-07 22:04:43 +00:00
|
|
|
eta_sq = 0.1 ^ 2
|
|
|
|
force = (g * m2 * m1 *^ (r2 - r1)) ^/
|
|
|
|
((sqrt (((r2 - r1) `dot` (r2 - r1)) + eta_sq)) ^ 3)
|
|
|
|
liftIO $ applyCentralForce (bodyRigidBody smallBall) force
|
2018-05-17 21:42:07 +00:00
|
|
|
) (poSmallBalls physos ++ poBigBalls physos)
|
|
|
|
|
|
|
|
mapM_ (\(bb1, bb2) -> do
|
|
|
|
ms1 <- liftIO $ getMotionState (bodyRigidBody bb1)
|
|
|
|
ms2 <- liftIO $ getMotionState (bodyRigidBody bb2)
|
|
|
|
r1 <- liftIO $ return . fmap realToFrac =<< getPosition ms1
|
|
|
|
r2 <- liftIO $ return . fmap realToFrac =<< getPosition ms2
|
|
|
|
let m1 = bodyMass bb1
|
|
|
|
-- m2 = bodyMass (poBigBall physos)
|
|
|
|
m2 = bodyMass bb2
|
|
|
|
eta_sq = 0.1 ^ 2
|
|
|
|
force = (g * m2 * m1 *^ (r2 - r1)) ^/
|
|
|
|
((sqrt (((r2 - r1) `dot` (r2 - r1)) + eta_sq)) ^ 3)
|
|
|
|
liftIO $ applyCentralForce (bodyRigidBody bb1) force
|
|
|
|
) ((,) <$> (poBigBalls physos) <*> (poBigBalls physos))
|
|
|
|
|
|
|
|
mapM_ (\(bb1, bb2) -> do
|
|
|
|
ms1 <- liftIO $ getMotionState (bodyRigidBody bb1)
|
|
|
|
ms2 <- liftIO $ getMotionState (bodyRigidBody bb2)
|
|
|
|
r1 <- liftIO $ return . fmap realToFrac =<< getPosition ms1
|
|
|
|
r2 <- liftIO $ return . fmap realToFrac =<< getPosition ms2
|
|
|
|
let m1 = bodyMass bb1
|
|
|
|
-- m2 = bodyMass (poBigBall physos)
|
|
|
|
m2 = bodyMass bb2
|
|
|
|
eta_sq = 0.1 ^ 2
|
|
|
|
force = (g * m2 * m1 *^ (r2 - r1)) ^/
|
|
|
|
((sqrt (((r2 - r1) `dot` (r2 - r1)) + eta_sq)) ^ 3)
|
|
|
|
liftIO $ applyCentralForce (bodyRigidBody bb1) force
|
|
|
|
) ((,) <$> (poSmallBalls physos) <*> (poBigBalls physos))
|
|
|
|
|
2017-11-07 21:22:46 +00:00
|
|
|
liftIO $ stepSimulation (pWorld phys) dt 10 Nothing
|
|
|
|
posrots <- mapM ((\ball -> do
|
|
|
|
ms <- liftIO $ getMotionState ball
|
|
|
|
npos <- liftIO $ return . fmap realToFrac =<< getPosition ms
|
|
|
|
nrot <- liftIO $ return . fmap realToFrac =<< getRotation ms
|
|
|
|
return (npos, nrot))
|
|
|
|
. bodyRigidBody) (poSmallBalls physos)
|
2018-05-17 21:42:07 +00:00
|
|
|
posrots2 <- mapM ((\ball -> do
|
|
|
|
ms <- liftIO $ getMotionState ball
|
|
|
|
npos <- liftIO $ return . fmap realToFrac =<< getPosition ms
|
|
|
|
nrot <- liftIO $ return . fmap realToFrac =<< getRotation ms
|
|
|
|
return (npos, nrot))
|
|
|
|
. bodyRigidBody) (poBigBalls physos)
|
2021-09-10 22:15:31 +00:00
|
|
|
liftIO $ atomically $ do
|
|
|
|
modifyTVar (ships sd) $ \ships ->
|
|
|
|
map (\(ship, (pos, rot)) ->
|
2017-11-07 21:22:46 +00:00
|
|
|
ship
|
|
|
|
{ shipRot = rot
|
|
|
|
, shipPos = pos
|
|
|
|
}
|
2021-09-10 22:15:31 +00:00
|
|
|
) (zip ships posrots)
|
|
|
|
modifyTVar (oplanets sd) $ \oplanets ->
|
|
|
|
map (\(ball, (pos, rot)) ->
|
2018-05-17 21:42:07 +00:00
|
|
|
ball
|
|
|
|
{ shipRot = rot
|
|
|
|
, shipPos = pos
|
|
|
|
}
|
2021-09-10 22:15:31 +00:00
|
|
|
) (zip oplanets posrots2)
|
|
|
|
liftIO $ atomically $ do
|
|
|
|
ind <- readTVar (focusIndex sd)
|
|
|
|
nplanets <- readTVar (oplanets sd)
|
|
|
|
planet <- readTVar (planet sd)
|
|
|
|
cam <- readTVar (camera sd)
|
|
|
|
writeTVar (camera sd) cam
|
|
|
|
{ cameraFocus = shipPos ((planet : nplanets) !! ind)
|
2018-05-17 21:42:07 +00:00
|
|
|
}
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
draw :: StateData -> Affection ()
|
|
|
|
draw sd = do
|
2020-01-05 14:07:27 +00:00
|
|
|
GL.viewport $= (GL.Position 0 0, GL.Size 1920 1080)
|
2021-09-10 22:15:31 +00:00
|
|
|
(planet, oplanets, ships, program, program2) <- liftIO $ do
|
|
|
|
p <- readTVarIO $ planet sd
|
|
|
|
o <- readTVarIO $ oplanets sd
|
|
|
|
s <- readTVarIO $ ships sd
|
|
|
|
pr <- readTVarIO $ program sd
|
|
|
|
pr2 <- readTVarIO $ program2 sd
|
|
|
|
return (p, o, s, pr, pr2)
|
2018-05-17 21:42:07 +00:00
|
|
|
drawThings program (planet : ships)
|
2018-05-18 05:56:11 +00:00
|
|
|
-- drawThings program (ships)
|
2018-05-17 21:42:07 +00:00
|
|
|
drawThings program2 oplanets
|
|
|
|
where
|
|
|
|
drawThings prog ts = do
|
2021-09-10 22:15:31 +00:00
|
|
|
(camera, proj, program) <- liftIO $ do
|
|
|
|
cam <- readTVarIO (camera sd)
|
|
|
|
p <- readTVarIO (proj sd)
|
|
|
|
program <- readTVarIO (program sd)
|
|
|
|
return (cam, p, program)
|
2018-05-17 21:42:07 +00:00
|
|
|
GL.currentProgram $= (Just . GLU.program $ prog)
|
|
|
|
mapM_ (\Ship{..} -> do
|
|
|
|
let view = lookAt
|
|
|
|
(cameraFocus camera +
|
|
|
|
rotVecByEulerB2A
|
|
|
|
(cameraRot camera)
|
|
|
|
(V3 0 0 (-cameraDist camera)))
|
|
|
|
(cameraFocus camera)
|
|
|
|
(V3 0 1 0)
|
|
|
|
model = mkTransformation shipRot shipPos
|
|
|
|
pvm = proj !*! view !*! model
|
|
|
|
liftIO $ GLU.setUniform program "mvp" pvm
|
|
|
|
GL.bindVertexArrayObject $= Just shipVao
|
|
|
|
liftIO $ GL.drawArrays GL.Triangles 0 (fromIntegral shipVaoLen)
|
|
|
|
) ts
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
handle :: StateData -> SDL.EventPayload -> Affection ()
|
|
|
|
handle sd (SDL.WindowClosedEvent _) = quit sd
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
handle sd (SDL.KeyboardEvent dat) = do
|
2017-11-07 21:22:46 +00:00
|
|
|
let key = SDL.keysymKeycode (SDL.keyboardEventKeysym dat)
|
|
|
|
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed) $
|
2021-09-10 22:15:31 +00:00
|
|
|
handleKey sd key
|
|
|
|
handle sd (SDL.MouseMotionEvent dat) = do
|
2017-11-07 21:22:46 +00:00
|
|
|
let (V2 rx ry) = fromIntegral <$> SDL.mouseMotionEventRelMotion dat
|
2021-09-10 22:15:31 +00:00
|
|
|
liftIO $ atomically $ modifyTVar (camera sd) $ \c ->
|
|
|
|
case SDL.mouseMotionEventState dat of
|
|
|
|
-- [SDL.ButtonRight] ->
|
|
|
|
-- let (V3 sx sy sz) = rotVecByEuler (cameraRot c) (V3 (rx / 10) 0 (ry / 10))
|
|
|
|
-- in c {cameraFocus = cameraFocus c + V3 sx 0 sy}
|
|
|
|
[] ->
|
|
|
|
let dphi = pi / 4 / 45 / 10
|
|
|
|
(Euler yaw pitch roll) = cameraRot c
|
|
|
|
nangle
|
|
|
|
| nangle' >= qc = qc - mu
|
|
|
|
| nangle' <= -qc = -qc + mu
|
|
|
|
| otherwise = nangle'
|
|
|
|
where
|
|
|
|
nangle' = (dphi * ry) + roll
|
|
|
|
qc = pi / 2
|
|
|
|
mu = 0.01
|
|
|
|
nrot =
|
|
|
|
Euler
|
|
|
|
yaw
|
|
|
|
(pitch + (rx * dphi))
|
|
|
|
nangle
|
|
|
|
in c
|
|
|
|
{ cameraRot = nrot
|
|
|
|
}
|
|
|
|
_ ->
|
|
|
|
c
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
handle _ _ = return ()
|
2017-11-07 21:22:46 +00:00
|
|
|
|
2021-09-10 22:15:31 +00:00
|
|
|
handleKey :: StateData -> SDL.Keycode -> Affection ()
|
|
|
|
handleKey sd code
|
2018-05-17 21:42:07 +00:00
|
|
|
| code == SDL.KeycodeTab = do
|
2021-09-10 22:15:31 +00:00
|
|
|
ps <- liftIO ((:) <$> readTVarIO (planet sd) <*> readTVarIO (oplanets sd))
|
|
|
|
liftIO $ atomically $ modifyTVar (focusIndex sd) $ \ind ->
|
|
|
|
if ind + 1 < length ps
|
|
|
|
then ind + 1
|
|
|
|
else 0
|
2017-11-07 21:22:46 +00:00
|
|
|
| code == SDL.KeycodeR =
|
|
|
|
GL.clearColor $= GL.Color4 1 0 0 1
|
|
|
|
| code == SDL.KeycodeG =
|
|
|
|
GL.clearColor $= GL.Color4 0 1 0 1
|
|
|
|
| code == SDL.KeycodeB =
|
|
|
|
GL.clearColor $= GL.Color4 0 0 1 1
|
|
|
|
| code == SDL.KeycodeP = do
|
|
|
|
r <- liftIO $ randomRIO (0, 1)
|
|
|
|
g <- liftIO $ randomRIO (0, 1)
|
|
|
|
b <- liftIO $ randomRIO (0, 1)
|
|
|
|
a <- liftIO $ randomRIO (0, 1)
|
|
|
|
GL.clearColor $= GL.Color4 r g b a
|
|
|
|
| code == SDL.KeycodeEscape =
|
2021-09-10 22:15:31 +00:00
|
|
|
quit sd
|
2017-11-07 21:22:46 +00:00
|
|
|
| code == SDL.KeycodeF = do
|
|
|
|
dt <- deltaTime <$> get
|
|
|
|
liftIO $ putStrLn $ show (1 / dt) ++ " FPS"
|
|
|
|
| code == SDL.KeycodeT =
|
2021-09-10 22:15:31 +00:00
|
|
|
toggleScreen 0
|
2017-11-07 21:22:46 +00:00
|
|
|
| code `elem`
|
|
|
|
[ SDL.KeycodeW
|
|
|
|
, SDL.KeycodeS
|
|
|
|
, SDL.KeycodeA
|
|
|
|
, SDL.KeycodeD
|
|
|
|
, SDL.KeycodeQ
|
|
|
|
, SDL.KeycodeE
|
|
|
|
]
|
|
|
|
= do
|
2021-09-10 22:15:31 +00:00
|
|
|
ship <- head <$> liftIO (readTVarIO $ ships sd)
|
|
|
|
let rot = shipRot ship
|
2017-11-07 21:22:46 +00:00
|
|
|
dphi = pi / 2 / 45
|
|
|
|
nquat = case code of
|
|
|
|
SDL.KeycodeW -> rot * axisAngle (V3 1 0 0) (-dphi)
|
|
|
|
SDL.KeycodeS -> rot * axisAngle (V3 1 0 0) dphi
|
|
|
|
SDL.KeycodeA -> rot * axisAngle (V3 0 1 0) (-dphi)
|
|
|
|
SDL.KeycodeD -> rot * axisAngle (V3 0 1 0) dphi
|
|
|
|
SDL.KeycodeE -> rot * axisAngle (V3 0 0 1) (-dphi)
|
|
|
|
SDL.KeycodeQ -> rot * axisAngle (V3 0 0 1) dphi
|
|
|
|
_ -> rot
|
2021-09-10 22:15:31 +00:00
|
|
|
liftIO $ atomically $ modifyTVar (ships sd) $ \ships ->
|
|
|
|
ship
|
2017-11-07 21:22:46 +00:00
|
|
|
{ shipRot = nquat
|
2021-09-10 22:15:31 +00:00
|
|
|
} : tail ships
|
2017-11-07 21:22:46 +00:00
|
|
|
| otherwise =
|
|
|
|
return ()
|