By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
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
  1. public void BallWCollision()
  2. {
  3.     //dx, dy is ball speed
  4.  
  5.     if(x <= 0){   //if ball goes right of screen 
  6.       dx = -dx;
  7.     }
  8.    else if(x >= Main.WINDOW_WIDTH){  //if ball goes left of screen
  9.     dx = -dx;
  10. }
  11.  
  12.    if(y <= 0){    //if ball goes top of screen
  13.      dy = -dy;
  14.    }
  15.    else if(y >= Main.WINDOW_HEIGHT){  //if ball goes bottom of screen
  16.      dy = -dy;
  17.     }
  18. }
  19.  

here i am check collision between player and ball. if ball touches playeer than change ball dy.
Expand|Select|Wrap|Line Numbers
  1. public void playerBallCollision(Ball b)
  2. {
  3.     if(player.getBounds().intersects(b.getBounds())){  //if player and ball touch each other
  4.      b.setDy(-b.getDy());
  5.     }
  6. }
  7.  
Apr 28 '13 #1
Share this Question
Share on Google+
4 Replies


Nepomuk
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

Expert 100+
P: 785
You list the code for checking that the ball does not go out of the screen. But I can't imagine what it has to do with checking the collision between paddle and ball. Please explain. For me the first should not be related to the second and the listed source code has nothing to do with your problem.

Now to the real problem: collision between paddle and ball:
Do you check collision only when ball moves, or also when paddle moves? I guess you only check in one case, because you said that the ball goes through the paddle when hit at the edge. So I can imagine following situations:
1.) Ball is at position X,Y and checked that it's free. So it moves there. Now Paddle moves at X,Y without checking. Crash!
2.) Paddle is at position X,Y and checked that it's free. So it moves there. Now Ball moves at X,Y without checking. Crash!
3.) If you have parallel running threads each for ball and paddle, following can happen even if both check:
a) Paddle is at position X,Y and checked that it's free. But it does not yet move there, because the thread is interrupted and goes to ball thread. Now Ball checks for position X,Y and moves there, because it was free. Now Paddle moves at X,Y position. Crash! (Solution: synchronize)
Apr 29 '13 #3

Expert 100+
P: 785
Nepomuk,

I guess that he does not compute with angles, but just sets the number of horizontal pixels before he moves verical and vice versa (That's how I implemented my demo years ago). So changing the sign of these dx, dy values inverts the angle. Example:
dx=1, dy=1 : The ball moves one right, one down, one right, one down etc. So it moves in 90+45 degree.
After reflection at wall or paddle:
dx=1, dy=-1 The ball moves one right, one up, one right, one up etc. So it moves in 45 degree.
Just to clarify: if you need another angle, for example half of 45 degree, you would define:
dx=1, dy=2 The ball moves one right, two up, one right, two up etc. So just inverting dy=-dy does the reflection trick.
Apr 29 '13 #4

Nepomuk
Expert 2.5K+
P: 3,112
I guess that he does not compute with angles, but just sets the number of horizontal pixels before he moves verical and vice versa
OK, that makes sense. Also, we seem to understand the problem differently, so just to clarify: @game2d, does the ball eventually bounce back or doesn't it?
Apr 29 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.