0

I'm working on a simple game and i need these squareBumpers which simply stands idle and when got hit, collides and reflects the ball. But currently the ball just flies through my squareBumpers. I can only use java awt and swing libraries. Here's the code:

class squareBumper {
      private int x = 300;
      private int y = 300;
      private Color color = new Color(66,139,139);

      public void paint(Graphics g) {
        Rectangle clipRect = g.getClipBounds();
          g.setColor(color);
          g.fillRect(x, y, 31, 31);
      }
   }

class BouncingBall {
  // Overview: A BouncingBall is a mutable data type.  It simulates a
  // rubber ball bouncing inside a two dimensional box.  It also
  // provides methods that are useful for creating animations of the
  // ball as it moves.

  private int x = 320;
  private int y = 598;
  public static double vx;
  public static double vy; 
  private int radius = 6;
  private Color color = new Color(0, 0, 0);

  public void move() {
    // modifies: this
    // effects: Move the ball according to its velocity.  Reflections off
    // walls cause the ball to change direction.
    x += vx;
    if (x <= radius) { x = radius; vx = -vx; }
    if (x >= 610-radius) { x = 610-radius; vx = -vx; }

    y += vy;
    if (y <= radius) { y = radius; vy = -vy; }
    if (y >= 605-radius) { y = 605-radius; vy = -vy; }
  }

  public void randomBump() {
    // modifies: this
    // effects: Changes the velocity of the ball by a random amount
    vx += (int)((Math.random() * 10.0) - 5.0);
    vx = -vx;
    vy += (int)((Math.random() * 10.0) - 5.0);
    vy = -vy;
  }

  public void paint(Graphics g) {
    // modifies: the Graphics object <g>.
    // effects: paints a circle on <g> reflecting the current position
    // of the ball.

    // the "clip rectangle" is the area of the screen that needs to be
    // modified
    Rectangle clipRect = g.getClipBounds();

    // For this tiny program, testing whether we need to redraw is
    // kind of silly.  But when there are lots of objects all over the
    // screen this is a very important performance optimization
    if (clipRect.intersects(this.boundingBox())) {
      g.setColor(color);
      g.fillOval(x-radius, y-radius, radius+radius, radius+radius);
    }
  }

  public Rectangle boundingBox() {
    // effect: Returns the smallest rectangle that completely covers the
    //         current position of the ball.

    // a Rectangle is the x,y for the upper left corner and then the
    // width and height
    return new Rectangle(x-radius, y-radius, radius+radius+1, radius+radius+1);
  }
}
DT7
  • 1,615
  • 14
  • 26
Umut
  • 409
  • 3
  • 7
  • 20
  • Is there a reason why your velocity components are `static`? – Zong Oct 22 '13 at 20:06
  • Yes, I'm getting the velocity values from xml schema which is received in a different class. – Umut Oct 22 '13 at 20:07
  • `static` shouldn't have anything to do with where values come from. It changes the semantics of the variables from instance members to class members. This means all your `BouncingBall`s will always have the same velocity. – Zong Oct 22 '13 at 20:13
  • See [this example](http://stackoverflow.com/a/14575043/418556) for tips. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 22 '13 at 20:14
  • I'll only need one ball so that's why i simply made it static. – Umut Oct 22 '13 at 20:17

2 Answers2

3

You need to detect when the ball has collided with the bumper. You have the boundingBox() method of BouncingBall, this will get you a rectangle that contains your ball. So you need to check if this rectangle intersects your square bumper (which implies a collision), and then do something with that.

hankd
  • 649
  • 3
  • 12
3

Take a look at the classes that implement the Shape interface. There are ellipses and other shapes, and they all implement a intersects(Rectangle2D) method. It might help you if you don't want to perform intersection yourself.

As for dealing with the collision, well, it depends on the level of accuracy you want. Simply deflecting the ball of edges is quite easy. Just determine whether the collided side of the rectangle is vertical or horizontal, and negate the corresponding velocity component accordingly. If you want to handle the corners, well that is a bit more complicated.

Zong
  • 6,160
  • 5
  • 32
  • 46