Mouse.getDX() / getDY() do not work properly on PC

Started by atalanteIT, March 05, 2015, 15:12:25

Previous topic - Next topic

atalanteIT

... however it works on mac.

Hi everybody!

This is happening if my I set Mouse.setGrabbed(true);

double dx = Mouse.getDX() * SPEED_MOUSE;
double dy = Mouse.getDY() * SPEED_MOUSE;


This simple kind of line do not return correct DX/DY on PC (work fine on MAC) - I use it for camera mvt.

In my pom.xml (Maven) if I called the version of LWGJL
<dependency>
  <groupId>org.lwjgl.lwjgl</groupId>
  <artifactId>lwjgl</artifactId>
  <version>2.9.1</version>
</dependency>

the problem is gone (however I have some issue on mac with fullscreen) but this is fixed with the 2.9.3 and it is not the pb here  ;)

Now if I put the last version 2.9.3, the getDx() or getDy() do not work. I notice also a weird stuff.
It looks like the cursor is set on 0,0 and if I move my mouse to the left (in  grabbed = true mode) dx = 0  but getDx() works only if my mouse is moving to the right.)

I also tried to put
while(Mouse.next()){
int dx = Mouse.getEventDx(); but I have -1 here ...
}


Does someone had such problem ? (again it work perfectly with 2.9.1 in Mac and PC and it does not work on PC only for 2.9.3)

Thanks a lot if you have any clue ...

EDIT


ok now this is a code that you execute (copy paste it, it is a stand alone class)
The application is simple : you move your mouse cursor, you have output in console.

so with the same code :

use org.lwjgl.lwjgl 2.9.1 - the output of DX/DY if the mouse move to the left on X, and nothing on Y : (it is an example - important is the numbers are negative)
Mouse -24 0
Mouse -27 0
Mouse -51 0
Mouse -34 0
Mouse -21 0

now with org.lwjgl.lwjgl 2.9.3 :
Mouse 0 -499
Mouse 0 -499
Mouse 0 -499

please try this following code :

