Mr Squidr
Mr Squidr

Reputation: 143

Applet "pong" game: ball goes right through paddle if paddle is moving towards the ball

usually there is only Y axis movement available in such game, but I decided to make it in such a way that X axis paddle movements are also allowed. game works perfectly fine IF I don't move paddle on X axis WHEN ball hits the paddle (in that case it goes right through.) If I stop X axis movement before ball hits paddle ball will bounce off of it every time.

At first I though this has to do with pixels but I also tried changing paddle collision in multiple ways with no positive results. Ball will NEVER bounce off paddle no matter how many times I try.

code very quite simple and I'm moving everything by just 1px at a time:

public void drawBall(Graphics g) { //ball speed, movement and collision
    b.set(b.getX()+VX,b.getY()+VY); //velocity X and velocity Y both at 1px
    g.setColor(Color.black);
    b.draw(g);

    if(b.getY()<0 || b.getY()>sizeY-b.getRadius()){ //top & bottom collision
        VY = -VY;
    }

    if(b.getX()<0 || b.getX()>sizeX-b.getRadius()){ //left & right detection
        b.center();
        VX = -VX;
        x = (int) (Math.random()*2+0);
        if(x == 0)
            VY = -VY;
    }

    if(b.getX() == p1.getX()+p1.width && b.getY()>p1.getY() && b.getY()<p1.getY()+p1.height){ // p1 paddle detection
        VX = -VX;
    }

    if(b.getX()+b.getRadius()==p2.getX() && b.getY()>p2.getY() && b.getY()<p2.getY()+p2.height){ // p2 paddle detection
        VX = -VX;
    }
}

To move paddles around I use simple flags that set movement to specific direction "on" on key press and "off" on key release. I run this in infinitive loop so it's being executed constantly.

this is how "run()" method looks like:

public void run() {
    while(true){
        repaint();
        try {
            Thread.sleep(5);
            playerMovement(); //moves players paddle on X and/or Y axis
            playerLimits(); //limits movment of players to specific area
        } catch (InterruptedException e){
        }
    }
}

Again I tried creating paddle collision in such a way that it's not pixel specific with "if(ball < than p1 paddle) then change ball direction" but with no success. I guess I'm missing something important here but I can't seem to find out what it is.

Thanks for any help in advance, I much appreciate it.

Upvotes: 1

Views: 173

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Don't mix your game engine code with your graphics code -- the two must be kept separate. The key to any solution is to make sure that the game engine recognizes a "collision" and handles it correctly and unambiguously. Instead of toggling your velocity directions, consider using setting the absolute values of these velocities depending on where the collision occurs.

For instance, you have this code:

if(b.getY() < 0 || b.getY() > sizeY-b.getRadius()){ //top & bottom collision
    VY = -VY;
}

but this risks getting a sprite trapped if it is caught beyond a margin. Perhaps better would be to do something like:

if (b.getY() < 0) {
    // note that variables should start with lower-case letters
    vy = Math.abs(vy); // make sure that it is going in a positive direction
}

if (b.getY() > sizeY - b.getRadius()) {
    vy = -Math.abs(vy);
}

and likewise for the x direction

Also note that if you separate graphics from game engine, your engine becomes separately testable and easier to debug.

Upvotes: 1

Related Questions