[Java|Android|LibGDX|...] Mouse movement and collision detection

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?

http://shrani.si/f/3j/4d/4Y7pGtCD/photo.png



... 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);
    }


... 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

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.

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)

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.

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. :slight_smile:

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

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:

		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.