454,650 Members | 1,493 Online Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

# changing ball position

 P: 59 i am trying to make a breakout game where you have a paddle and ball bounces around breaking bricks. the problem is that if ball touches the corner of player(green paddle). than it goes inside of player. i could not understand why. only thing it might be bc of all the dx, -dx, dy, -dy's. i am doing this so it would change ball postion. so it looks like it bounces of the player or window. here i am check collision between ball and window. x,y,dx,dy are ball postion and speed. Expand|Select|Wrap|Line Numbers public void BallWCollision() {     //dx, dy is ball speed       if(x <= 0){   //if ball goes right of screen        dx = -dx;     }    else if(x >= Main.WINDOW_WIDTH){  //if ball goes left of screen     dx = -dx; }      if(y <= 0){    //if ball goes top of screen      dy = -dy;    }    else if(y >= Main.WINDOW_HEIGHT){  //if ball goes bottom of screen      dy = -dy;     } }   here i am check collision between player and ball. if ball touches playeer than change ball dy. Expand|Select|Wrap|Line Numbers public void playerBallCollision(Ball b) {     if(player.getBounds().intersects(b.getBounds())){  //if player and ball touch each other      b.setDy(-b.getDy());     } }   Apr 28 '13 #1
4 Replies

 Expert 2.5K+ P: 3,112 From your description, I'm guessing the problem is within code you haven't posted; how do the "getBounds()" functions work? How does "intersects(...))" work? Also, from what I can see you change the sign of the ball speeds but not the angle; from what I remember from physics, when reflected the angle of incidence equals the angle of reflection (and Wikipedia seems to agree with me). Of course, this is unrelated to the problem you mentioned. Apr 29 '13 #2 