[2/2] dinput: Fix direction computation code

Vincent Pelletier plr.vincent at gmail.com
Fri Jan 23 18:38:40 CST 2009


Fix direction computation code when effect is defined in DIEFF_CARTESIAN mode.
- use atan2 to generate -PI..+PI angle (removes the need to care about
  divide-by-zero, fixes all "forward" directions not being available)
- remap axes according to effect setting (it was taken for granted that order
  was [DIJOFS_X,DIJOFS_Y])

I don't know how/if it should be tested. The measurable effect that code has 
would be on joystick coordinates, requiring to make the joystick move...

Comparison of values generated:
- Lower left: x=-1, y=-1, upper right: x=1, y=1
- Up is forward, down is backward, left is left and right is right.
- Values are the computed angle, from 0 to FFFF. Backward should be 0, left
  4000, forward 8000, right C000 (source: kernel documentation).

Old code:
F555 ... FEA6 0000 015A ... 0AAA
.                              .
C000                        4000
.                              .
0AAA ... 015A 0000 FEA6 ... F556

New code:
5FFF ... 7BEF 7FFF 840E ... 9FFE
.                              .
3FFF                        BFFE
.
1FFD ... 040D FFFE FBEE ... DFFE

-- 
Vincent Pelletier
-------------- next part --------------
From 3d3c0cb91808ed484431163ed42a6414bba3f0d1 Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <plr.vincent at gmail.com>
Date: Sat, 24 Jan 2009 01:36:53 +0100
Subject: Fix direction computation code when effect is defined in DIEFF_CARTESIAN mode.
 - use atan2 to generate -PI..+PI angle (removes the need to care about divide-by-zero, fixes all "forward" directions not being available)
 - remap axes according to effect setting (it was taken for granted that order was [DIJOFS_X,DIJOFS_Y])

---
 dlls/dinput/effect_linuxinput.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/dlls/dinput/effect_linuxinput.c b/dlls/dinput/effect_linuxinput.c
index f57f1be..62e10aa 100644
--- a/dlls/dinput/effect_linuxinput.c
+++ b/dlls/dinput/effect_linuxinput.c
@@ -576,15 +576,15 @@ static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
 	    }
 	} else { /* two axes */
 	    if (peff->dwFlags & DIEFF_CARTESIAN) {
-		/* avoid divide-by-zero */
-		if (peff->rglDirection[1] == 0) {
-                    if (peff->rglDirection[0] >= 0)
-                        This->effect.direction = 0x4000;
-                    else if (peff->rglDirection[0] < 0)
-                        This->effect.direction = 0xC000;
+		LONG x, y;
+		if (peff->rgdwAxes[0] == DIJOFS_X) {
+		    x = peff->rglDirection[0];
+		    y = peff->rglDirection[1];
 		} else {
-		    This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
+		    x = peff->rglDirection[1];
+		    y = peff->rglDirection[0];
 		}
+		This->effect.direction = (int)((3 * M_PI / 2 - atan2(y, x)) * 0x7FFF / M_PI);
 	    } else {
 		/* Polar and spherical are the same for 2 axes */
 		/* Precision is important here, so we do double math with exact constants */
-- 
1.5.6.5



More information about the wine-patches mailing list