AutoHotkey Community

It is currently May 26th, 2012, 6:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: February 5th, 2009, 4:24 am 
Offline

Joined: February 5th, 2009, 4:18 am
Posts: 1
Location: United States
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 5th, 2009, 5:05 pm 
Offline

Joined: October 2nd, 2008, 8:16 pm
Posts: 21
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Mtes, rafaelloureiro, tic, Yahoo [Bot] and 72 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group