From e2b76464e847e5c656e6adf6d8e07a054756cda0 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 30 Dec 2024 20:59:27 -0800 Subject: Use bounding volume in other places. --- Demos/Pong/Main.hs | 9 ++++----- Demos/Pong/Pong.hs | 40 ++++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Demos/Pong/Main.hs b/Demos/Pong/Main.hs index df90020..b93325d 100644 --- a/Demos/Pong/Main.hs +++ b/Demos/Pong/Main.hs @@ -9,6 +9,7 @@ import Spear.Math.Matrix4 as Matrix4 hiding (position) import Spear.Math.Spatial import Spear.Math.Spatial2 import Spear.Math.Vector +import Spear.Physics.Collision import Spear.Render.Core.Pipeline import Spear.Render.Core.State import Spear.Render.Immediate @@ -101,11 +102,9 @@ renderBackground = ,vec2 pmin pmax)] renderGO :: GameObject -> Game ImmRenderState () -renderGO go = do - let (AABB2 (Vector2 xmin ymin) (Vector2 xmax ymax)) = aabb go - (Vector2 xcenter ycenter) = position go - immPreservingMatrix $ do - immTranslate (vec3 xcenter ycenter 0) +renderGO go = + let (AABB2Volume (AABB2 (Vector2 xmin ymin) (Vector2 xmax ymax))) = boundingVolume go + in immDrawQuads2d [ (vec2 xmin ymin ,vec2 xmax ymin diff --git a/Demos/Pong/Pong.hs b/Demos/Pong/Pong.hs index b12f792..790a98e 100644 --- a/Demos/Pong/Pong.hs +++ b/Demos/Pong/Pong.hs @@ -3,12 +3,11 @@ {-# LANGUAGE TypeSynonymInstances #-} module Pong - ( GameEvent (..), - GameObject, - newWorld, - stepWorld, - aabb, - ) +( GameEvent (..) +, GameObject +, newWorld +, stepWorld +) where import Spear.Math.AABB @@ -25,8 +24,8 @@ import Data.Monoid (mconcat) -- Configuration -padSize = vec2 0.07 0.015 -ballSize = 0.012 :: Float +padSize = vec2 0.070 0.015 +ballSize = vec2 0.012 0.012 ballSpeed = 0.7 :: Float initialBallVelocity = vec2 1 1 maxBounceAngle = (65::Float) * (pi::Float)/(180::Float) @@ -54,10 +53,10 @@ data GameObjectId deriving (Eq, Show) data GameObject = GameObject - { gameObjectId :: !GameObjectId - , aabb :: {-# UNPACK #-} !AABB2 - , basis :: {-# UNPACK #-} !Transform2 - , gostep :: Step [GameObject] [GameEvent] GameObject GameObject + { gameObjectId :: !GameObjectId + , gameObjectSize :: {-# UNPACK #-} !Vector2 + , basis :: {-# UNPACK #-} !Transform2 + , gostep :: Step [GameObject] [GameEvent] GameObject GameObject } @@ -88,17 +87,14 @@ instance Spatial GameObject Vector2 Angle Transform2 where instance Bounded2 GameObject where - boundingVolume obj = aabb2Volume $ translate (position obj) (aabb obj) + boundingVolume obj = aabb2Volume $ translate (position obj) (AABB2 (-size) size) + where size = gameObjectSize obj -ballBox, padBox :: AABB2 -ballBox = AABB2 (vec2 (-s) (-s)) (vec2 s s) where s = ballSize -padBox = AABB2 (-padSize) padSize - newWorld = - [ GameObject Ball ballBox (makeAt initialBallPos) $ stepBall initialBallVelocity, - GameObject Enemy padBox (makeAt initialEnemyPos) stepEnemy, - GameObject Player padBox (makeAt initialPlayerPos) stepPlayer + [ GameObject Ball ballSize (makeAt initialBallPos) $ stepBall initialBallVelocity, + GameObject Enemy padSize (makeAt initialEnemyPos) stepEnemy, + GameObject Player padSize (makeAt initialPlayerPos) stepPlayer ] where makeAt = newTransform2 unitx2 unity2 @@ -129,7 +125,7 @@ stepBall vel = bounceBall vel .> moveBall bounceBall :: Vector2 -> Step [GameObject] [GameEvent] GameObject (Vector2, GameObject) bounceBall vel = step $ \_ dt gos events ball -> - let (AABB2 pmin pmax) = translate (position ball) (aabb ball) + let (AABB2Volume (AABB2 pmin pmax)) = boundingVolume ball sideCollision = x pmin < 0 || x pmax > 1 backCollision = y pmin < 0 || y pmax > 1 flipX v@(Vector2 x y) = if sideCollision then vec2 (-x) y else v @@ -150,7 +146,7 @@ paddleBounce ball events vel paddle = let collision = Collision Ball (gameObjectId paddle) `elem` events in if collision then - let (AABB2 pmin pmax) = translate (position paddle) (aabb paddle) + let (AABB2Volume (AABB2 pmin pmax)) = boundingVolume paddle center = (x pmin + x pmax) / (2::Float) -- Normalized offset of the ball from the paddle's center, [-1, +1]. -- It's outside the [-1, +1] range if there is no collision. -- cgit v1.2.3