changing ball position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    changing ball position

    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.
    Code:
    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.
    Code:
    public void playerBallCollision(Ball b)
    {
        if(player.getBounds().intersects(b.getBounds())){  //if player and ball touch each other
         b.setDy(-b.getDy());
        }
    }
    Last edited by Rabbit; Apr 28 '13, 04:31 AM. Reason: fixed code tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

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

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      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)

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

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

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by chaarmann
          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?

          Comment

          Working...