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]
# 1

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? :)

Malohkan at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 2
hi Malohkan! thanks for ur idea :) i'll try it, hope it'l work this time.P.S. yah, i noticed the "i" enclosed in a <>.. nice one :)
frustr8ed at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 3
hello again! regarding the intersect method..am i not recoding the collidesWith() method (MIDP 2.0)? just wondering..i think they're just the same, aren't they?
frustr8ed at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 4
well you don't have an "else" before "return false;" in the snippet you gave us. Is it in your code? If not, there's your problem
Malohkan at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 5

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.. :(

frustr8ed at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 6
i think my code is already working..my other problem is, once the image and the player sprite collides..the player sprite can move no longer. what must be the best way to solve this? hope someone could help :)
frustr8ed at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 7

I assume what you're doing is saying that the sprite can't go past the colliding walls. But when it collides, you need to either move it back so it's not colliding anymore, or you need to look ahead and see if it's going to collide, then say it can't move. You'll probably want to do the first one. So if the guy moves left, and runs into a sprite, move him back to the right just enough so that he's no longer colliding.

Malohkan at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...
# 8
thanks Malohkan, i'll try the first one :)
frustr8ed at 2007-7-3 18:35:40 > top of Java-index,Other Topics,Java Game Development...