Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: bounding box collision

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default bounding box collision

    hi im trying to make a packman style game for an assignment but the problem im having is i cant remove the munchies which the monster is supposed to eat,
    im trying to make an array that will remove the munchies. ive made an array called munchie that i want setting to 1 if it is to be draw and if it is 0 then the munchie will disapear, ive highlighted in green the bit im having problems with.

    i hope im making sence

    please help thankyou

    import javax.swing.*;
    import java.applet.AudioClip;
    import java.awt.*;
     
    /**
     *
     * <p>Title: MonsterGame</p>
     * <p>An interactive animation with Munchies and user-controlled Monster</p>
     * <p>Copyright: Copyright (c) 2005</p>
     * @author Cathy French
     * @version 1.2   September 2006
     */
    public class MonsterGame extends ICGGSPanel
    {
        // game constants
        private final int NUM_MUNCHIES = 5;   // the number of munchies to be displayed
     
        // game information
        private  int score;  // player score (not currently used)
        private  int boardWidth, boardHeight;  // game board dimensions
     
        // Munchie data
        private  int[] munchieXPos;  // x coordinate of top left corner of each munchie image
        private int[] munchieYPos;   // y coordinate of top left corner of each munchie image
        private int[] munchieAnimationCount;  // current animation counter for each munchie
        private  Image munchieImage1, munchieImage2;  // the images to display
        private int munchieWidth, munchieHeight;  // munchie dimensions
     
     
        // Monster data
        private  int monsterXPos, monsterYPos;  // coordinates of top left corner of monster
        private int monsterSize;  // size of monster
        private  int monsterSpeed;  // monster speed in pixels per frame
        private  Color monsterColour;  
        private  int monsterDirection;  // direction monster is moving
        private  int monsterAnimationCount;  // animation counter for monster
        private AudioClip monsterBumpWallSound;  // sound to play when monster hits a wall
     
     
     
        int[]munchie;
     
        int arcAngle;
        int startAngle = 0;
        int startAngle2;
        /**
         * Constructor - creates a game using the two images and audio clip
         * @param image1 Image - the first image for the munchies
         * @param image2 Image - the second image for the munchies
         * @param theAudio AudioClip - sound to be played when the monster hits a wall
         */
        public MonsterGame(Image image1, Image image2, AudioClip theAudio)
        {
            // initialise Munchie information
            // create the arrays, need one "slot" for each munchie
            munchieXPos = new int[NUM_MUNCHIES];
            munchieYPos = new int[NUM_MUNCHIES];
            munchieAnimationCount = new int[NUM_MUNCHIES];
            // store the munchie images
            munchieImage1 = image1;
            munchieImage2 = image2;
            // the munchie is the same size as the loaded image
            munchieWidth = image1.getWidth(null);
            munchieHeight = image1.getHeight(null);
     
     
            // initialise the Monster information
            monsterColour = Color.red;
            monsterBumpWallSound = theAudio;
            monsterSize = 30;
     
            // initialise background colour
            this.setBackground(Color.WHITE);
        }
     
        /**
         * Use to reset the game
         * sets score to zero
         * sets up Monster and munchies in random positions
         */
     
        public void resetGame()
        {
            // make the game board the same size as the panel
            boardWidth = this.getWidth();
            boardHeight = this.getHeight();
            score = 0;
            // reset the Monster to a random position and direction
            monsterXPos = (int)(Math.random() * (boardWidth - monsterSize));
            monsterYPos = (int)(Math.random() * (boardHeight - monsterSize));
            monsterDirection = (int)(Math.random() * 4) + 1;
            monsterSpeed = 5;
            monsterAnimationCount = 0;
     
            // reset all the munchies to random positions and different animation counts
            for (int i = 0; i < NUM_MUNCHIES; i++) {
                munchieXPos[i] = (int)(Math.random() * (boardWidth - munchieWidth));
                munchieYPos[i] = (int)(Math.random() * (boardHeight - munchieHeight));
                munchieAnimationCount[i] = i % 10;
            }
     
        }
     
        /**
         * overrides superclass method
         * processes user input
         * @param input int - code for the lastest user input
         */
        public void processInput(int input)
        {
             if (input > 0 && input < 5)  // if the input is a valid direction
                monsterDirection = input;  // set the current direction to the input
        }
     
        /**
         * overrides superclass method
         * updates the game objects
         */
        public void update()
        {
            // update moster direction and animation
             switch (monsterDirection)
            {
              case Input.RIGHT:
                  startAngle2 = startAngle = 10;
                  // monster is moving right, increase its X-coordinate
                  monsterXPos = monsterXPos + monsterSpeed;
                  if (monsterXPos > (boardWidth - monsterSize)) // has the monster hit the right wall?
                  {
                      monsterXPos = boardWidth - monsterSize;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
     
                  }
     
                  monsterDirection = Input.NONE;
     
                  break;
     
                  case Input.LEFT:
                      startAngle2 = startAngle = 185;
                      monsterXPos = monsterXPos - monsterSpeed;
    if (monsterXPos < 0) // has the monster hit the right wall?
                  {
                      monsterXPos = 0;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
                  }
     
                      monsterDirection = Input.NONE;
     
                      break;
     
                      case Input.DOWN:
                          startAngle2 = startAngle = 280;
                  //monster is moving right, increase its X-coordinate
                  monsterYPos = monsterYPos + monsterSpeed;
                  if (monsterYPos > (boardWidth - monsterSize)) // has the monster hit the right wall?
                  {
                      monsterYPos = boardWidth - monsterSize;  // move it back to just touching right wall
                      monsterBumpWallSound.play();  // play an appropriate sound
                  }
     
                  monsterDirection = Input.NONE;
     
                   break;
     
                  case Input.UP:
                      startAngle2 = startAngle = 100;
                      monsterYPos = monsterYPos - monsterSpeed;
                            if (monsterYPos < 0) // has the monster hit the right wall?
                                {
                                    monsterYPos = 0;  // move it back to just touching right wall
                                    monsterBumpWallSound.play();  // play an appropriate sound
                                }
     
                      monsterDirection = Input.NONE;
     
     
                  // to do - fill in appropriate code for other directions
            }
     
     
     
     
             if (monsterAnimationCount < 5)
            {
     
                // for frames 0 to 4, his mouth gradually closes
                arcAngle = 310 + 10 * monsterAnimationCount;
                startAngle2 = startAngle2 - 5*monsterAnimationCount;
            }
            else
            {
     
                 //for frames 5 to 9, it opens again
                arcAngle = 310 + (10  - monsterAnimationCount) * 10;
                startAngle2 = startAngle2 - (10 - monsterAnimationCount) * 5;
            }
     
     
     
     
     
             // increment the monster animation counter 
           monsterAnimationCount++;
           if (monsterAnimationCount >= 10)  // when the counter gets to 10, set it back to 0
               monsterAnimationCount = 0;
     
           // update each munchie animation
            for (int i = 0; i < NUM_MUNCHIES; i++)
            {
               // increment the animation counter 
                munchieAnimationCount[i]++;
                if (munchieAnimationCount[i] >= 10) // when the counter gets to 10, set it back to 0
                    munchieAnimationCount[i] = 0;
            }
        }
     
     
        /**
         * called by system after repaint() call (or Applet moved or resized)
         * @param g Graphics  object for painting on
         */
        public boolean collisionDetection(int xPos1, int yPos1, int Height1, int Width1, int xPos2, int yPos2, int Height2, int Width2)
    {
     
    int bottom1, bottom2;
    int top1, top2;
    int left1, left2;
    int right1,right2;
     
    left1= xPos1;
    left2 =xPos2;
    right1 = xPos1+Width1;
    right2 = xPos2+Width2;
    top1 = yPos1;
    top2 = yPos2;
    bottom1 = yPos1+Height1;
    bottom2 = yPos2+Height2;
     
    if (bottom1 < top2) return(true);
    if (top1 > bottom2) return(true);
     
    if (right1 < left2) return(true);
    if (left1 > right2) return(true);
     
    return (false);
    }
     
     
    public void paintComponent(Graphics g)
     
    {
     
    super.paintComponent(g); // paint the default panel
     
    // paint each of the Munchies in turn
    //if(munchie > 0 )
    {
     
    for (int i = 0; i < NUM_MUNCHIES; i++)
     
      [COLOR="lime"]if(munchie[i]>1 )[/COLOR]
     
    {
    Image im; // this is the image to be displayed
    if (munchieAnimationCount[i] < 5)
    {
    im = munchieImage1;
    }
    else
    {
    // display the second image when the counter is between 5 and 9
    im = munchieImage2;
    }
     
    // draw the image in the correct position
    g.drawImage(im, munchieXPos[i], munchieYPos[i], null);
    }
     
    g.setColor(monsterColour);
    g.fillArc(monsterXPos, monsterYPos, monsterSize, monsterSize, startAngle, arcAngle);
    g.setColor(Color.BLACK);
    g.drawRect(0, 0, boardWidth-1, boardHeight-1);
     
     
     
    //then to implement:
    for(int i=0;i<NUM_MUNCHIES;i++)
     
    {
    boolean Flag = collisionDetection(monsterXPos, monsterYPos, monsterSize,monsterSize ,munchieXPos[i],munchieYPos[i],20,20);
     
    if (Flag==false)
    {
    [COLOR="Lime"]munchie[i]=0;[/COLOR]
     
    score=score+100;
    Flag=true;
    }
    }
    }
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: bounding box collision

    First, if this is the code in its entirety, it doesn't look like the array munchie is allocated (eg munchie = new int[NUM_MUNCHIES] )
    Second:
    Quote Originally Posted by beechy34 View Post
    ive made an array called munchie that i want setting to 1 if it is to be draw and if it is 0 then the munchie will disapear
    if ( munchie[i]>1 ){
    //draw image
    }

    The conditional above does not follow the guidelines you laid out for 1 to draw, 0 to not draw...perhaps try
    if ( munchie[i] == 1 ){
    //draw image
    }

    Hope this helps

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bounding box collision

    hay thats helped me alot thanks,
    but the munchies still wont disapear when the monster goes over them.
    here is an explanation of what someone told me should happen

    the flag gets set to false if their's been a collision so that particular munchie gets set a value of one. If you go up to your Draw section and just add a line checking whether the value is larger than 0 before the munchie is drawn, it shouldn't render it to the screen.

    here is the code again but just a small piece
    public boolean collisionDetection(int xPos1, int yPos1, int Height1, int Width1, int xPos2, int yPos2, int Height2, int Width2)
    {
     
    int bottom1, bottom2;
    int top1, top2;
    int left1, left2;
    int right1,right2;
     
    left1= xPos1;
    left2 =xPos2;
    right1 = xPos1+Width1;
    right2 = xPos2+Width2;
    top1 = yPos1;
    top2 = yPos2;
    bottom1 = yPos1+Height1;
    bottom2 = yPos2+Height2;
     
    if (bottom1 < top2) return(true);
    if (top1 > bottom2) return(true);
     
    if (right1 < left2) return(true);
    if (left1 > right2) return(true);
     
    return (false);
    }
     
     
    public void paintComponent(Graphics g)
     
    {
        int munchie[]= new int[NUM_MUNCHIES];
     
    super.paintComponent(g); // paint the default panel
     
    // paint each of the Munchies in turn
    //if(munchie > 0 )
    {
     
    for (int i = 0; i < NUM_MUNCHIES; i++)
     
      if(munchie[i]==0 )
     
    {
    Image im; // this is the image to be displayed
    if (munchieAnimationCount[i] < 5)
    {
    im = munchieImage1;
    }
    else
    {
    // display the second image when the counter is between 5 and 9
    im = munchieImage2;
    }
     
    // draw the image in the correct position
    g.drawImage(im, munchieXPos[i], munchieYPos[i], null);
    }
     
    g.setColor(monsterColour);
    g.fillArc(monsterXPos, monsterYPos, monsterSize, monsterSize, startAngle, arcAngle);
    g.setColor(Color.BLACK);
    g.drawRect(0, 0, boardWidth-1, boardHeight-1);
     
     
     
    //then to implement:
    for(int i=0;i<NUM_MUNCHIES;i++)
     
    {
    boolean Flag = collisionDetection(monsterXPos, monsterYPos, monsterSize,monsterSize ,munchieXPos[i],munchieYPos[i],20,20);
     
    if (Flag==false)
    {
    munchie[i]=1;
     
     
    //score=score+100;
    Flag=true;
    }
    }
    }
    }
    }

    please help me thanks

Similar Threads

  1. big problems bounding() in Java2d with LineBreakMeasurer.
    By fenderman in forum AWT / Java Swing
    Replies: 0
    Last Post: July 27th, 2009, 01:15 PM
  2. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM