help in collision detection between sprite and images
hello! need help in detecting collision between my sprite and my images. im developing a game whereas i have a
player sprite and some images such as trees, buildings, rocks etc. i want to detect whenever the player sprite collides with the rock or trees or any of my images. i used CollidesWith(image,int,int,boolean) but it doesn't work. do i need to put my images on a layerManager? by the way, i stored my images and their x & y coordinates (locations) in an array. my collision code goes this way:
public boolean checkCollision (Sprite Psprite) {/* Psprite is my player sprite */
...
if (PSprite.CollidesWith(images[..],Xcoordinate[..],Ycoordinate[..],true) /* images[..] is an array to my images
such as trees, rocks etc */
return true;
return false;
}
need help so badly.. :(
[862 byte] By [
frustr8ed] at [2007-9-30 10:23:36]

A better idea for storing your images would be to have all the images in one array. Have stuff like...public static final int
TREE = 0,
BUSH = 1,
GRASS = 2,
DIRT = 3,
PLAYER = 4;
Then in your game you have a Sprite object, a class you would create yourself, store 3 ints: image, x, and y. Then when you draw in your game loop, to draw a Sprite it looks like:g2d.drawImage(images[sprite.getImage()], sprite.getX(), sprite.getY(), null);
//Or if you want the sprite to have it's own rendering method:
//
//Main game class:
public static final Image[] images; //initialize these
public void drawSprites(Graphics2D g2d) {
for (int i = 0; i < sprites.length; i++) {
sprites[i].render(g2d);
}
}
//
//In Sprite class:
public void render(Graphics2D g2d) {
g2d.drawImage(MainClass.images[image], x, y, null);
}
This way you can avoid storing the same image in multiple locations. Now, as for collisions... You know the x and y coordinates of your sprite and your player sprite, so essentially you have the information to represent a "rectangle" surrounding them. Now if you look at the Rectangle.intersects(Rectangle) method, you see:/**
* Tests if the interior of this <code>Rectangle2D</code>
* intersects the interior of a specified set of rectangular
* coordinates.
* @param x,y the coordinates of the upper left corner
* of the specified set of rectangular coordinates
* @param w the width of the specified set of rectangular
* coordinates
* @param h the height of the specified set of rectangular
* coordinates
* @return <code>true</code> if this <code>Rectangle2D</code>
* intersects the interior of a specified set of rectangular
* coordinates; <code>false</code> otherwise.
* @since 1.2
*/
public boolean intersects(double x, double y, double w, double h) {
if (isEmpty() || w <= 0 || h <= 0) {
return false;
}
double x0 = getX();
double y0 = getY();
return (x + w > x0 &&
y + h > y0 &&
x < x0 + getWidth() &&
y < y0 + getHeight());
}
So I'm sure you can take that nice little snippet of code and adapt it to the information you have about your sprites to create a working collidesWith() method. Good luck!
PS: I just noticed something... the board now changes the [] to <> around my "i" to avoid italicizing, isn't that nice? :)
hi again! here's my original code:
Note: I got the X and Y coordinates in a single array.
// Psprite is my player sprite
public boolean checkCollision(Sprite Psprite) {
int xyCntr = 0;
// elemCntr is the counter to the images array
int elemCntr = map.getElemCntr();
Image[] ELEMENTS = new Image[100];
ELEMENTS = map.getElements(); /* ELEMENTS contains my images */
int[] XYcoordinates = new int[200];
XYcoordinates = map.getXYcoordinates();/* contains the XY coordinates of my images */
Psprite.defineCollisionRectangle(Psprite.getX(),Psprite.getY(),30,50);
for (int eCntr = 0; eCntr < elemCntr; eCntr++) {
if (Psprite.collidesWith(ELEMENTS[eCntr], XYcoordinates[xyCntr], XYcoordinates[(++xyCntr)], true)) {
return true;
}
xyCntr++;
}
return false;
}
my collision detection seems not to work. need help really.. :(