Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Collision Detection in Java Applet
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
huskiesgoaler34




PostPosted: Tue Jun 07, 2011 12:40 pm   Post subject: Collision Detection in Java Applet

Hey, I have one problem that I am having in my game. How do I check to see that there is a collision between the yellow circles and the Pac Man Sprite. I tried to add some code which returns the values of their coordinates but I don't know where to go from there. What I want the program to do is to replace the yellow circle with a black circle over it when the PacMan "eats" it (or in fact passes over it). Can someone help please. I will like help to get one collision for one circle. This will give my enough knowledge to finish the rest. Thanks in advance.

code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.awt.image.*;
import java.util.*;
import sun.audio.*;    //import the sun.audio package
import java.io.*;


public class PacMaze2 extends JApplet implements Runnable, KeyListener
{
  static Font gameFont = new Font("SanSerif", Font.BOLD, 72);
  int width;
  int height;
  int xcordPink = 150;
  int ycordBlue = 133;
  int xcordOrange =253;
  ArrayList <Rectangle> walls;
  Image backgroundBuf;
  Image screenBuf;
  Image img;
  Image cherry;
  Image pinkGhost;
  Image blueGhost;
  Image orangeGhost;
  Image finishLineCongratulations;
  boolean collision;
  JLabel player;
  Thread runner;
  int x = 355;
  int y = 255;
  int speed = 10;
  Graphics2D background;
  Thread thread;
 
  public static void Music (){
 
//  InputStream in = new FileInputStream("Opening_Sound.mp3");
//// Create an AudioStream object from the input stream.
//AudioStream as = new AudioStream(in);         
//// Use the static class member "player" from class AudioPlayer to play
//// clip.
//AudioPlayer.player.start(as);           
//// Similarly, to stop the audio.
//AudioPlayer.player.stop(as);

  }



 
  public void init ()
  {
    Music();
    collisions();
    img = getImage (getCodeBase(),"Animated_PacMan_Left.gif");
    pinkGhost = getImage (getCodeBase(),"Pink_Ghost.gif");
    blueGhost = getImage (getCodeBase(),"Blue_Ghost.gif");
    orangeGhost = getImage (getCodeBase (),"Orange_Ghost.gif");
    cherry = getImage (getCodeBase (),"Pac_Cherry.gif");
    finishLineCongratulations = getImage (getCodeBase(), "PacMan_Congrats.gif");
    player = new JLabel (new ImageIcon (img));
    player.setSize (img.getWidth (this), img.getHeight (this));
    addKeyListener(this);
    setFocusable (true);
    width = this.getWidth();
    height = this.getHeight();
    walls = new ArrayList <Rectangle> ();
    buildWalls();
       
  }
 
 
 
  public void start ()
  {
   
   
    if (thread == null )
      thread = new Thread (this);
    thread.start();
   
  }
 
