Gaming Mouse position lag

Just starting out? Need help? Post your questions and find answers here.
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Gaming Mouse position lag

Post 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.
Last edited by Azur on Fri Oct 03, 2014 12:29 am, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mousedeltax question

Post 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
Last edited by infratec on Thu Oct 02, 2014 8:15 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mousedeltax question

Post 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
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Mousedeltax question

Post 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.
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mousedeltax question

Post 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
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Mousedeltax question

Post 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
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Mousedeltax question

Post 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 ?
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mousedeltax question

Post 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
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Mousedeltax question

Post 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
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: Mousedeltax question

Post 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()
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Gaming Mouse position lag

Post 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...)
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Gaming Mouse position lag

Post by IdeasVacuum »

Open the screen with #PB_Screen_NoSynchronization, that should have a beneficial effect (discovered by netmaestro).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply