Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Lock mouse on game screen  (Read 1799 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2012-12-15 11:27:39 »

Like in topic. Currently I am using Robot class to lock mouse on game screen, but this is not something I want, because it can't do it smoothly (in the way user can't see mouse "jumps").

This is my code (inside main game loop):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
if (xAbs<=Display.getX()+10) {
            robot.mouseMove(Display.getX()+10, yAbs);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (yAbs<=Display.getY()+30) {
            robot.mouseMove(xAbs, Display.getY()+30);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (xAbs>=Display.getX()+Display.getWidth()-10) {
            robot.mouseMove(Display.getX()+Display.getWidth()-10, yAbs);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (yAbs>=Display.getY()+Display.getHeight()) {
            robot.mouseMove(xAbs, Display.getY()+Display.getHeight());
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }


Is there any way to do it "smoothly"?

Check out my Devblog! Smiley
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-12-15 16:55:23 »

Well, at what rate is this code executing? And what do you mean by "jump"? Is the mouse cursor snapping back and forth between the desired position and the actual mouse position?
Offline Mac70
« Reply #2 - Posted 2012-12-15 18:03:36 »

It is executing about 60 times per second, but FPS may vary. By "jumping" I mean that when you want to leave "box" and move mouse fast enough, you can see how mouse leave game screen and comes back to it one frame later.

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-12-15 18:21:45 »

Reposition the mouse cursor every frame, to the center of the viewport.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #4 - Posted 2012-12-16 17:52:35 »

What's wrong with
Mouse.setGrabbed(true);
?

Myomyomyo.
Offline Mac70
« Reply #5 - Posted 2012-12-16 17:57:40 »

What's wrong with
Mouse.setGrabbed(true);
?

Now there is no mouse on screen.

Check out my Devblog! Smiley
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2012-12-16 17:58:14 »


If the mouse is snapping on the mouse all the time, why would you want to draw the mouse anyways?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Mac70
« Reply #7 - Posted 2012-12-16 18:00:35 »

I want to see cursor, but it must be "trapped" inside game screen.

Check out my Devblog! Smiley
Offline PeterNicholson
« Reply #8 - Posted 2012-12-16 18:06:47 »

Why not hide the mouse with Mouse.setGrabbed(true), and then create a cursor texture, render it and make it follow the position of the mouse with Mouse.getX() and Mouse.getY() . Hope that helped!  Smiley

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-12-16 18:17:31 »

Why not hide the mouse with Mouse.setGrabbed(true), and then create a cursor texture, render it and make it follow the position of the mouse with Mouse.getX() and Mouse.getY() . Hope that helped!  Smiley

As far as I know, this does only help a little bit, since the Mouse.getX() returns negative values very soon, if you move the mouse to the left. So if you'd draw a cursor directly at the Mouse.getX() position, it would be drawn outside the window.

What could possibly work is:
Set mouse to grabbed,
If the mouse is outside the window on the left, set the x position to the leftmost position,
If the mouse is outside the window on the top, set the y position to the topmost position,
If the mouse is outside the window on the right, set the x position to the rightmost position,
If the mouse is outside the window on the bottom, set the y position to the bottommost position,
Render mouse.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-12-16 18:24:44 »

Just use Mouse.getDX() and Mouse.getDY() to see how much the mouse traveled in the last frame Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline PeterNicholson
« Reply #11 - Posted 2012-12-16 18:34:53 »

http://www.2shared.com/file/LaFt_tB_/CursorTestRUN.html
Download that. I hope that is what you mean. Made by me in 10 minuets! Source code: http://pastebin.java-gaming.org/e08aa0f4936  Grin

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline Mac70
« Reply #12 - Posted 2012-12-16 18:41:40 »

Very big thanks, this is exacly the effect that I want to obtain. Wink

Check out my Devblog! Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!