  public void stop ()
  {
   if (thread != null )
    thread.interrupt();
   thread = null;
  }
 

 
 
public void buildWalls( )
 
{   // create Rectangle objects and store in ArrayList walls
 
  int wallThickness = 10;
     
  walls.add (new Rectangle (0,0, width, wallThickness));//top border
  walls.add (new Rectangle (0,0, wallThickness, height)); //left border     /BORDERS AROUND APPLET FRAME
  walls.add (new Rectangle (width-wallThickness,0, wallThickness,height));//right border
  walls.add (new Rectangle (0, height-wallThickness,width,wallThickness));//down border
 
  walls.add( new Rectangle( 50,height-wallThickness-240, wallThickness, 65 ) ); //top space divider on left side
  walls.add( new Rectangle( 50,height-wallThickness-120, wallThickness, 70 ) );  //bottom space divider on left side     
  walls.add( new Rectangle( 50,height-wallThickness-50, width-60, wallThickness ) ); //bottom horizontal piece
  walls.add( new Rectangle( 50,height-wallThickness-250, width-200, wallThickness ) ); //top horizontal piece (left)
  walls.add( new Rectangle( 285,height-wallThickness-250, width-340, wallThickness ) ); //top horizontal piece (right)
  walls.add( new Rectangle( 180,height-wallThickness-150, width-20, wallThickness ) );//middle piece (across)
  walls.add( new Rectangle( 250,height-wallThickness-100, width-20, wallThickness ) ); //lower middle piece across
  walls.add( new Rectangle( 180,height-205, wallThickness, 100 ) );  //middle piece
  walls.add( new Rectangle( 120,height-205, wallThickness, 100 ) );
  }

public void paintWalls ()
{
  backgroundBuf = createImage (width, height);
  background = (Graphics2D) backgroundBuf.getGraphics();
  background.setColor (Color.black);
  background.fillRect (0,0,width, height);
  background.setColor (Color.RED);
 
  for (int i=0; i<walls.size(); i++)
  {   
    Rectangle r = (Rectangle) walls.get (i);
 background.fillRect (r.x, r.y, r.width, r.height);
      }
}
public void paint (Graphics g)
{
 
  g.drawImage (screenBuf,0,0, this);
  g.drawImage (cherry,64,55,this);
  g.drawImage (finishLineCongratulations,358,155,this);

 // g.drawImage (img,256,42,this);  //MY TESTING PACMAN
  g.setColor (Color.BLUE);
  g.drawString("START" , 350, 250);
  g.setColor (Color.BLUE);
  g.drawString("FINISH" ,350, 150);
 
/////////////////////////////////////////////CIRCLES FOR FOOD/////////////////////////////////////////////////////////
  g.setColor(Color.YELLOW);
  g.fillOval (340,265,10,10);
  g.fillOval (300,265,10,10);
  g.fillOval (260,265,10,10);
  g.setColor(Color.GREEN);   //LOWER LEVEL
  g.fillOval (218,262,15,15);
  g.setColor(Color.YELLOW);
  g.fillOval (180,265,10,10);
  g.fillOval (140,265,10,10);
  g.fillOval (100,265,10,10);
  g.fillOval (60,265,10,10);
  g.fillOval (21,265,10,10);
   
  g.fillOval (21,225,10,10);
  g.setColor(Color.GREEN);
  g.fillOval (19,180,15,15);
  g.setColor(Color.YELLOW);
  g.fillOval (21,140,10,10);
  g.fillOval (21,100,10,10);   //left moving upward
  g.fillOval (21,60,10,10);
  g.fillOval (21,21,10,10);
   
  g.fillOval (61,21,10,10);
  g.fillOval (101,21,10,10);
  g.fillOval (141,21,10,10);   //moving right at top
  g.fillOval (181,21,10,10);
  g.fillOval (221,21,10,10);  //1
  g.fillOval (261,21,10,10);
  g.fillOval (300,21,10,10);
  g.fillOval (338,21,10,10);
  g.fillOval (371,21,10,10);
 
  g.setColor(Color.GREEN);
  g.fillOval (369,57,15,15);
  g.setColor(Color.YELLOW);
  g.fillOval (338,61,10,10);
  g.fillOval (300,61,10,10); //2
  g.fillOval (261,61,10,10);
  g.fillOval (221,61,10,10);
  g.fillOval (181,61,10,10);
  g.fillOval (141,61,10,10);  //second line of food
  g.fillOval (101,61,10,10);
 
  g.fillOval (371,120,10,10);
  g.fillOval (338,120,10,10);
  g.fillOval (300,120,10,10);
  g.fillOval (261,120,10,10); //4
  g.fillOval (221,120,10,10);

  g.fillOval (371,90,10,10);
  g.fillOval (338,90,10,10);  //3
  g.fillOval (300,90,10,10);
  g.fillOval (261,90,10,10);
  g.fillOval (221,90,10,10);
  g.fillOval (150,90,10,10);  ///ANGLED FOOD CIRCLE
  g.fillOval (101,95,10,10);
 
  g.fillOval (371,215,10,10);
  g.fillOval (338,215,10,10);
  g.fillOval (300,215,10,10);
  g.fillOval (260,215,10,10);
  g.fillOval (218,215,10,10);
  g.fillOval (178,215,10,10);
  g.fillOval (138,215,10,10);
 
 
  g.fillOval (103,215,10,10);
  g.fillOval (103,175,10,10);
  g.fillOval (103,135,10,10);
  g.fillOval (65,215,10,10);
  g.fillOval (65,175,10,10);
  g.fillOval (65,135,10,10);
  g.fillOval (65,95,10,10);
 
   
  g.fillOval (338,165,10,10);
  g.fillOval (300,165,10,10);  //5
  g.fillOval (260,165,10,10);
  g.fillOval (218,165,10,10);
 
  g.fillOval (150,90,10,10);
  g.fillOval (150,130,10,10);
  g.fillOval (150,170,10,10); 
 
  g.drawImage (img,x,y,this);//345,255
 
  g.drawImage (pinkGhost,xcordPink,255,this);//pink ghost sprite
  xcordPink--;
  if (xcordPink <-1)
  {
     xcordPink = 150;

  }
  g.drawImage (blueGhost,13,ycordBlue,this);
  ycordBlue--;
  if (ycordBlue <-1)
  {
    ycordBlue = 133; //blue ghost sprite
  }
  g.drawImage (orangeGhost,xcordOrange,84,this);
  xcordOrange--;
  if (xcordOrange <-1)
  {
    xcordOrange = 253; //blue ghost sprite
  }
 
}


public void keyReleased (KeyEvent key) {}   //have to be here (all 3 methods)
public void keyTyped (KeyEvent key) {}
public void keyPressed (KeyEvent key) {
  int code = key.getKeyCode();
  if (code == KeyEvent.VK_UP)
   y-=speed;
  if (code == KeyEvent.VK_DOWN)
    y+=speed;
  else if (code == KeyEvent.VK_LEFT)
    x-=speed;
  else if (code == KeyEvent.VK_RIGHT)
    x+=speed;
  player.setLocation (x,y);
}

//public boolean checkCollisionWalls (Rectangle entityAura)          //?????????????????
//{
//  for (int i= 0; i<walls.size (); i++)
// {
//   if (entityAura.intersects (walls.get(i)))
//     return true;
// }
//  return false;
//}
 

public Rectangle boundariesPacMan ()

