Collision detection between images

Hi everyone. I have been creating a game for my Computer science coursework. So far i have the animation and the mouse movement working without any problem, but i can't get the collision detection to work. Can anyone have a look at the code and see what is wrong.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.awt.image.*;

import java.awt.geom.*;

public class GameDesignPanel extends JPanel implements MouseListener,

MouseMotionListener,

Runnable{

Thread runner;

BoundedImage cow, cow1, cow2, cow3;

BoundedImage person, pick;

BoundedImage tree, tree1, tree2, tree3, tree4;

BoundedImage pub;

Image personObj;

int personX;

int personY;

Image pubObj;

Image cowObj;

Image treeObj;

int x = 10;

int y = 300;

int moveAmount = 5;

// List of BoundedImages, that will represent the person and the scene Objects

private LinkedList sceneObjects;

public GameDesignPanel(int personX1, int personY1) {

super();

sceneObjects = new LinkedList();

pick = null;

personX = personX1;

personY = personY1;

setBackground(Color.white);

// Declare the cow object

Toolkit kit = Toolkit.getDefaultToolkit();

cowObj = kit.getImage("cow.jpg");

cow = new BoundedImage(cowObj, this);

cow1 = new BoundedImage(cowObj, this);

cow2 = new BoundedImage(cowObj, this);

cow3 = new BoundedImage(cowObj, this);

treeObj = kit.getImage("tree.jpg");

tree = new BoundedImage(treeObj, this);

tree1 = new BoundedImage(treeObj, this);

tree2 = new BoundedImage(treeObj, this);

tree3 = new BoundedImage(treeObj, this);

tree4 = new BoundedImage(treeObj, this);

//Declare the pub Object

pubObj = kit.getImage("pub.jpg");

pub = new BoundedImage(pubObj, this);

//Declare the person object

personObj = kit.getImage("cartman1.gif");

person = new BoundedImage(personObj, this);

Random r = new Random();

int width = (int)getSize().getWidth();

int height = (int)getSize().getHeight();

cow1.movePosition(new Point(20, 40));

sceneObjects.add(person);

sceneObjects.add(cow);

sceneObjects.add(cow1);

sceneObjects.add(cow2);

sceneObjects.add(cow3);

sceneObjects.add(tree);

sceneObjects.add(tree1);

sceneObjects.add(tree2);

sceneObjects.add(tree3);

sceneObjects.add(tree4);

sceneObjects.add(pub);

addMouseListener(this);

addMouseMotionListener(this);

// Assign all the Images to the relevant BoundedImage

}

public void paintComponent(Graphics g){

Graphics2D g2D = (Graphics2D)g;

BoundedImage bi;

g2D.setColor(Color.white);

g2D.fillRect(0, 0, getSize().width, getSize().height);

g2D.drawImage(cow.getImage(), x, y, this);

g2D.drawImage(person.getImage(), person.getTransform(), this);

g2D.drawImage(cow1.getImage(), cow1.getTransform(), this);

g2D.drawImage(cow2.getImage(), 130, 100, this);

g2D.drawImage(cow3.getImage(), 300, 20, this);

g2D.drawImage(tree.getImage(), 10, 500, this);

g2D.drawImage(tree1.getImage(), 30, 400, this);

g2D.drawImage(tree2.getImage(), 300, 200, this);

g2D.drawImage(tree3.getImage(), 500, 357, this);

g2D.drawImage(tree4.getImage(), 340, 240, this);

I CAN'T UNDERSTAND WHY THIS DOESN'T WORK

if(person.getBounds2D().intersects(tree4.getBounds2D())){

g2D.drawString("colllision", 10, 30);

}

}

/**

* Starts the animation

*/

void playAnimation() {

if (runner == null) {

runner = new Thread(this);

runner.start();

}//if

}// playAnimation()

/**

*Stops the animation

*/

void stopAnimation() {

if (runner != null) {

runner = null;

}//if

}// stopAnimation()

public void run() {

while(true){

x += moveAmount;

walk(10, getSize().width + 20);

if ((x > 400) | (x < 5))

x *= -1;

repaint();

try {

Thread.sleep(100);

} catch (InterruptedException e) {}

}

}

public void walk(int start, int end){

for (int i = start; i < end; i +=moveAmount){

x = i;

//x *= -1;

repaint();

pause(150);

}

}

public void pause(int time){

try {

Thread.sleep(time);

} catch (InterruptedException e) {}

}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e){

if(pick == null){

BoundedImage bi;

for(int i = 0; i < 11; i++){

bi = (BoundedImage)sceneObjects.get(0);

//Pick up person

if(bi.getBounds2D().contains(e.getPoint())){

person = bi;

return;

}

}

}

}

public void mouseReleased(MouseEvent e){

repaint();

}

// method from mousemotionlisteren

public void mouseDragged(MouseEvent e){

if(person != null){

person.movePosition(e.getPoint());

repaint();

}

}

public void mouseMoved(MouseEvent e){

}

}

Any help or suggestions would be really appreciated. Thank you

[5447 byte] By [jamesloughneya] at [2007-9-28 5:15:24]
# 1
I suggest reposting the code using [code] tags, so we can read it.
YATArchivista at 2007-7-9 16:26:29 > top of Java-index,Other Topics,Java Game Development...
# 2
Using paint of color on area of same color to examine test results?: System.out.println("Ouch! Who put that tree there?");
smokingbevela at 2007-7-9 16:26:29 > top of Java-index,Other Topics,Java Game Development...
# 3

Okay, I'm going to assume that BoundedImage is a class you were supplied with, or maybe wrote yourself. I don't know of any class by that name in J2SE.

I think the problem is that you never set the location of your BoundedImage objects. You have hardcoded positions in your paintComponent() method, which probably belong on the BoundedImages themselves.

Your code to test the intersection calls: tree4.getBounds2D()

but you never set the bounds of tree4. Simply painting the image on the screen does not set the bounds of the BoundedImage.

Probably the right thing to do (I'm guessing a bit as I don't have the source for BoundedImage) is to put code along these lines in the GameDesignPanel constructor:

cow1.setBounds2D(new Rectangle2D.Float(5, 5, 60, 60));

cow2.setBounds2D(new Rectangle2D.Float(130, 100, 50, 50));

cow3.setBounds2D(new Rectangle2D.Float(200, 120, 50, 50));

// etc...

You probably don't want to set the width and height of the image though. You should get its dimension from the image itself and use those. But you *do* want to set the location (x, y) of the BoundedImage.

Then use the location you set on it in the paintComponent() method instead of the hardcoded numbers you have there now.

If you're still having problems, you'll have to post the source of BoundedImage.

Cheers,

-c

cdbennetta at 2007-7-9 16:26:29 > top of Java-index,Other Topics,Java Game Development...
# 4

i like to make each of my images a member of a class extending Shape which draws itself and the image. then, in the main routine, you can call

if(circle[a].intersects(circle[b])){

...reverse directions or kill, whatever

}

outawatera at 2007-7-9 16:26:30 > top of Java-index,Other Topics,Java Game Development...
# 5

Cheers guys, I will give those suggestions a try. I handed the project in before christmas with some collision code that was hacked together the night before, but I am still going to try out your ideas, cos hopefully it will give me better working code for the next game that I try to write.

Well got to go, but thanks again for the help.

all the best, James :)

jamesloughneya at 2007-7-9 16:26:30 > top of Java-index,Other Topics,Java Game Development...