Pixel Collision Detection (not what u think at first)

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 :stuck_out_tongue:

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

could you please explain how the gameplay works in more detail?

Sure ill try, here an image:

http://i.imagebanana.com/img/xavmwrhp/Unbenannt.png

Dont know if u can see clearly…
But you controll that pixels(red thingys) with your mouse, they follow it…
well thats it atm ^^ later on there would be diffrent colered player who can “capture” my pixels, vice versa, with a algorithm like game of life…
But that shouldnt be too hard.

Dont look at the big rectangle, doesnt do anything.
and the errors in the backfground, well i have problems with overlapping pixels too…

If i understand your process correctly i have an idea…

instead of generating the boolean array each cycle, change your array only when a pixel moves. this will reduce the amount of assignments each cycle.

QuadTree?

Well thanks deepthought, did that before with the addition of generating it each cycle completely new.
But sadly no feelable performace gain, but thanks anyways ^^

QuadTree looks intresting and might be what im looking for, thanks, will look into it c:

Try to get rid of your exceptions first. Generating exceptions is expensive.