Skip to content

Commit 9117432

Browse files
authored
Merge pull request #1 from texx00/bugfixes/zero-division-error
Bugfix zerodivisionerror
2 parents c76b5b6 + b5f0e2e commit 9117432

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

XInput.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,20 @@ def get_thumb_values(state):
252252
magL = sqrt(LX*LX + LY*LY)
253253
magR = sqrt(RX*RX + RY*RY)
254254

255-
normLX = LX / magL
256-
normLY = LY / magL
257-
normRX = RX / magR
258-
normRY = RY / magR
255+
if magL != 0:
256+
normLX = LX / magL
257+
normLY = LY / magL
258+
else: # if magL == 0 the stick is centered, there is no direction
259+
normLX = 0
260+
normLY = 0
261+
262+
if magR != 0:
263+
normRX = RX / magR
264+
normRY = RY / magR
265+
else: # if magR == 0 the stick is centered, there is no direction
266+
normRX = 0
267+
normRY = 0
268+
259269

260270
normMagL = 0
261271
normMagR = 0
@@ -396,8 +406,12 @@ def get_events():
396406

397407
magL = sqrt(LX*LX + LY*LY)
398408

399-
normLX = LX / magL
400-
normLY = LY / magL
409+
if magL != 0:
410+
normLX = LX / magL
411+
normLY = LY / magL
412+
else: # if magL == 0 the stick is centered, there is no direction
413+
normLX = 0
414+
normLY = 0
401415

402416
normMagL = 0
403417

@@ -429,8 +443,12 @@ def get_events():
429443

430444
magR = sqrt(RX*RX + RY*RY)
431445

432-
normRX = RX / magR
433-
normRY = RY / magR
446+
if magR != 0:
447+
normRX = RX / magR
448+
normRY = RY / magR
449+
else: # if magR == 0 the stick is centered, there is no direction
450+
normRX = 0
451+
normRY = 0
434452

435453
normMagR = 0
436454

0 commit comments

Comments
 (0)