#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Chaning balls postion


    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.
    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;
    }
    }
    [/code]


    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());
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    problem is that if ball touches the corner of player(green paddle). than it goes inside of player.
    Have you tried debugging the code by adding printlns to show the positions of the ball and the paddle when that happens so you can see what values the computer sees when it executes? Why does the code move the ball inside the player? What are the values used to make that decision?

IMN logo majestic logo threadwatch logo seochat tools logo