  return (new Rectangle (x,y,28,28));
 }
public Rectangle boundariesFood ()

  return (new Rectangle (x,y,340,265));
 }


public void collisions ()
{
 
// if ()
 
 
 
 
}
public void run ()
{
  paintWalls ();
  screenBuf = createImage (width, height);
  Graphics gapplet = (Graphics2D) screenBuf.getGraphics();
 
  while (true)
  {
    long time = System.currentTimeMillis();

    gapplet.drawImage (backgroundBuf, 0,0, this);
    repaint();
    time = (1000) - (System.currentTimeMillis() - time);
    try {
      Thread.sleep (10);
    } catch (Exception ex) { stop (); }
  }
   
}

}
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Jun 07, 2011 2:03 pm   Post subject: RE:Collision Detection in Java Applet

Posted Image, might have been reduced in size. Click Image to view fullscreen.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
huskiesgoaler34




PostPosted: Tue Jun 07, 2011 3:03 pm   Post subject: Re: Collision Detection in Java Applet

Oh man...that ruined my day... Wink
RandomLetters




PostPosted: Tue Jun 07, 2011 3:49 pm   Post subject: RE:Collision Detection in Java Applet

Since you can only move in orthogonal directions in pacman, d simplifies to x2-x1 or y2-y1 depending on direction
huskiesgoaler34




PostPosted: Tue Jun 07, 2011 4:48 pm   Post subject: Re: Collision Detection in Java Applet

ok thanks for the heads up
RandomLetters




PostPosted: Tue Jun 07, 2011 5:05 pm   Post subject: RE:Collision Detection in Java Applet

To use collision though, you'll need to reconsider how you draw circles.

No matter what type of collision you use, you'll need to keep the dots in an array, so that you can get their coordinates.

for example,

public class PacMan extends... {
Food[] foods;
}

class Food {
int x, y;
}
huskiesgoaler34




PostPosted: Tue Jun 07, 2011 5:07 pm   Post subject: Re: Collision Detection in Java Applet

would it be better to use an arraylist
RandomLetters




PostPosted: Tue Jun 07, 2011 5:10 pm   Post subject: RE:Collision Detection in Java Applet

Sure. I just like the simple dot notation with arrays.
Sponsor
Sponsor
Sponsor
sponsor
huskiesgoaler34




PostPosted: Sun Jun 12, 2011 3:58 pm   Post subject: Re: Collision Detection in Java Applet

ok, so i tried everything and i am trying to come up with a very simple solution. if the pacman is at x and y, then there is a collision because the PacMan is over the circle (food). now for some reason, as soon as i start the program, a collision is detected. why is that? here is my code that deals with the collision detection:

code:


public boolean collision ()
{
 
 collision = false;
 
 if (x == 335 && y == 255){
   collision = true;
   return (collision);
 }else {
   collision = false;
   return (collision);
 }
 
}



The variable value is passed to the paint method and the if statement is executed:

code:


if (collision = true)
  {
    g.setColor(Color.BLACK);
    g.fillOval (340,265,10,10);
   
      }

RandomLetters




PostPosted: Sun Jun 12, 2011 5:25 pm   Post subject: RE:Collision Detection in Java Applet

if (collision = true)

that's not a method call, or a comparison.

that statements takes a variable collision and sets it to true.
Zren




PostPosted: Sun Jun 12, 2011 5:40 pm   Post subject: RE:Collision Detection in Java Applet

public boolean collision () { return (x == 335 && y == 255); }

is the short form of:

public boolean collision ()
{

collision = false;

if (x == 335 && y == 255){
collision = true;
return (collision);
}else {
collision = false;
return (collision);
}

}

Remember that if statement deal with comparisons which are boolean statements. No point in spinning up pointless variables.
huskiesgoaler34




PostPosted: Sun Jun 12, 2011 7:02 pm   Post subject: Re: Collision Detection in Java Applet

Ok, I got the collision checking to work for the first piece of food. Now, I am wondering, when I draw the black circle over the yellow circle, is there a way to stop the flickering. Here is my code:

code:


g.setColor(Color.YELLOW);
g.fillOval (340,265,10,10);



This draws the original yellow food.

code:


 collision = false;
 
 if (x <= 335 && y == 255){
   collision = true;
   g.setColor(Color.BLACK);
   g.fillOval (340,265,10,10);
     
 }else {
   collision = false;
   
 }



This is the collision checking to see if the food has been eaten. If so, then draw black circle over the original yellow circle. Is there anyway to prevent the flickering?
huskiesgoaler34




PostPosted: Sun Jun 12, 2011 7:15 pm   Post subject: Re: Collision Detection in Java Applet

Is there a way to "terminate" the original drawing of the food command?

For example:

If collision is detected; don't draw circle to screen
Zren




PostPosted: Sun Jun 12, 2011 7:25 pm   Post subject: RE:Collision Detection in Java Applet

What about when there isn't a collision (after you ate it)? You don't want it to show then. Make an ArrayList of food and remove them as their eaten ( or a fixed array with a Food object with an isNommed flag.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: