WiimotePlayer: Use IR sensor for aiming

This commit is contained in:
Matthias Schiffer 2010-04-10 13:21:04 +02:00
parent 6af7b89939
commit 083619cc87

View file

@ -8,11 +8,13 @@ import Control.Monad
import Data.Bits import Data.Bits
import Data.Function (on) import Data.Function (on)
import Data.List (sortBy) import Data.List (sortBy)
import Data.Maybe
import Data.Ratio ((%)) import Data.Ratio ((%))
import Data.Typeable import Data.Typeable
import HWiid import HWiid
import Player import Player
import Tank
data WiimotePlayer = WiimotePlayer Wiimote data WiimotePlayer = WiimotePlayer Wiimote
@ -37,29 +39,35 @@ instance Player WiimotePlayer where
ny = ((fromIntegral . extNunchukStickY $ ext) - 0x80)/0x80 ny = ((fromIntegral . extNunchukStickY $ ext) - 0x80)/0x80
in if (nx*nx + ny*ny) < 0.4 then (x, y) else (x+nx, y+ny) in if (nx*nx + ny*ny) < 0.4 then (x, y) else (x+nx, y+ny)
--ax = aimx - (fromRational . toRational . tankX $ tank) aim = handleIR state
--ay = aimy - (fromRational . toRational . tankY $ tank) aangle = if isJust aim
then
let aimx = fst $ fromJust aim
aimy = snd $ fromJust aim
ax = aimx - (fromRational . toRational . tankX $ tank)
ay = aimy - (fromRational . toRational . tankY $ tank)
in if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing
else
Nothing
move = (mx /= 0 || my /= 0) move = (mx /= 0 || my /= 0)
angle = atan2 my mx angle = atan2 my mx
moveangle = if move then Just $ fromRational $ round ((angle - (sin $ 8*x)/8)*1000000*180/pi)%1000000 else Nothing moveangle = if move then Just $ fromRational $ round ((angle - (sin $ 8*x)/8)*1000000*180/pi)%1000000 else Nothing
when foo $ print $ state
return (WiimotePlayer wiimote, moveangle, move, aangle, shoot)
--aangle = if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing
when foo $ print $ handleIR state
return (WiimotePlayer wiimote, moveangle, move, Nothing, shoot)
irXScale :: Float irXScale :: Float
irXScale = 1 irXScale = 20
irXTranslate :: Float irXTranslate :: Float
irXTranslate = 0 irXTranslate = 0 + 7
irYScale :: Float irYScale :: Float
irYScale = 1 irYScale = 20
irYTranslate :: Float irYTranslate :: Float
irYTranslate = 0 irYTranslate = -10 + 8
handleIR :: WiimoteState -> Maybe (Float, Float) handleIR :: WiimoteState -> Maybe (Float, Float)
handleIR state = handle $ sortIRSourcesByPos $ take 2 $ sortIRSourcesBySize $ stateIRSources state handleIR state = handle $ sortIRSourcesByPos $ take 2 $ sortIRSourcesBySize $ stateIRSources state