This site requires JavaScript, please enable it in your browser!
Greenfoot back
thebutterycanadian
thebutterycanadian wrote ...

2017/1/21

Breakout paddle collision problem

thebutterycanadian thebutterycanadian

2017/1/21

#
Hello, I'm trying to code a breakout scenario according to the Joy of Code videos on youtube. I am having trouble getting the ball to correctly detect when it hits the paddle. For some reason, the ball always acts as though the collision happens slightly above where the paddle appears on the screen, instead of colliding with the actual paddle image. If I move the paddle closer to the ball, the ball still detects the collision as occurring above the paddle instead of on it no matter what. Here is the code I am currently using to detect paddle collision.
 private void checkPaddle()
        {
            Actor paddle = getOneIntersectingObject(paddle.class);
            if (paddle != null) {
                deltaY = -deltaY;
                int offset = getX() - paddle.getX();
                deltaX = deltaX + (offset/10);
                if (deltaX > 7) {
                    deltaX = 7;
                }
                if (deltaX < -7) {
                    deltaX = -7;
                }
                         
                

            }            
        }
danpost danpost

2017/1/21

#
Try this. Add the following to the ball class (or if you already have the first line in the code, add the code in the block to the end of the block in your code):
public Ball()
{
    // other code (if you already have a 'public Ball()' in the class)
    GreenfootImage image = getImage();
    image.setColor(java.awt.Color.black);
    int w = image.getWidth();
    int h = image.getHeight();
    image.drawRect(0, 0, w-1, h-1);
}
This will show the extent of the actual image unless your background is dark, in which case you can change 'black' to 'white'. Report back what you end up seeing.
You need to login to post a reply.