Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pixel Collision Detection (not what u think at first)  (Read 1611 times)
0 Members and 1 Guest are viewing this topic.
Offline Noobxgockel

Junior Newbie





« Posted 2012-11-24 19:38:12 »

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 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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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
Offline deepthought
« Reply #1 - Posted 2012-11-24 19:44:58 »

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

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Noobxgockel

Junior Newbie





« Reply #2 - Posted 2012-11-24 20:01:49 »

Sure ill try, here an image:

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...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deepthought
« Reply #3 - Posted 2012-11-24 20:14:36 »

If i understand your process correctly i have an idea...

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.


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.


jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline pjt33
« Reply #4 - Posted 2012-11-24 20:15:57 »

QuadTree?
Offline Noobxgockel

Junior Newbie





« Reply #5 - Posted 2012-11-24 20:24:15 »

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:
Offline EgonOlsen
« Reply #6 - Posted 2012-11-24 20:40:35 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (19 views)
2014-11-22 12:13:56

xFryIx (68 views)
2014-11-13 12:34:49

digdugdiggy (47 views)
2014-11-12 21:11:50

digdugdiggy (41 views)
2014-11-12 21:10:15

digdugdiggy (35 views)
2014-11-12 21:09:33

kovacsa (59 views)
2014-11-07 19:57:14

TehJavaDev (62 views)
2014-11-03 22:04:50

BurntPizza (60 views)
2014-11-03 18:54:52

moogie (75 views)
2014-11-03 06:22:04

CopyableCougar4 (76 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!