Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 1547 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2012-12-15 12: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
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-12-15 17: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 19: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: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-12-15 19: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 18:52:35 »

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

Myomyomyo.
Offline Mac70
« Reply #5 - Posted 2012-12-16 18: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: 106
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2012-12-16 18: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 19:00:35 »

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

Check out my Devblog! Smiley
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #8 - Posted 2012-12-16 19: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: 106
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-12-16 19: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: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-12-16 19: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

Senior Member


Medals: 3
Projects: 1



« Reply #11 - Posted 2012-12-16 19: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 19: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.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!