Hello guys,
first post here on the forum ^^
Story:
Well i have an very old game in mind, but i forgot the name.
It looked simple and easy to recode, but well…
Its some sort of “PixelEater”, i will post if someone solves my problem
There is a simple “Pixel” class which just has coordinates and a color.
This “pixels” draw method just draws a 3x3 square on a 2D surface.
Now that the pixels dont overlap i have to do a collision detection.
Well since this “Pixels” are small and there shall be a ton (maybe 1000-2000, if i remember correctly from the old game),
they need to collide.
Well, atm the best solution i got is to do at every beginning of a workcycle, generate a 2 dimensional boolean Array with true = blocked and false = not blocked(no pixel here).
and then each pixel checks if he can moves in the direction he want to.
It goes good the first 10 pixels with no prob, but if u get above ~ 1500 it gets laggy as hell and 1 cpu core i dying…
Is there any way to get it faster, when all pixels should be moved, and with a reasonable speed ?
well heres the code…
“Pixel’s” collsion, called by Slick2D engine:
(pixelMap = booleanArray)
private void checkCollision(GamePlayState game, GameContainer container) {
int xc, yc; //x/yCoordiantes one the grid in real pixels :S like point(1/1) would be grid (0/0)
xc = xr / 3; // calculation based on realCoordinates
yc = yr / 3;
try {
if ((game.pixelMap[xc + 1][yc + 1] == true && xSpeed > 0 && ySpeed > 0)
|| (game.pixelMap[xc - 1][yc - 1] == true && xSpeed < 0 && ySpeed < 0)
|| (game.pixelMap[xc + 1][yc - 1] == true && xSpeed > 0 && ySpeed < 0)
|| (game.pixelMap[xc - 1][yc + 1] == true && xSpeed < 0 && ySpeed > 0)) {
xSpeed = 0;
ySpeed = 0;
} else {
if ((game.pixelMap[xc + 1][yc] == true && xSpeed > 0)
|| (game.pixelMap[xc - 1][yc] == true && xSpeed < 0)) {
xSpeed = 0;
}
if ((game.pixelMap[xc][yc + 1] == true && ySpeed > 0)
|| (game.pixelMap[xc][yc - 1] == true && ySpeed < 0)) {
ySpeed = 0;
}
}
} catch (Exception e) {
Log.warn("CheckCollision error");
}
}
hope u understand me, if u have questions just ask ^^
~Markus