package be;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class TestMouse {
	  
    /** time at last frame */
    long lastFrame;
  
    /** frames per second */
    int fps;
    /** last fps time */
    long lastFPS;
  
    StringBuffer sb;
    
    int WIDTH = 800, HEIGHT = 600;

	private boolean quit = false;
  
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
  
        initGL(); // init OpenGL
        getDelta(); // call once before loop to initialise lastFrame
        lastFPS = getTime(); // call before loop to initialise fps timer
        
        sb = new StringBuffer();
        
        System.out.println(" * Please use 2.9.1 and everything works fine.");
        System.out.println(" * if you use 2.9.3 DX will be set to 0 for moving your mouse to the left.");
        System.out.println(" * Mouse should get the DX / DY when mouse is moving");
        System.out.println(" * Press ESC to quit");
        System.out.println(" * Press E key to set grabbed to true");
        
  
        while (!quit) {
            int delta = getDelta();
  
            update(delta);
            renderGL();
  
            Display.update();
            Display.sync(60); // cap fps to 60fps
        }
  
        Display.destroy();
    }
  
    public void update(int delta) {
    	quit = Display.isCloseRequested();
        while (Keyboard.next()) {
        	if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
            	quit  = true;
            }
            if(Keyboard.getEventKey() == Keyboard.KEY_E) {
            	Mouse.setGrabbed(true);
            }
        }
        sb.setLength(0);
        while(Mouse.next()){
        	sb.append("Mouse ").append(Mouse.getEventDX()).append(" ").append(Mouse.getEventDY());
        	System.out.println(sb.toString());
        }
         
        updateFPS(); // update FPS Counter
    }
  
    /**
     * Set the display mode to be used 
     * 
     * @param width The width of the display required
     * @param height The height of the display required
     * @param fullscreen True if we want fullscreen mode
     */
    public void setDisplayMode(int width, int height, boolean fullscreen) {
 
        // return if requested DisplayMode is already set
                if ((Display.getDisplayMode().getWidth() == width) && 
            (Display.getDisplayMode().getHeight() == height) && 
            (Display.isFullscreen() == fullscreen)) {
            return;
        }        
        
        try {
            DisplayMode targetDisplayMode = null;
             
            if (fullscreen) {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
                int freq = 0;
                 
                for (int i=0;i<modes.length;i++) {
                    DisplayMode current = modes[i];
                     
                    if ((current.getWidth() == width) && (current.getHeight() == height)) {
                        if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
                            if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
                                targetDisplayMode = current;
                                freq = targetDisplayMode.getFrequency();
                            }
                        }
 
                        // if we've found a match for bpp and frequence against the 
                        // original display mode then it's probably best to go for this one
                        // since it's most likely compatible with the monitor
                        if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
                            (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
                            targetDisplayMode = current;
                            break;
                        }
                    }
                }
            } else {
                targetDisplayMode = new DisplayMode(width,height);
            }
             
            if (targetDisplayMode == null) {
                System.out.println("Failed to find value mode: "+width+"x"+height+" fs="+fullscreen);
                return;
            }
 
            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);
             
        } catch (LWJGLException e) {
            System.out.println("Unable to setup mode "+width+"x"+height+" fullscreen="+fullscreen + e);
        }
    }
     
    /** 
     * Calculate how many milliseconds have passed 
     * since last frame.
     * 
     * @return milliseconds passed since last frame 
     */
    public int getDelta() {
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
  
        return delta;
    }
  
    /**
     * Get the accurate system time
     * 
     * @return The system time in milliseconds
     */
    public long getTime() {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
  
    /**
     * Calculate the FPS and set it in the title bar
     */
    public void updateFPS() {
        if (getTime() - lastFPS > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }
  
    public void initGL() {
    	GL11.glMatrixMode(GL11.GL_PROJECTION);
    	GL11.glLoadIdentity();
    	GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }
  
    public void renderGL() {
        // Clear The Screen And The Depth Buffer
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    }
  
    public static void main(String[] argv) {
        TestMouse mouseTest = new TestMouse();
        mouseTest.start();
    }
}


Please ignore the last line, I do not know why it appears here (conflict with CSS ?)

Now it will be a little better to see the problem.
Important : version of LWJGL and execute it on PC (here windows 7)

atalanteIT

Hi again - nobody got such issue ?
Did someone tried the code ?
Did someone tried it also on Ubuntu (difference of behavior)?

What is the reliable way to get Dx / Dy now ? (for PC,MAC and Linux)
A work-around may be ?

Thanks,

spasi

Your code works fine (Windows 8.1), without the issue you're describing.

Your event loop is not exactly correct though. The methods getDX/getDY return the mouse translation aggregate since the last time you called them. They are not meant to be called within a Mouse.next() loop, you can use getEventDX/getEventDY instead.

If anyone else can test the above code, please let me know if you're experiencing the same problem.

atalanteIT

Ok thanks spasi for the remark.
I update the line in the previous code.

sb.append("Mouse ").append(Mouse.getEventDX()).append(" ").append(Mouse.getEventDY());


I tried the new code with 2.9.1 and I have
Mouse -1 0
Mouse -2 0

That is correct (when I pressed E or without - means the Mouse.setGrabbed(true) the result is the good one)
so everything is fine with 2.9.1.

Now with 2.9.3
I have
Mouse -1 0
Mouse -2 0


if the mouse move to the left (so perfect).

but as soon as I hit the key E (Mouse.setGrabbed(true) ) the output is :

Mouse 0 -499
Mouse 0 -499


I guess if someone has windows 7 could have a try ...
The detail for the machine I am using right now is :
Windows 7 enterprise
Service Pack 1

Please not that the issue is NOT occurring with the version of the lib 2.9.1

P.S.Ã,²: I did not try the 2.9.2 - it is even a different behavior. The result is correct if the key E is not hit. As soon as I hit E, the output in the console frizzed and I have a huge list of
'Mouse 0 -499'  that causes a crash (I use the Alt+Tab to get back to Eclipse, and it seems to kill the application)

Anybody can try with a windows 7 with a positive / negative feedback ?