Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // My best IR Mouse Script, with 5DOF Tracking
- // By Carl Kenner
- //Edited for MAME 2P/ppjoy by Bakou
- // Change these values for wiimote1:
- var.SensorBarSeparation = 8.0 inches // distance between middles of two sensor bar dots
- var.NoYawAllowed = true // Calculates X if no yaw is allowed, otherwise calculates Yaw but not X
- var.IRMulX = 1.0
- var.IRMulY = 1.0
- var.IROffsetX = 0 // add to mouse.x
- var.IROffsetY = 0 // add to mouse.y
- var.IRLeftButton = Wiimote1.A
- var.IRRightButton = Wiimote1.B
- var.FreezeTime = 25ms
- // change these values for wiimote2
- var.SecondSensorBarSeparation = 8.0 inches // distance between middles of two sensor bar dots
- var.SecondNoYawAllowed = true // Calculates X if no yaw is allowed, otherwise calculates Yaw but not X
- var.SecondIRMulX = 1.2
- var.SecondIRMulY = 1.2
- var.SecondIROffsetX = 0 // add to mouse.x
- var.SecondIROffsetY = 0 // add to mouse.y
- var.SecondIRLeftButton = Wiimote2.A
- var.SecondIRRightButton = Wiimote2.B
- var.SecondFreezeTime = 25ms
- // Code for wiimote 1 ------------------------------------------------------------------------------------
- // Compensate for roll
- var.c = cos(Smooth(wiimote1.roll, 10))
- var.s = sin(Smooth(wiimote1.roll, 10))
- if wiimote1.dot1vis then
- var.dot1x = var.c*(511.5-wiimote1.dot1x)/511.5 - var.s*(wiimote1.dot1y-383.5)/511.5
- var.dot1y = var.s*(511.5-wiimote1.dot1x)/511.5 + var.c*(wiimote1.dot1y-383.5)/511.5
- end if
- if wiimote1.dot2vis then
- var.dot2x = var.c*(511.5-wiimote1.dot2x)/511.5 - var.s*(wiimote1.dot2y-383.5)/511.5
- var.dot2y = var.s*(511.5-wiimote1.dot2x)/511.5 + var.c*(wiimote1.dot2y-383.5)/511.5
- end if
- // if both dots are visible check which is which and how far apart
- if wiimote1.dot1vis and wiimote1.dot2vis then
- if var.dot1x <= var.dot2x then
- var.leftdot = 1
- var.dotdeltay = var.dot2y - var.dot1y
- else
- var.leftdot = 2
- var.dotdeltay = var.dot1y - var.dot2y
- end if
- var.dotdeltax = abs(var.dot1x-var.dot2x)
- var.DotSep = hypot(var.dotdeltax, var.dotdeltay) * 511.5
- var.IRDistance = var.SensorBarSeparation * 1320 / var.DotSep
- end if
- // sort out the position of the left and right dots
- if var.leftdot = 1 then
- if wiimote1.dot1vis and wiimote1.dot2vis then
- var.LeftDotX = var.dot1x
- var.LeftDotY = var.dot1y
- var.LeftDotVis = true
- var.RightDotX = var.dot2x
- var.RightDotY = var.dot2y
- var.RightDotVis = true
- else if wiimote1.dot1vis then
- if hypot(var.leftdotx-var.dot1x,var.leftdoty-var.dot1y) <= hypot(var.rightdotx-var.dot1x,var.rightdoty-var.dot1y) then
- // is the real dot 1
- var.LeftDotX = var.dot1x
- var.LeftDotY = var.dot1y
- var.RightDotX = var.dot1x + var.dotdeltax
- var.RightDotY = var.dot1y + var.dotdeltay
- var.LeftDotVis = true
- var.RightDotVis = false
- else
- // was originally dot 2, but now called dot 1.
- var.leftdot = 2 // this dot (1) is actually the right dot
- var.LeftDotX = var.dot1x - var.dotdeltax
- var.LeftDotY = var.dot1y - var.dotdeltay
- var.RightDotX = var.dot1x
- var.RightDotY = var.dot1y
- var.RightDotVis = true
- var.LeftDotVis = false
- end if
- else if wiimote1.dot2vis then
- var.LeftDotX = var.dot2x - var.dotdeltax
- var.LeftDotY = var.dot2y - var.dotdeltay
- var.RightDotX = var.dot2x
- var.RightDotY = var.dot2y
- var.RightDotVis = true
- var.LeftDotVis = false
- end if
- else if var.leftdot = 2 then
- if wiimote1.dot1vis and wiimote1.dot2vis then
- var.LeftDotX = var.dot2x
- var.LeftDotY = var.dot2y
- var.LeftDotVis = true
- var.RightDotX = var.dot1x
- var.RightDotY = var.dot1y
- var.RightDotVis = true
- else if wiimote1.dot1vis then
- if hypot(var.leftdotx-var.dot1x,var.leftdoty-var.dot1y) <= hypot(var.rightdotx-var.dot1x,var.rightdoty-var.dot1y) then
- var.leftdot = 1 // dot 1 is now the left dot
- var.LeftDotX = var.dot1x
- var.LeftDotY = var.dot1y
- var.RightDotX = var.dot1x + var.dotdeltax
- var.RightDotY = var.dot1y + var.dotdeltay
- var.LeftDotVis = true
- var.RightDotVis = false
- else
- // the real dot 1 (on the right)
- var.LeftDotX = var.dot1x - var.dotdeltax
- var.LeftDotY = var.dot1y - var.dotdeltay
- var.RightDotX = var.dot1x
- var.RightDotY = var.dot1y
- var.RightDotVis = true
- var.LeftDotVis = false
- end if
- else if wiimote1.dot2vis then
- var.RightDotX = var.dot2x + var.dotdeltax
- var.RightDotY = var.dot2y + var.dotdeltay
- var.LeftDotX = var.dot2x
- var.LeftDotY = var.dot2y
- var.LeftDotVis = true
- var.RightDotVis = false
- end if
- else
- var.LeftDotX = var.dot1x
- var.LeftDotY = var.dot1y
- var.RightDotX = var.LeftDotX
- var.RightDotY = var.LeftDotY
- var.LeftDotVis = true
- var.RightDotVis = true
- end if
- // Find the imaginary middle dot
- var.MiddleDotX = (var.leftdotx + var.rightdotx)/2
- var.MiddleDotY = (var.leftdoty + var.rightdoty)/2
- var.MiddleDotVis = wiimote1.dot1vis or wiimote1.dot2vis
- if var.MiddleDotVis then
- var.TotalPitch = atan2(511.5*var.MiddleDotY,1320) + Wiimote.Pitch
- var.DotYaw = atan2(-511.5*var.MiddleDotX,1320) // assume yaw is 0
- var.WiimoteYawNoX = atan2(511.5*var.MiddleDotX,1320)
- var.WiimoteXNoYaw = -sin(var.dotyaw)*var.IRDistance
- var.WiimoteY = -sin(var.totalpitch)*var.IRDistance
- var.WiimoteZ = (-sqrt(sqr(var.IRDistance) - sqr(var.WiimoteY)))*var.IRDistance/RemoveUnits(var.IRDistance)
- end if
- // scale it to the screen range 0 to 1
- var.IRx = var.IRMulX*var.middledotx/2 + 0.5
- var.IRy = var.IRMulY*var.middledoty*1023/767/2 + 0.5
- var.IRvis = wiimote1.dot1vis or wiimote1.dot2vis
- var.IROnScreen = 0 <= var.IRx <= 1 and 0 <= var.IRy <= 1
- // is it off the screen?
- var.IRTooFarLeft = var.IRx < 0 or (var.IRx < 0.1 and (not var.IRvis))
- var.IRTooFarRight = var.IRx > 1 or (var.IRx > 1-0.1 and (not var.IRvis))
- var.IRTooFarUp = var.IRy < 0 or (var.IRy < 0.1 and (not var.IRvis))
- var.IRTooFarDown = var.IRy > 1 or (var.IRy > 1-0.1 and (not var.IRvis))
- // Heavily smooth small movements, but do zero lag for quick movements
- var.MoveAmount = 1024*hypot(delta(var.IRx), delta(var.IRy))
- if smooth(var.MoveAmount) > 12 then
- var.SmoothX = var.IRx
- var.SmoothY = var.IRy
- var.LastSureFrame = PIE.Frame
- else if (PIE.frame-var.LastSureFrame) > 18 then
- var.SmoothX = Smooth(var.IRx, 18, 4/1024)
- var.SmoothY = Smooth(var.IRy, 18, 4/1024)
- else if (PIE.frame-var.LastSureFrame) > 14 then
- var.SmoothX = Smooth(var.IRx, 14, 4/1024)
- var.SmoothY = Smooth(var.IRy, 14, 4/1024)
- else if (PIE.frame-var.LastSureFrame) > 10 then
- var.SmoothX = Smooth(var.IRx, 10, 4/1024)
- var.SmoothY = Smooth(var.IRy, 10, 4/1024)
- else if (PIE.frame-var.LastSureFrame) > 6 then
- var.SmoothX = Smooth(var.IRx, 6, 4/1024)
- var.SmoothY = Smooth(var.IRy, 6, 4/1024)
- else if (PIE.frame-var.LastSureFrame) > 2 then
- var.SmoothX = Smooth(var.IRx, 2, 4/1024)
- var.SmoothY = Smooth(var.IRy, 2, 4/1024)
- end if
- // Freeze the mouse cursor while they start pressing the button
- // otherwise it will make the cursor jump
- var.Freeze = (var.IRLeftButton or var.IRRightButton) and KeepDown(pressed(var.IRLeftButton) or pressed(var.IRRightButton), var.FreezeTime)
- // Only change the mouse position if pointing at the screen
- // otherwise they can still use a real mouse
- if var.IRvis and (not var.Freeze) then
- // convert to joystick coordinates
- ppjoy0.Analog0 = (var.SmoothX - 0.5 ) * 2.0
- ppjoy0.Analog1 = (var.SmoothY - 0.5 ) * 2.0
- end if
- // delay the buttons slightly so we have time to freeze the cursor (is that needed?)
- ppjoy0.Digital2 = var.IRLeftButton and (not KeepDown(pressed(var.IRLeftButton), 40ms))
- ppjoy0.Digital0 = var.IRRightButton and (not KeepDown(pressed(var.IRRightButton), 40ms))
- ppjoy0.Digital1 = wiimote1.one
- ppjoy0.Digital3 = wiimote1.two
- ppjoy0.Digital4 = wiimote1.Plus
- ppjoy0.Digital5 = wiimote1.Minus
- ppjoy0.Digital6 = wiimote1.Home
- ppjoy0.Digital7 = wiimote1.Up
- ppjoy0.Digital8 = wiimote1.Down
- ppjoy0.Digital9 = wiimote1.Left
- ppjoy0.Digital10 = wiimote1.Right
- // Code for wiimote #2 ------------------------------------------------------------------------------------
- // Compensate for roll
- var.Secondc = cos(Smooth(wiimote2.roll, 10))
- var.Seconds = sin(Smooth(wiimote2.roll, 10))
- if wiimote2.dot1vis then
- var.Seconddot1x = var.Secondc*(511.5-wiimote2.dot1x)/511.5 - var.Seconds*(wiimote2.dot1y-383.5)/511.5
- var.Seconddot1y = var.Seconds*(511.5-wiimote2.dot1x)/511.5 + var.Secondc*(wiimote2.dot1y-383.5)/511.5
- end if
- if wiimote2.dot2vis then
- var.Seconddot2x = var.Secondc*(511.5-wiimote2.dot2x)/511.5 - var.Seconds*(wiimote2.dot2y-383.5)/511.5
- var.Seconddot2y = var.Seconds*(511.5-wiimote2.dot2x)/511.5 + var.Secondc*(wiimote2.dot2y-383.5)/511.5
- end if
- // if both dots are visible check which is which and how far apart
- if wiimote2.dot1vis and wiimote2.dot2vis then
- if var.Seconddot1x <= var.Seconddot2x then
- var.Secondleftdot = 1
- var.Seconddotdeltay = var.Seconddot2y - var.Seconddot1y
- else
- var.Secondleftdot = 2
- var.Seconddotdeltay = var.Seconddot1y - var.Seconddot2y
- end if
- var.Seconddotdeltax = abs(var.Seconddot1x-var.Seconddot2x)
- var.SecondDotSep = hypot(var.Seconddotdeltax, var.Seconddotdeltay) * 511.5
- var.SecondIRDistance = var.SecondSensorBarSeparation * 1320 / var.SecondDotSep
- end if
- // sort out the position of the left and right dots
- if var.Secondleftdot = 1 then
- if wiimote2.dot1vis and wiimote2.dot2vis then
- var.SecondLeftDotX = var.Seconddot1x
- var.SecondLeftDotY = var.Seconddot1y
- var.SecondLeftDotVis = true
- var.SecondRightDotX = var.Seconddot2x
- var.SecondRightDotY = var.Seconddot2y
- var.SecondRightDotVis = true
- else if wiimote2.dot1vis then
- if hypot(var.Secondleftdotx-var.Seconddot1x,var.Secondleftdoty-var.Seconddot1y) <= hypot(var.Secondrightdotx-var.Seconddot1x,var.Secondrightdoty-var.Seconddot1y) then
- // is the real dot 1
- var.SecondLeftDotX = var.Seconddot1x
- var.SecondLeftDotY = var.Seconddot1y
- var.SecondRightDotX = var.Seconddot1x + var.Seconddotdeltax
- var.SecondRightDotY = var.Seconddot1y + var.Seconddotdeltay
- var.SecondLeftDotVis = true
- var.SecondRightDotVis = false
- else
- // was originally dot 2, but now called dot 1.
- var.Secondleftdot = 2 // this dot (1) is actually the right dot
- var.SecondLeftDotX = var.Seconddot1x - var.Seconddotdeltax
- var.SecondLeftDotY = var.Seconddot1y - var.Seconddotdeltay
- var.SecondRightDotX = var.Seconddot1x
- var.SecondRightDotY = var.Seconddot1y
- var.SecondRightDotVis = true
- var.SecondLeftDotVis = false
- end if
- else if wiimote2.dot2vis then
- var.SecondLeftDotX = var.Seconddot2x - var.Seconddotdeltax
- var.SecondLeftDotY = var.Seconddot2y - var.Seconddotdeltay
- var.SecondRightDotX = var.Seconddot2x
- var.SecondRightDotY = var.Seconddot2y
- var.SecondRightDotVis = true
- var.SecondLeftDotVis = false
- end if
- else if var.Secondleftdot = 2 then
- if wiimote2.dot1vis and wiimote2.dot2vis then
- var.SecondLeftDotX = var.Seconddot2x
- var.SecondLeftDotY = var.Seconddot2y
- var.SecondLeftDotVis = true
- var.SecondRightDotX = var.Seconddot1x
- var.SecondRightDotY = var.Seconddot1y
- var.SecondRightDotVis = true
- else if wiimote2.dot1vis then
- if hypot(var.Secondleftdotx-var.Seconddot1x,var.Secondleftdoty-var.Seconddot1y) <= hypot(var.Secondrightdotx-var.Seconddot1x,var.Secondrightdoty-var.Seconddot1y) then
- var.Secondleftdot = 1 // dot 1 is now the left dot
- var.SecondLeftDotX = var.Seconddot1x
- var.SecondLeftDotY = var.Seconddot1y
- var.SecondRightDotX = var.Seconddot1x + var.Seconddotdeltax
- var.SecondRightDotY = var.Seconddot1y + var.Seconddotdeltay
- var.SecondLeftDotVis = true
- var.SecondRightDotVis = false
- else
- // the real dot 1 (on the right)
- var.SecondLeftDotX = var.Seconddot1x - var.Seconddotdeltax
- var.SecondLeftDotY = var.Seconddot1y - var.Seconddotdeltay
- var.SecondRightDotX = var.Seconddot1x
- var.SecondRightDotY = var.Seconddot1y
- var.SecondRightDotVis = true
- var.SecondLeftDotVis = false
- end if
- else if wiimote2.dot2vis then
- var.SecondRightDotX = var.Seconddot2x + var.Seconddotdeltax
- var.SecondRightDotY = var.Seconddot2y + var.Seconddotdeltay
- var.SecondLeftDotX = var.Seconddot2x
- var.SecondLeftDotY = var.Seconddot2y
- var.SecondLeftDotVis = true
- var.SecondRightDotVis = false
- end if
- else
- var.SecondLeftDotX = var.Seconddot1x
- var.SecondLeftDotY = var.Seconddot1y
- var.SecondRightDotX = var.SecondLeftDotX
- var.SecondRightDotY = var.SecondLeftDotY
- var.SecondLeftDotVis = true
- var.SecondRightDotVis = true
- end if
- // Find the imaginary middle dot
- var.SecondMiddleDotX = (var.Secondleftdotx + var.Secondrightdotx)/2
- var.SecondMiddleDotY = (var.Secondleftdoty + var.Secondrightdoty)/2
- var.SecondMiddleDotVis = wiimote2.dot1vis or wiimote2.dot2vis
- if var.SecondMiddleDotVis then
- var.SecondTotalPitch = atan2(511.5*var.SecondMiddleDotY,1320) + Wiimote.Pitch
- var.SecondDotYaw = atan2(-511.5*var.SecondMiddleDotX,1320) // assume yaw is 0
- var.SecondWiimoteYawNoX = atan2(511.5*var.SecondMiddleDotX,1320)
- var.SecondWiimoteXNoYaw = -sin(var.Seconddotyaw)*var.SecondIRDistance
- var.SecondWiimoteY = -sin(var.Secondtotalpitch)*var.SecondIRDistance
- var.SecondWiimoteZ = (-sqrt(sqr(var.SecondIRDistance) - sqr(var.SecondWiimoteY)))*var.SecondIRDistance/RemoveUnits(var.SecondIRDistance)
- end if
- // scale it to the screen range 0 to 1
- var.SecondIRx = var.SecondIRMulX*var.Secondmiddledotx/2 + 0.5
- var.SecondIRy = var.SecondIRMulY*var.Secondmiddledoty*1023/767/2 + 0.5
- var.SecondIRvis = wiimote2.dot1vis or wiimote2.dot2vis
- var.SecondIROnScreen = 0 <= var.SecondIRx <= 1 and 0 <= var.SecondIRy <= 1
- // is it off the screen?
- var.SecondIRTooFarLeft = var.SecondIRx < 0 or (var.SecondIRx < 0.1 and (not var.SecondIRvis))
- var.SecondIRTooFarRight = var.SecondIRx > 1 or (var.SecondIRx > 1-0.1 and (not var.SecondIRvis))
- var.SecondIRTooFarUp = var.SecondIRy < 0 or (var.SecondIRy < 0.1 and (not var.SecondIRvis))
- var.SecondIRTooFarDown = var.SecondIRy > 1 or (var.SecondIRy > 1-0.1 and (not var.SecondIRvis))
- // Heavily smooth small movements, but do zero lag for quick movements
- var.SecondMoveAmount = 1024*hypot(delta(var.SecondIRx), delta(var.SecondIRy))
- if smooth(var.SecondMoveAmount) > 12 then
- var.SecondSmoothX = var.SecondIRx
- var.SecondSmoothY = var.SecondIRy
- var.SecondLastSureFrame = PIE.Frame
- else if (PIE.frame-var.SecondLastSureFrame) > 18 then
- var.SecondSmoothX = Smooth(var.SecondIRx, 18, 4/1024)
- var.SecondSmoothY = Smooth(var.SecondIRy, 18, 4/1024)
- else if (PIE.frame-var.SecondLastSureFrame) > 14 then
- var.SecondSmoothX = Smooth(var.SecondIRx, 14, 4/1024)
- var.SecondSmoothY = Smooth(var.SecondIRy, 14, 4/1024)
- else if (PIE.frame-var.SecondLastSureFrame) > 10 then
- var.SecondSmoothX = Smooth(var.SecondIRx, 10, 4/1024)
- var.SecondSmoothY = Smooth(var.SecondIRy, 10, 4/1024)
- else if (PIE.frame-var.SecondLastSureFrame) > 6 then
- var.SecondSmoothX = Smooth(var.SecondIRx, 6, 4/1024)
- var.SecondSmoothY = Smooth(var.SecondIRy, 6, 4/1024)
- else if (PIE.frame-var.SecondLastSureFrame) > 2 then
- var.SecondSmoothX = Smooth(var.SecondIRx, 2, 4/1024)
- var.SecondSmoothY = Smooth(var.SecondIRy, 2, 4/1024)
- end if
- // Freeze the mouse cursor while they start pressing the button
- // otherwise it will make the cursor jump
- var.SecondFreeze = (var.SecondIRLeftButton or var.SecondIRRightButton) and KeepDown(pressed(var.SecondIRLeftButton) or pressed(var.SecondIRRightButton), var.SecondFreezeTime)
- // Only change the mouse position if pointing at the screen
- // otherwise they can still use a real mouse
- if var.SecondIRvis and (not var.SecondFreeze) then
- // convert to joystick coordinates
- ppjoy2.Analog0 = (var.SecondSmoothX - 0.5 ) * 2.0
- ppjoy2.Analog1 = (var.SecondSmoothY - 0.5 ) * 2.0
- end if
- // delay the buttons slightly so we have time to freeze the cursor (is that needed?)
- ppjoy2.Digital2 = var.SecondIRLeftButton and (not KeepDown(pressed(var.SecondIRLeftButton), 40ms))
- ppjoy2.Digital0 = var.SecondIRRightButton and (not KeepDown(pressed(var.SecondIRRightButton), 40ms))
- ppjoy2.Digital1 = wiimote2.one
- ppjoy2.Digital3 = wiimote2.two
- ppjoy2.Digital4 = wiimote2.Plus
- ppjoy2.Digital5 = wiimote2.Minus
- ppjoy2.Digital6 = wiimote2.Home
- ppjoy2.Digital7 = wiimote2.Up
- ppjoy2.Digital8 = wiimote2.Down
- ppjoy2.Digital9 = wiimote2.Left
- ppjoy2.Digital10 = wiimote2.Right
- debug = 'Joy0: ' + ppjoy0.Analog0 + ' ' + ppjoy0.Analog1 + 'Joy2: ' + ppjoy2.Analog0 + ' ' + ppjoy2.Analog1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement