AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with IF logic for Gearshift project

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
kayl669



Joined: 05 Feb 2009
Posts: 1
Location: United States

PostPosted: Thu Feb 05, 2009 3:24 am    Post subject: Need help with IF logic for Gearshift project Reply with quote

Hi all. I want to use my joystick as a gear shifter for a racing game. I have already compiled the joystick script in AutoHotKey Help. It gives me a popup box displaying the percent of joystick x,y axis. I am using these numbers in the logic below.

Ok. Now, Here is my script:

#Persistent ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return

WatchAxis:
GetKeyState, JoyX, JoyX ; Get position of X axis.
GetKeyState, JoyY, JoyY ; Get position of Y axis.
KeyToHoldDownPrev = %KeyToHoldDown% ; Prev now holds the key that was down before

(if any).

;
; Joystick axis gear shifter project for Nascar Racing 2003 Season
;


if (JoyX < 1)and(JoyY < 1)
{
KeyToHoldDown = Numpad7
}
if (JoyX = 37)and(JoyY < 1)
{
KeyToHoldDown = Numpad8
}
if JoyY = 87
{
KeyToHoldDown = Numpad2
}
if JoyY = 42
{
KeyToHoldDown = Numpad6
}
if JoyY = 85
{
KeyToHoldDown = Numpad3
}
else KeyToHoldDown =

if KeyToHoldDown = %KeyToHoldDownPrev% ; The correct key is already down (or no key

is needed).
return ; Do nothing.

; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1 ; Avoid delays between keystrokes.
if KeyToHoldDownPrev ; There is a previous key to release.
Send, {%KeyToHoldDownPrev% up} ; Release it.
if KeyToHoldDown ; There is a key to press down.
Send, {%KeyToHoldDown% down} ; Press it down.
return

This code will not work properly and I have no idea why. Can anyone help me?
Back to top
View user's profile Send private message
elektron



Joined: 02 Oct 2008
Posts: 21

PostPosted: Thu Feb 05, 2009 4:05 pm    Post subject: Reply with quote

In the scheme of things, are you trying to do a layout of the traditional H pattern, or is it more or less a push up to shift up down to shift down type of thing?

Either way, you can use the same type of logic defined for one to use for the other, just the complexity grows as does the number of "gears" involved in your project.

This can be done a number of ways. However, I think it would be in your best interest to do a map to calibrate the joystick. Find the range of it's parameters, just to gain a relative referencepoint.

You're thinking about this pretty linearly, from what I can see anyways, while also not taking into account the variance needed. Joysticks aren't perfect, which is why I said you should REALLY be sure of the position that you're using.

The way to sort through this is through nested IF statements. But before you can do that, it's better to get the logic straight on the problem. This is how I would do it.

Code:

;;Given a box that is square, with dimensions of 100x100 we have something looking like this

--------------
-               -
-               -
-               -
--------------

;; So lets say we're only looking for whether the y is positive
;; You'd use this for up to shift up and down to shift down.
;;Break it in half and test the values


--------------
-      +y     -
--------------
-      -y      -
--------------




Code:

Loop,
{
   if  %JoyY% = 0 ;; It's centered
   {
      continue ;; No need to do anything
   }
   else if %JoyY% > 0 ;; positive,  in quadrant +y
   {
      ShiftUp
      continue ;; We're done, don't test for anything else
   }
   else if %JoyY% < 0 ;; negative, in quadrant -y
   {
      ShiftDown
      continue
   }
   else ;; wtf?
   }
      MsgBox, unexpected parameter at position 0:%JoyY%
   }
Sleep, 50 ;; So it's not so heavy...
}


From there on it's just a matter of testing for further values, and knowing what each quadrant value means.
Code:

1=-x, +y
2=+x, +y
3=-x, -y
4=+x, -y

--------------
-   1   |  2   -
--------------
-   3   |  4   -
--------------



Don't have the time to continue on, but you basically get it now. Test for one thing at a time and go from there. It's all about breaking it down into plies... expanding.
[code]


Tell me how it works for you.

-- elektron
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group