Subscribe to Dogstopper's Code Mania        RSS Feed

Basic Bounding Boxes

Icon 2 Comments
In games, it is a very common operation to check whether or not two or more "sprites" (objects with an image) are colliding and if so, then they take action. I will now demonstrate this idea with a program that moves 2 balls closer toward each other until they meet, at which time, they will stop moving. I do this by testing a collision method in the Ball class.

But FIRST, let me tell what a bounding box is. A bounding box is a simple both (x, y, width, height) that represents the rectangular area of an image on screen. Now, in games, these images are encompassed by associated methods that aid in that image's movements, collisions, tracking, and so forth. Bounding boxes represent that area and can be checked against another to see if two sprites have collided.

So, now let me further explain using a program.

Firstly, the Ball sprite.
class Ball {
        // Set our bounds and movement speed
        private Rectangle box;
        private int xDir, yDir;

        // Set a specific position and direction.
        public Ball(int x, int y, int xSpeed, int ySpeed) {
            box = new Rectangle(x,y,20,20);
            xDir = xSpeed;
            yDir = ySpeed;

        // Move by incrementing the x and y locations.
        public void move() {
            box.setLocation((int) box.getX() + xDir,
                   (int) box.getY() + yDir);

        // If one Ball's Rectangle intersects another, then a collision occurs
        public boolean collides(Ball other) {
            if (box.intersects(other.getBounds()))
                return true;
                return false;

        // Returns the boundingBox
        public Rectangle getBounds() {
            return box;

        // Draws the circle at given spot
        public void draw(Graphics g) {
            g.fillOval((int) box.getX(), (int) box.getY(),
                    (int) box.getWidth(), (int) box.getHeight());

As you can see, it tests collision by checking another Ball for intersection. IT also provides its own draw() and move() methods, as I show in my first lesson, "Frame Rate in Java".

Now, for the panel that encompasses the Balls.
public class BallPanel extends JPanel implements ActionListener {
        // Make the two balls.
        private Ball ball1;
        private Ball ball2;

        // Run the balls toward each other.
        public BallPanel() {
             ball1 = new Ball(0,50,1,0);
             ball2 = new Ball(500,50,-1,0);

             // Set up a Timer for 15 milliseconds to call actionListener
             Timer time = new Timer(15, this);

        // Check for collision, if not, then keep moving
        public void actionPerformed(ActionEvent e) {
             if (ball1.collides(ball2)) {
                 // stop the ball, so do nothing
             else {

        // Controls the painting
        public void paintComponent(Graphics g) {
                // Paint the background
            g.fillRect(0, 0, 640, 480);

                // Draw the balla

                // makes sure monitors are up to date on *NIX systems

That's relatively straightforward. Every time actionPerformed() is executed, collision is checked, movement is made, and it is redrawn.

Now finally, the JFrame that does very minimal stuff.
public class BallFrame extends JFrame {

    BallFrame() {
        setSize(640, 480);

        add(new BallPanel());

    public static void main(String[] args) {
        new BallFrame();


I hope that has helped you understand more about sprite collision using bounding boxes, which are the most basic of collision detection. Later, you may expect bounding circles and polygons!

Thanks For Reading!
~ Stephen Schwahn

2 Comments On This Entry

Page 1 of 1

Java Student 

23 February 2010 - 08:53 PM
Did my question give you an idea for making this Bounding-box tut? :rolleyes:

Good addition to your Java blog


24 February 2010 - 01:07 PM
Actually, this has been a draft for a good long while, and I only just published it :)
Page 1 of 1

January 2021

101112131415 16

Recent Entries

Search My Blog

Recent Comments

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)