November 28, 2020, 11:20:07 AM

Author Topic: [bb] Xbox 360 Controller Input by xtremegamr [ 1+ years ago ]  (Read 642 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Xbox 360 Controller Input
Author : xtremegamr
Posted : 1+ years ago

Description : It works pretty well, but there are a few drawbacks:

-If you hold down both triggers, the functions ReturnTrigger() and TriggerDown() with both return neutral.

-The Right Stick functions don't really work that well. I'm open to suggestions on how to make it work.


Code :
Code: BlitzBasic
  1. ;Xbox 360 Controller Library
  2. ;Programmed by xtremegamr
  3.  
  4. ;All Functions have X360_ before them
  5.  
  6. ;LeftStickX#()
  7. ;returns the x position of the Left Stick
  8. Function X360_LeftStickX#(port)
  9.  
  10. If port=-1 Then
  11.         returnvalue#=JoyX()
  12. Else
  13.         returnvalue#=JoyX(port)
  14. End If
  15.  
  16. Return returnvalue#
  17.  
  18. End Function
  19.  
  20. ;LeftStickY#()
  21. ;returns the y position of the Left Stick
  22. Function X360_LeftStickY#(port)
  23.  
  24. If port=-1 Then
  25.         returnvalue#=JoyY()
  26. Else
  27.         returnvalue#=JoyY(port)
  28. End If
  29.  
  30. Return returnvalue#
  31.  
  32. End Function
  33.  
  34. ;LeftStickXDir()
  35. ;returns the x direction of the Left Stick
  36. Function X360_LeftStickXDir(port)
  37.  
  38. If port=-1 Then
  39.         returnvalue=JoyXDir()
  40. Else
  41.         returnvalue=JoyXDir(port)
  42. End If
  43.  
  44. Return returnvalue
  45.  
  46. End Function
  47.  
  48. ;LeftStickYDir()
  49. ;return the y direction of the Left Stick
  50. Function X360_LeftStickYDir(port)
  51.  
  52. If port=-1 Then
  53.         returnvalue=JoyYDir()
  54. Else
  55.         returnvalue=JoyYDir(port)
  56. End If
  57.  
  58. Return returnvalue
  59.  
  60. End Function
  61.  
  62. ;RightStickX#()
  63. ;returns the x position of the Right Stick
  64. Function X360_RightStickX#(port)
  65.  
  66. If port=-1 Then
  67.         returnvalue=JoyPitch()
  68. Else
  69.         returnvalue=JoyPitch(port)
  70. End If
  71.  
  72. Return returnvalue
  73.  
  74. End Function
  75.  
  76. ;RightStickY#()
  77. ;returns the y position of the Right Stick
  78. Function X360_RightStickY#(port)
  79.  
  80. If port=-1 Then
  81.         returnvalue=JoyYaw()
  82. Else
  83.         returnvalue=JoyYaw(port)
  84. End If
  85.  
  86. Return returnvalue
  87.  
  88. End Function
  89.  
  90. ;RightStickXDir#()
  91. ;returns the x direction of the Right Stick
  92. Function X360_RightStickXDir(neutral,port)
  93.  
  94. If port=-1 Then
  95.         If JoyPitch()<neutral Then returnvalue=-1
  96.         If JoyPitch()>neutral Then returnvalue=1
  97. Else
  98.         If JoyPitch(port)<neutral Then returnvalue=-1
  99.         If JoyPitch(port)>neutral Then returnvalue=1
  100. End If
  101.  
  102. Return returnvalue
  103.  
  104. End Function
  105.  
  106. ;RightStickYDir#()
  107. ;returns the y direction of the Right Stick
  108. Function X360_RightStickYDir(neutral,port)
  109.  
  110. If port=-1 Then
  111.         If JoyYaw()<neutral Then returnvalue=-1
  112.         If JoyYaw()>neutral Then returnvalue=1
  113. Else
  114.         If JoyYaw(port)<neutral Then returnvalue=-1
  115.         If JoyYaw(port)>neutral Then returnvalue=1
  116. End If
  117.  
  118. Return returnvalue
  119.  
  120. End Function
  121.  
  122. ;ReturnTrigger#()
  123. ;returns trigger value (joyz)
  124. Function X360_ReturnTrigger#(port)
  125.  
  126. If port=-1 Then
  127.         returnvalue#=JoyZ()
  128. Else
  129.         returnvalue#=JoyZ(port)
  130. End If
  131.  
  132. Return returnvalue#
  133.  
  134. End Function
  135.  
  136. ;TriggerDown()
  137. ;returns the value of the current trigger that is down
  138. Function X360_TriggerDown(port)
  139.  
  140. If port=-1 Then
  141.         z=JoyZDir()
  142. Else
  143.         z=JoyZDir(port)
  144. End If
  145.  
  146. ;return
  147. Return z
  148.  
  149. End Function
  150.  
  151. ;AButtonHit()
  152. ;returns true is the a button was hit
  153. Function X360_AButtonHit(port)
  154.  
  155. If port=-1 Then
  156.         If JoyHit(1) Then Return True
  157. Else
  158.         If JoyHit(1,port) Then Return True
  159. End If
  160.  
  161. Return False
  162.  
  163. End Function
  164.  
  165. ;BButtonHit()
  166. ;returns true is the b button was hit
  167. Function X360_BButtonHit(port)
  168.  
  169. If port=-1 Then
  170.         If JoyHit(2) Then Return True
  171. Else
  172.         If JoyHit(2,port) Then Return True
  173. End If
  174.  
  175. Return False
  176.  
  177. End Function
  178.  
  179. ;YButtonHit()
  180. ;returns true is the y button was hit
  181. Function X360_YButtonHit(port)
  182.  
  183. If port=-1 Then
  184.         If JoyHit(4) Then Return True
  185. Else
  186.         If JoyHit(4,port) Then Return True
  187. End If
  188.  
  189. Return False
  190.  
  191. End Function
  192.  
  193. ;XButtonHit()
  194. ;returns true is the x button was hit
  195. Function X360_XButtonHit(port)
  196.  
  197. If port=-1 Then
  198.         If JoyHit(3) Then Return True
  199. Else
  200.         If JoyHit(3,port) Then Return True
  201. End If
  202.  
  203. Return False
  204.  
  205. End Function
  206.  
  207. ;RBButtonHit()
  208. ;returns true is the rbumper was hit
  209. Function X360_RBButtonHit(port)
  210.  
  211. If port=-1 Then
  212.         If JoyHit(6) Then Return True
  213. Else
  214.         If JoyHit(6,port) Then Return True
  215. End If
  216.  
  217. Return False
  218.  
  219. End Function
  220.  
  221. ;LBButtonHit()
  222. ;returns true is the lbumper was hit
  223. Function X360_LBButtonHit(port)
  224.  
  225. If port=-1 Then
  226.         If JoyHit(5) Then Return True
  227. Else
  228.         If JoyHit(5,port) Then Return True
  229. End If
  230.  
  231. Return False
  232.  
  233. End Function
  234.  
  235. ;StartButtonHit()
  236. ;returns true is the start button was hit
  237. Function X360_StartButtonHit(port)
  238.  
  239. If port=-1 Then
  240.         If JoyHit(8) Then Return True
  241. Else
  242.         If JoyHit(8,port) Then Return True
  243. End If
  244.  
  245. Return False
  246.  
  247. End Function
  248.  
  249. ;BackButtonHit()
  250. ;returns true is the x button was hit
  251. Function X360_BackButtonHit(port)
  252.  
  253. If port=-1 Then
  254.         If JoyHit(7) Then Return True
  255. Else
  256.         If JoyHit(7,port) Then Return True
  257. End If
  258.  
  259. Return False
  260.  
  261. End Function
  262.  
  263. ;LStickButtonHit()
  264. ;returns true is the left stick pressed in (hit)
  265. Function X360_LStickButtonHit(port)
  266.  
  267. If port=-1 Then
  268.         If JoyHit(9) Then Return True
  269. Else
  270.         If JoyHit(9,port) Then Return True
  271. End If
  272.  
  273. Return False
  274.  
  275. End Function
  276.  
  277. ;RStickButtonHit()
  278. ;returns true is the right stick pressed in (hit)
  279. Function X360_RStickButtonHit(port)
  280.  
  281. If port=-1 Then
  282.         If JoyHit(10) Then Return True
  283. Else
  284.         If JoyHit(10,port) Then Return True
  285. End If
  286.  
  287. Return False
  288.  
  289. End Function
  290.  
  291. ;AButtonDown()
  292. ;returns true is the a button was Down
  293. Function X360_AButtonDown(port)
  294.  
  295. If port=-1 Then
  296.         If JoyDown(1) Then Return True
  297. Else
  298.         If JoyDown(1,port) Then Return True
  299. End If
  300.  
  301. Return False
  302.  
  303. End Function
  304.  
  305. ;BButtonDown()
  306. ;returns true is the b button was Down
  307. Function X360_BButtonDown(port)
  308.  
  309. If port=-1 Then
  310.         If JoyDown(2) Then Return True
  311. Else
  312.         If JoyDown(2,port) Then Return True
  313. End If
  314.  
  315. Return False
  316.  
  317. End Function
  318.  
  319. ;YButtonDown()
  320. ;returns true is the y button was Down
  321. Function X360_YButtonDown(port)
  322.  
  323. If port=-1 Then
  324.         If JoyDown(4) Then Return True
  325. Else
  326.         If JoyDown(4,port) Then Return True
  327. End If
  328.  
  329. Return False
  330.  
  331. End Function
  332.  
  333. ;XButtonDown()
  334. ;returns true is the x button was Down
  335. Function X360_XButtonDown(port)
  336.  
  337. If port=-1 Then
  338.         If JoyDown(3) Then Return True
  339. Else
  340.         If JoyDown(3,port) Then Return True
  341. End If
  342.  
  343. Return False
  344.  
  345. End Function
  346.  
  347. ;RBButtonDown()
  348. ;returns true is the rbumper was Down
  349. Function X360_RBButtonDown(port)
  350.  
  351. If port=-1 Then
  352.         If JoyDown(6) Then Return True
  353. Else
  354.         If JoyDown(6,port) Then Return True
  355. End If
  356.  
  357. Return False
  358.  
  359. End Function
  360.  
  361. ;LBButtonDown()
  362. ;returns true is the lbumper was Down
  363. Function X360_LBButtonDown(port)
  364.  
  365. If port=-1 Then
  366.         If JoyDown(5) Then Return True
  367. Else
  368.         If JoyDown(5,port) Then Return True
  369. End If
  370.  
  371. Return False
  372.  
  373. End Function
  374.  
  375. ;StartButtonDown()
  376. ;returns true is the start button was Down
  377. Function X360_StartButtonDown(port)
  378.  
  379. If port=-1 Then
  380.         If JoyDown(8) Then Return True
  381. Else
  382.         If JoyDown(8,port) Then Return True
  383. End If
  384.  
  385. Return False
  386.  
  387. End Function
  388.  
  389. ;BackButtonDown()
  390. ;returns true is the x button was Down
  391. Function X360_BackButtonDown(port)
  392.  
  393. If port=-1 Then
  394.         If JoyDown(7) Then Return True
  395. Else
  396.         If JoyDown(7,port) Then Return True
  397. End If
  398.  
  399. Return False
  400.  
  401. End Function
  402.  
  403. ;LStickButtonDown()
  404. ;returns true is the left stick pressed in (Down)
  405. Function X360_LStickButtonDown(port)
  406.  
  407. If port=-1 Then
  408.         If JoyDown(9) Then Return True
  409. Else
  410.         If JoyDown(9,port) Then Return True
  411. End If
  412.  
  413. Return False
  414.  
  415. End Function
  416.  
  417. ;RStickButtonDown()
  418. ;returns true is the right stick pressed in (Down)
  419. Function X360_RStickButtonDown(port)
  420.  
  421. If port=-1 Then
  422.         If JoyDown(10) Then Return True
  423. Else
  424.         If JoyDown(10,port) Then Return True
  425. End If
  426.  
  427. Return False
  428.  
  429. End Function


Comments :


TaskMaster(Posted 1+ years ago)

 All of the buttons on the 360 controller are analog.  Is it not possible to retrieve that info?In other words, you can determine how hard a person is pushing the A button.


xtremegamr(Posted 1+ years ago)

 I dunno. I'm just pretty much using the Joy functions in blitz.


xtremegamr(Posted 1+ years ago)

 I was playing around with the Joy functions and I found out you can test the D-Pad for input, too! Here's the code:-------------------------------------------------------------------------------;DPadDir();returns the direction of the d-padFunction X360_DPadDir(port)If port=-1 Then   dir=JoyHat()Else   dir=JoyHat(port)End If;check directionSelect dir   Case 0 ;up      Return 1   Case 45 ;up-right      Return 2   Case 90 ;right      Return 3   Case 135 ;down-right      Return 4   Case 180 ;down      Return 5   Case 225 ;down-left      Return 6   Case 270 ;left      Return 7   Case 315 ;up-left      Return 8   Case -1 ;no direction pressed      Return 0   Default ;just in case something goes VERY wrong      Return -1End SelectEnd Function


TaskMaster(Posted 1+ years ago)

 Nice to see the D-Pad.  But, why not just return the 0, 45, 90, etc...Then your function could Return dir.And what is dir equal to when you return -1, or have you seen that happen?


Stevie G(Posted 1+ years ago)

 <a href="codearcs78f3.html?code=1592" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=1592[/url]


xtremegamr(Posted 1+ years ago)

 I've never seen anything that would make this function return -1; that was just in case anything went very wrong.Also, I just realized, the RightStickX() and RightStickY() functions work. It's the RightStickXDir() and RightStickYDir() that is the problem.


Yo! Wazzup?(Posted 1+ years ago)

 Probably if someone pushes down three buttons and pops one off, you're still pushing down the buttons, right?Hmmm...


Heliotrope(Posted 1+ years ago)

 Three coments...1.WHOOOOOOOOOOOOOW SOOOOOO MANY FUNCTIONS!!!!!!!!!!!!            (Still recouperating from shock :-( ) )2.Are XBOX conectors the same as XBOX 360?                                                     3.How doo I conect it to my computeerrrrrrrrrrrr. :-((


andy_mc(Posted 1+ years ago)

 @mark1110: sorry no, old xbox controllers are different to 360 controllers. 360 controllers use standard USB connection.


SLotman(Posted 1+ years ago)

 Look at my sig. The "free userlibs" I give there works on xbox360 joys, even with rumble. I just ask in return, if you like it, or use it on any commercial application, that you make some donation, and possibly some credit somewhere in the game.


bloos_magoos(Posted 1+ years ago)

 In case anybody cares, I made a nice graphical representation of your button presses so you can try this out (even though this thread is ancient)You have to put the functions AND xtremegamr's dpad code (posted around here somewhere) at the bottom of your program, then paste the following code on top
Code: [Select]


Graphics 1024,768,0,2
AppTitle "Xinput Demo by bloos_magoos"

While Not KeyHit(1)
Cls

;Test left stick
Text 0,120,"Left Stick X - "+Int(X360_LeftStickX(0))
Text 0,130,"Left Stick Y - " + Int(X360_LeftStickY(0))
Oval 0,0,100,100,0
Oval 50+X360_LeftStickX(0)*50,50+X360_LeftStickY(0)*50,1,1,1

;Right
Text 200,120,"Right Stick X - "+Int(X360_RightStickX(0))
Text 200,130,"Right Stick Y - " + Int(X360_RightStickY(0))
Oval 100,0,100,100,0
Oval 150+X360_RightStickX(0)/4,50+X360_RightStickY(0)/4,1,1,1

Text 300,50,"<--- THEY LOOK LIKE BOOBS (.)(.)"
Text 300,60,"Right Analog Stick Graphic is not exact because the func returns a 180 instead of 1"
Line 0,180,1024,180 ;separator
;///////
;testing triggers
;right
Rect 50,200,50,10,0
Rect 50,200,50,Int(-X360_ReturnTrigger(0)*10),1
;Left
Rect 0,200,50,10,0
Rect 0,200,50,Int(X360_ReturnTrigger(0)*10),1




Text 0,230,"NOTICE - - Pressing both triggers makes trigger axis return zero :("
Text 0,250,"Trigger Axis ->" +Int(X360_ReturnTrigger(0))
Line 0,300,1024,300 ;separator
;////////
;Buttons


If Not X360_AButtonDown(0)
Text 0,310,"A"
Else Text 0,310,">>A<<"
EndIf


If Not X360_BButtonDown(0)
Text 0,320,"B"
Else Text 0,320,">>B<<"
EndIf

If Not X360_XButtonDown(0)
Text 0,330,"X"
Else Text 0,330,">>X<<"
EndIf

If Not X360_YButtonDown(0)
Text 0,340,"Y"
Else Text 0,340,">>Y<<"
EndIf

If Not X360_RBButtonDown(0)
Text 0,360,"Right Bumper"
Else Text 0,360,">>Right Bumper<<"
EndIf

If Not X360_LBButtonDown(0)
Text 0,370,"Left Bumper"
Else Text 0,370,">>Left Bumper<<"
EndIf

If Not X360_LStickButtonDown(0)
Text 0,390,"Left Stick"
Else Text 0,390,">>Left Stick<<"
EndIf

If Not X360_RStickButtonDown(0)
Text 0,400,"Right Stick"
Else Text 0,400,">>Right Stick<<"
EndIf

If Not X360_StartButtonDown(0)
Text 0,420,"Start"
Else Text 0,420,">>Start<<"
EndIf

If Not X360_BackButtonDown(0)
Text 0,430,"Back"
Else Text 0,430,">>Back<<"
EndIf

Line 0,450,1024,450 ;separator
;////////////
;Dpad
dir=X360_DPadDir(0)

Text 0,500,"D-PAD"
If dir=1
Text 0,520,"^"
EndIf

If dir=2
Text 0,520,"^ + >"
EndIf

If dir=3
Text 0,520,">"
EndIf

If dir=4
Text 0,520,"v + >"
EndIf

If dir=5
Text 0,520,"v"
EndIf

If dir=6
Text 0,520,"v + <"
EndIf

If dir=7
Text 0,520,"<"
EndIf

If dir=8
Text 0,520,"^ + <"
EndIf

If dir=0
Text 0,520," - - "
EndIf

If dir=-1
RuntimeError "Something happened I guess"
EndIf






Flip
Wend
 
Yes I know I coded it lazy lol.


Species(Posted 1+ years ago)

 i have an xbox controller for windows. it works fine with blitz3d when you install the software that comes with the controller. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal