Page 1 of 1

Gaming Mouse position lag

Posted: Wed Oct 01, 2014 8:56 pm
by Azur
Hello, it seems i encounter some issues with mousedelta usage.
When, i move my mouse slowly everything works fine but when i move it fast the delta is laggy.
I have this problem in every 3d examples if the mouse rotate the camera, i can't do a fast 360, the camera just stop rotating if i move too fast.
Someone else have the same issue ?
thx.

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 7:56 am
by infratec
Hi,

first of all:
if you want that someone look after your problem, than provide a working (or not working) code.

Like this:

Code: Select all

InitSprite()
InitMouse()
InitKeyboard()


OpenWindow(0, 0, 0, 800, 600, "MouseDelta Test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

XDelta = 400
YDelta = 300
MouseLocate(XDelta, YDelta)


LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")

Repeat
  FlipBuffers()
  ClearScreen(RGB(0,0,0))
  
  ExamineKeyboard()
  ExamineMouse()
  
  XDelta + MouseDeltaX()
  YDelta + MouseDeltaY()
  
  x = MouseX()
  y = MouseY()
  
  If MouseButton(#PB_MouseButton_Middle)
    ;MouseLocate(400, 300)
    Debug "Abs: " + Str(x) + " / " + Str(y)
    Debug "Rel: " + Str(XDelta) + " / " + Str(YDelta)
  EndIf
  
  DisplaySprite(0, x - SpriteWidth(0) / 2, y - SpriteHeight(0) / 2)
  
Until MouseButton(#PB_MouseButton_Left) Or MouseButton(#PB_MouseButton_Right)
The output should have always the same coordinates.
If you move the mouse slow, than it's true.
If you move the mouse fast, it's not longer true :cry:

So, you are right.

Maybe it's a bug, maybe it's a limitation of windows.
To avoid this, use your own delta by storing the last results of MouseX() and MouseY().

And....
Sometimes when I stop the program, the window stucks (no response from the program),
but I can debug the code step by step.
That's very strange.

I use win 7 and PB 5.31 for the test.

Bernd

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 8:07 am
by infratec
'Bugfix':

Code: Select all

InitSprite()
InitMouse()
InitKeyboard()


OpenWindow(0, 0, 0, 800, 600, "MouseDelta Test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

XDelta = 400
YDelta = 300
MouseLocate(XDelta, YDelta)
LastMouseX = XDelta
LastMouseY = YDelta


LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")

Repeat
  FlipBuffers()
  ClearScreen(RGB(0,0,0))
  
  ExamineKeyboard()
  ExamineMouse()
  
  x = MouseX()
  y = MouseY()
    
  If x > LastMouseX
    XDelta + (x - LastMouseX)
  Else
    XDelta - (LastMouseX - x)
  EndIf
  LastMouseX = x
  
  If y > LastMouseY
    YDelta + (y - LastMouseY)
  Else
    YDelta - (LastMouseY - y)
  EndIf
  LastMouseY = y
  
  If MouseButton(#PB_MouseButton_Middle)
    ;MouseLocate(400, 300)
    Debug "Abs: " + Str(x) + " / " + Str(y)
    Debug "Rel: " + Str(XDelta) + " / " + Str(YDelta)
  EndIf
  
  DisplaySprite(0, x - SpriteWidth(0) / 2, y - SpriteHeight(0) / 2)
  
Until MouseButton(#PB_MouseButton_Left) Or MouseButton(#PB_MouseButton_Right)
If you really want the delta,
replace the nDelta + and nDelta - by a nDelta =

Bernd

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 3:25 pm
by Azur
Hello thx for answering.
I didnt post code example bcause as i said, i can test this in every PB's examples that use mouse for camera rotation.

No it still dosent work.
Full Win reinstall and the same issue.
When i move the mouse fast, the mousedelta is corrupted
Tested on another computer works fine.
Tested with another mouse on my computer, works fine.
I own a MadCat RAT7.
Never had problem in game, this mouse works fine.

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 3:46 pm
by infratec
What doesn't work?
My 'bugfix' solutions shows not the identical values?

And yes, with a screen you have no mouse pointer.
If you need one, create a sprite for it.

Bernd

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 4:17 pm
by Azur
Nevermind about the pointer question i tested it again and it work.
Your bugFix works fine, both values are the same but if i move the mouse fast ( not very fast ), the sprite dosent move.
Seems it only happends with the RAT7

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 8:01 pm
by Azur
Hello Bernd
In fact when i test your bugFix, there is no pointer anymore and the closewindow gadget is unreachable.
Can you confirm this ?

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 8:40 pm
by infratec
Hi,

yes that's true.
If you use ExamineMouse()
the 'normal' mouse stuff from windows is 'out of order'.
Than you need to handle this.

When you use WindowedScreen() you normally don't use this.
It is only for fullscreen game programs.

A windowedscreen program is more like this:

Code: Select all

InitSprite()

OpenWindow(0, 0, 0, 800, 600, "MouseDelta Test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

XDelta = 400
YDelta = 300
LastMouseX = XDelta
LastMouseY = YDelta


LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasic.bmp")

Exit = #False
Repeat
  
  Repeat
    
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_LeftClick
        Debug "Abs: " + Str(x) + " / " + Str(y)
        Debug "Rel: " + Str(XDelta) + " / " + Str(YDelta)
      Case #PB_Event_CloseWindow
        Exit = #True
    EndSelect
    
  Until Event = 0
  
  FlipBuffers()
  ClearScreen(RGB(0,0,0))

  
  x = WindowMouseX(0)
  y = WindowMouseY(0)
   
  If x > LastMouseX
    XDelta + (x - LastMouseX)
  Else
    XDelta - (LastMouseX - x)
  EndIf
  LastMouseX = x
 
  If y > LastMouseY
    YDelta + (y - LastMouseY)
  Else
    YDelta - (LastMouseY - y)
  EndIf
  LastMouseY = y
  
  DisplaySprite(0, x - SpriteWidth(0) / 2, y - SpriteHeight(0) / 2)
 
Until Exit
Bernd

Re: Mousedeltax question

Posted: Thu Oct 02, 2014 9:15 pm
by Azur
Ok thanks for confirming.
Yes i agree there is no absolute necessity for using examineMouse in a windowed context.
I lose my mind with this mouse issue and see bugs/strange behaviours everywhere.
I posted on the French forum and there is a user, Ar-S, who told me he had the same issue with a MadCat RAT5 mouse, he gave me a piece of code who partialy resolve the problem for RAT7/5 and probably some Razer and Roccat mouses.

Code: Select all

; ------- RAT 7 Mouse delta calculation
; ------- by Ar-S on the French forum

InitMouse()
InitSprite()

Structure appli
  mouseDeltaX.l
  mouseDeltaY.l
EndStructure

Global appli.appli

Procedure mousethread(flag.i) ; souris roccat, razer .....
   Static quit.i
   If flag
      Repeat
         If ExamineMouse()
            appli\mouseDeltaX=MouseDeltaX()
            appli\mouseDeltaY=MouseDeltaY()
         EndIf
         Delay(1)
      Until quit
      quit=#False
   Else
      If Not flag
         quit=#True
         While quit
            Delay(1)
         Wend
      EndIf
   EndIf
EndProcedure

OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,640,480)
CreateThread(@mousethread(),#True)

Repeat
  ClearScreen(RGB(0,0,0))
  StartDrawing(ScreenOutput())
    Box(0,0,appli\mouseDeltaX*10,480,RGB(255,0,0))
  StopDrawing()  
  FlipBuffers()
Until WindowEvent()=#PB_Event_CloseWindow

Re: Mousedeltax question

Posted: Fri Oct 03, 2014 12:21 am
by Azur
Ok i have a working example but im sorry my english is too bad for a deep explanation.
but ...
It seems some gaming mouses have a high refresh rate, and, if the main programme run at 60fps f.a.e. the motion is laggy.
By using a thread we can request the motion at a higher rate.
The thread wait for the main program to ask for the motion, and fetch the sum of each displacement since the previous request.
In this example the main loop run at 50ms on purpose with a delay(50)
You can change this value and see that slow and fast motions of the mouse are recorded.
Hope it can help pew pew pew mouses owners.

Code: Select all

EnableExplicit

InitSprite()
InitKeyboard()
InitMouse()

Structure appli
  flag_request_mouse_delta.i  ; set this to 1 to request the deltas
  mouseDeltaX.i               ; deltas are recovered here
  mouseDeltaY.i
  thread.l                    ; index for killThread()
EndStructure

Global appli.appli

OpenWindow(0,0,0,800,600,"",#PB_Window_ScreenCentered) ;i have no idea what i'm doing
OpenWindowedScreen(WindowID(0),0,0,800,600)

Procedure sortie()
  KillThread(appli\thread)
  End
EndProcedure
                                            ;******************
Procedure scan()                            ; press esc to QUIT     
  Define ev=WaitWindowEvent(1)              ;******************
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    sortie()
  EndIf
EndProcedure

Procedure render()
  appli\flag_request_mouse_delta=1          ; set flag to 1 -> request deltas
  While appli\flag_request_mouse_delta=1    ; wait for flag=0
    Delay(1)
  Wend
  ClearScreen(RGB(0,0,0))                   ; deltas are fetched by the thread
  StartDrawing(ScreenOutput())
    Box(0,0,appli\mouseDeltaX,300,RGB(255,0,0))
  StopDrawing()
  FlipBuffers()
EndProcedure

Procedure mouseScan(var.i)                    ; this thread records the mouse position 
  Static xincrement.i                       
  Static yincrement.i
  Repeat
  If  appli\flag_request_mouse_delta=0      ; increment deltas until they are requested
    ExamineMouse()
    xincrement+MouseDeltaX()
    yincrement+MouseDeltaY()
    Delay(1)
  Else
    appli\mouseDeltaX=xincrement            ; request flag = 1 -> fetch deltas
    appli\mouseDeltaY=yincrement
    xincrement=0
    yincrement=0
    appli\flag_request_mouse_delta=0        ; set request flag = 0
    Delay(1)
  EndIf
  ForEver
EndProcedure

Procedure main()
  Repeat
    scan()            ; scan ui
    render()          ; draws 
    Delay(50)         ; simulate a laggy app
  ForEver
EndProcedure

appli\thread=CreateThread(@mouseScan(),#True)

main()

Re: Gaming Mouse position lag

Posted: Fri Oct 03, 2014 9:25 am
by Bananenfreak
I don´t know if I don´t understand your Problem, but with my mouse (1700dpi) there´s no problem at all.
Whenever I rotate the camera as fast and much as I can the camera rotates normal.

Perhaps it´s not PB, but your mouse? (Just a case...)

Re: Gaming Mouse position lag

Posted: Fri Oct 03, 2014 11:48 am
by IdeasVacuum
Open the screen with #PB_Screen_NoSynchronization, that should have a beneficial effect (discovered by netmaestro).