0

My goal is getting the ball to be centered at the paddle even if the value of the ball radius were to change in future versions of the game. My only problem is implementing the correct math formula for the x coordinate of the gameball. I got the y coordinate formula working perfectly.

I do not need the correct answer. I just need guidance and suggestions to get the answer.

Here is a picture of the java program:

http://i33.photobucket.com/albums/d86/warnexus/ball.png

You can find the code below the comment "// Trouble figuring the math here".

    /** Radius of the ball in pixels */
private static final int BALL_RADIUS = 500;


private void setup_Paddle()
{
    // TODO Auto-generated method stub

    // x coordinate of the upper left corner
    // y coordinate of the upper left corner

    paddle = new GRect(20,20,PADDLE_WIDTH,PADDLE_HEIGHT);
    paddle.setFilled(true);
    paddle.setColor(Color.PINK);
    add(paddle,paddleInitialLocationX,paddleInitialLocationY);

}

private void setup_Ball()
{

    // Trouble figuring the math here
    int ballSetUpCoordX = (int) (paddle.getX());
    // Good Code!
    int ballSetUpCoordY = (int) (paddle.getY()-BALL_RADIUS);

    gameBall = new GOval(BALL_RADIUS,BALL_RADIUS);
    gameBall.setFilled(true);
    gameBall.setColor(Color.BLUE);

    add(gameBall,ballSetUpCoordX,ballSetUpCoordY);
}

    private GOval gameBall;
    private GRect paddle;
    private int paddleInitialLocationX = 200;
    private int paddleInitialLocationY = 500;
Nicholas
  • 679
  • 2
  • 11
  • 29

1 Answers1

2

Coordinates are generally for the top left corner of an object. So to get any two objects o1 and o2 to be centered the same place, you have to offset based on size.

Here we'll move o1's center to o2's center.

int o2CenterX = o2.x - (o2.width/2);
//If we just used o2CenterX, it would put the corner of o1 into the center of o2
o1.x = o2CenterX - (o1.width/2);

Repeat for y, which you appear to have already done(radius serves as width/2). You will likely have to adjust this formula slightly unless you want the paddle and ball intersecting on the screen.

Thomas
  • 5,074
  • 1
  • 16
  • 12
  • I tried what you said but it does not seem right unless I'm coding it wrong: int center = (int) (paddle.getX() - (PADDLE_WIDTH/2)); int ballSetUpCoordX = (int) (center -(PADDLE_WIDTH)/2); – Nicholas Jul 01 '12 at 00:23
  • it still a good suggestion I would vote up the comment but I need 4 more reps. – Nicholas Jul 01 '12 at 01:26
  • You've got the second line wrong. It needs to be offset by the ball width. – Thomas Jul 01 '12 at 02:59
  • I took your input and fix it. The ball width is the ball's radius : int center = (int) (paddle.getX() - (PADDLE_WIDTH/2)); int ballSetUpCoordX = (int) (center - (BALL_RADIUS)/2); If I make the ball's radius small like 50, the ball is set up far left of the paddle instead of on the top center of the paddle. Thanks for the feedback so far Thomas! – Nicholas Jul 01 '12 at 22:07
  • I played around with the code and this formula seems to work the best for all possible positive value of the ball radius: int ballSetUpCoordX = (int) (paddle.getX() - (BALL_RADIUS/2)); Again thanks for giving me feedback Thomas! – Nicholas Jul 01 '12 at 22:36
  • o2CenterX needs to be `o2.x + (o2.width/2)`; plus instead of minus. Otherwise it looks good. – dash-tom-bang Jul 02 '12 at 00:28
  • Ah yes, that's right! Got the definitive code for all radius values. Thanks Thomas! Here's the code: int center = (int) (paddle.getX() + (PADDLE_WIDTH/2)); int ballSetUpLocationX = (int) (center - (BALL_RADIUS /2)); – Nicholas Jul 02 '12 at 19:09