aboutsummaryrefslogtreecommitdiff
path: root/Demos/Pong/Pong.hs
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-11-18 17:42:20 -0800
committer3gg <3gg@shellblade.net>2024-11-18 17:42:20 -0800
commitea4100bd02143fea255cc0441cbeeb842796a58a (patch)
tree31eb20e30132db2886491a3985c379f7ead9e9a2 /Demos/Pong/Pong.hs
parentfe00d4135a66b1a8a9dd7b2c03a74727b3f644b2 (diff)
Fix momentum.
Diffstat (limited to 'Demos/Pong/Pong.hs')
-rw-r--r--Demos/Pong/Pong.hs16
1 files changed, 8 insertions, 8 deletions
diff --git a/Demos/Pong/Pong.hs b/Demos/Pong/Pong.hs
index dd8855b..b9661ee 100644
--- a/Demos/Pong/Pong.hs
+++ b/Demos/Pong/Pong.hs
@@ -31,7 +31,7 @@ initialBallVelocity = vec2 1 1
31maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) 31maxBounceAngle = (65::Float) * (pi::Float)/(180::Float)
32playerSpeed = 1.0 :: Float 32playerSpeed = 1.0 :: Float
33enemySpeed = 7.0 :: Float 33enemySpeed = 7.0 :: Float
34enemyMomentum = 0.1 :: Float 34enemyMomentum = 1.0 :: Float
35initialEnemyPos = vec2 0.5 0.9 35initialEnemyPos = vec2 0.5 0.9
36initialPlayerPos = vec2 0.5 0.1 36initialPlayerPos = vec2 0.5 0.1
37initialBallPos = vec2 0.5 0.5 37initialBallPos = vec2 0.5 0.5
@@ -150,13 +150,13 @@ moveBall = step $ \_ dt _ _ (vel, ball) -> (translate (vel * dt) ball, moveBall)
150stepEnemy = movePad 0 .> clamp 150stepEnemy = movePad 0 .> clamp
151 151
152movePad :: Float -> Step [GameObject] e GameObject GameObject 152movePad :: Float -> Step [GameObject] e GameObject GameObject
153movePad previousMomentum = step $ \_ dt gos _ pad -> 153movePad previousMomentumVector = step $ \_ dt gos _ pad ->
154 let ball = head gos 154 let ball = head gos
155 offset = (x . position $ ball) - (x . position $ pad) 155 heading = (x . position $ ball) - (x . position $ pad)
156 chaseVector = enemySpeed * offset 156 chaseVector = enemySpeed * heading
157 momentum = previousMomentum + enemyMomentum * chaseVector 157 momentumVector = previousMomentumVector + enemyMomentum * heading * dt
158 vx = chaseVector + momentum 158 vx = chaseVector * dt + momentumVector
159 in (translate (vec2 (vx * dt) 0) pad, movePad momentum) 159 in (translate (vec2 vx 0) pad, movePad momentumVector)
160 160
161sign :: Float -> Float 161sign :: Float -> Float
162sign x = if x >= 0 then 1 else -1 162sign x = if x >= 0 then 1 else -1