Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  [Java|Android|LibGDX|...] Mouse movement and collision detection  (Read 487 times)
0 Members and 1 Guest are viewing this topic.
Offline JanNovak18

Senior Newbie





« Posted 2014-02-22 13:21:41 »

Hi everyone,

I have decided to make Air Hockey game. But I have already got into trouble. I am moving paddle with mouse that is cool. But when I am checking collision with middle line there is a problem. (2)On slow mouse movement collision works fine, but when moving mouse fast (1) collision is not detected well, because mouse move faster than paddle is rendered so paddle get stuck before middle. I have tried making game also in java without engines and it is the same, so here is the problem which I dont understand how to fix it? Any ideas?



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
... class public class MouseHandler implements MouseMotionListener
@Override
    public void mouseMoved(MouseEvent mouseEvent)
    {
        if (!(mouseEvent.getX() > panel.getWidth()/2 - paddle.getR()))
        {
            paddle.setX(mouseEvent.getX());
        }

        paddle.setY(mouseEvent.getY() - paddle.getR()/2);
        paddle.setR(panel.getHeight()/5);
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
... class Panel extends JPanel
@Override
    public void paint(Graphics g1) {
        ...

        g.setColor(Color.GREEN);
        g.fillOval(paddle.getX(),paddle.getY(),paddle.getR(),paddle.getR());
       
        ....
    }


// That is code from java but the similiar code I have in libGDX engine...

Regards
Jan
Offline trollwarrior1
« Reply #1 - Posted 2014-02-22 13:34:42 »

I didn't quite catch your problem, but what I understood is that when you move your mouse fast, collision might be skipped.
The only way to solve this is take lastx,lasty coordinates, as well as newx,newy coordinates, and get deltax,deltay values (lastx-newx)=deltax
Now, move your paddle 1 pixel at a time until the pixels moved are greater than the deltax. After each time you increase your paddle's position by 1, check for collision.
Offline JanNovak18

Senior Newbie





« Reply #2 - Posted 2014-02-22 13:56:35 »

Here is the flash video for better presentation how does the problem look like...

http://megaswf.com/file/2642304 (Click "Play flash full screen" for better view)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-02-22 13:58:14 »

So I was right.
Offline Gibbo3771
« Reply #4 - Posted 2014-02-22 14:03:38 »

So I was right.


I guess he is only checking for collisions each frame? When really he should be checking for collisions every time the paddle moves.

Small game so this shouldn't cause performance issues.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline JanNovak18

Senior Newbie





« Reply #5 - Posted 2014-02-22 18:42:59 »

Thank you guys, I managed to solve the problem by doing exactly what @trollwarrior1 said. Both of answers helped me that I improved knowledge about the problem. Thanks again. But I have one more question so I wont open new thread for that I will just ask here.   Smiley

It is about antialiasing or "smoothing", when I was programming in pure Java with no engines like libGDX I just set rendering hints to Graphics2D to smooth lines,text,shapes and Java done its job beuatiful
1  
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);


But on libGDX I could not find anything usefull all what I found on internet and tried it, it makes situation worse. Is there any easy or little harder way to get desired smooth effect. Because without smoothing lines,it looks very ugly.
Offline Gibbo3771
« Reply #6 - Posted 2014-02-22 19:57:20 »

Thank you guys, I managed to solve the problem by doing exactly what @trollwarrior1 said. Both of answers helped me that I improved knowledge about the problem. Thanks again. But I have one more question so I wont open new thread for that I will just ask here.   Smiley

It is about antialiasing or "smoothing", when I was programming in pure Java with no engines like libGDX I just set rendering hints to Graphics2D to smooth lines,text,shapes and Java done its job beuatiful
1  
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);


But on libGDX I could not find anything usefull all what I found on internet and tried it, it makes situation worse. Is there any easy or little harder way to get desired smooth effect. Because without smoothing lines,it looks very ugly.


This techincally should fix it:

1  
2  
      sprite.getTexture().setFilter(TextureFilter.Linear,
            TextureFilter.Linear);


But being honest, I don't see much difference on my laptop screen but on my android devices it looks very anti-aliased.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (5 views)
2014-07-29 03:06:25

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

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

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

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

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

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

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

MustardPeter (44 views)
2014-07-16 23:30:00
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!