Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 perfect collision detection  (Read 3782 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Duke


Projects: 3



« Posted 2012-12-18 22:28:30 »

I want to translate a C++ function I found on the net. Here is the C++ code:
// Full object-to-object pixel-level collision detector:
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
short int Sprite_Collide(sprite_ptr object1, sprite_ptr object2) {
 
    int left1, left2, over_left;
    int right1, right2, over_right;
    int top1, top2, over_top;
    int bottom1, bottom2, over_bottom;
    int over_width, over_height;
    int i, j;
    unsigned char *pixel1, *pixel2;
 
    left1 = object1->x;
    left2 = object2->x;
    right1 = object1->x + object1->width;
    right2 = object2->x + object2->width;
    top1 = object1->y;
    top2 = object2->y;
    bottom1 = object1->y + object1->height;
    bottom2 = object2->y + object2->height;
 
 
    // Trivial rejections:
    if (bottom1 < top2) return(0);
    if (top1 > bottom2) return(0);
   
    if (right1 < left2) return(0);
    if (left1 > right2) return(0);
 
 
    // Ok, compute the rectangle of overlap:
    if (bottom1 > bottom2) over_bottom = bottom2;
    else over_bottom = bottom1;
 
    if (top1 < top2) over_top = top2;
    else over_top = top1;
 
    if (right1 > right2) over_right = right2;
    else over_right = right1;
 
    if (left1 < left2) over_left = left2;
    else over_left = left1;
 
 
    // Now compute starting offsets into both objects' bitmaps:
    i = ((over_top - object1\1->y) * object1->width) + over_left;
    pixel1 = object1->frames[object1->curr_frame] + i;
 
    j = ((over_top - object2->y) * object2->width) + over_left;
    pixel2 = object2->frames[object2->curr_frame] + j;
 
   
    // Now start scanning the whole rectangle of overlap,
    // checking the corresponding pixel of each object's
    // bitmap to see if they're both non-zero:
 
    for (i=0; i < over_height; I++) {
        for (j=0; j < over_width; j++) {
            if (*pixel1 > 0) && (*pixel2 > 0) return(1);
            pixel1++;
            pixel2++;
        }
        pixel1 += (object1->width - over_width);
        pixel2 += (object2->width - over_width);
    }
 
 
    // Worst case!  We scanned through the whole darn rectangle of overlap
    // and couldn't find a single colliding pixel!
 
    return(0);
 
};


I understand most of the code. I get lost at line 45 and downwards.
Some help?
PS I am using BufferedImage, and the getRGB methods are extremely slow. I am thinking to store all the colors in an int array, that is initialized before the game starts.
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2012-12-19 00:48:18 »

Is it safe to assume the sprites won't be scaled in anyway? As in their drawn size matches the image size? Or do you want help with sprites possibly being scaled as well...

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2012-12-19 06:42:21 »

I must ask, why are you doing it this way? is this a learning experience? cause it would be a lot easier to just get some pre-existing code thats already in java.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline P0jahn

Senior Duke


Projects: 3



« Reply #3 - Posted 2012-12-19 20:47:00 »

Is it safe to assume the sprites won't be scaled in anyway? As in their drawn size matches the image size? Or do you want help with sprites possibly being scaled as well...
The spriates wont be scaled in any way after they are loaded for the first time.

I must ask, why are you doing it this way? is this a learning experience? cause it would be a lot easier to just get some pre-existing code thats already in java.
I cant find any pixel perfect code written in java, so I have to translate this C++ code.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #4 - Posted 2012-12-19 23:37:24 »

check out this thread:

http://www.gamedev.net/topic/329033-pixel-perfect-collision-detection/
Offline SHC
« Reply #5 - Posted 2012-12-25 12:27:21 »

See my implementation. https://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/util/GUtil.java#70

Offline P0jahn

Senior Duke


Projects: 3



« Reply #6 - Posted 2012-12-25 21:29:02 »

Your implementation is identical to the this one:
http://www.gamedev.net/topic/329033-pixel-perfect-collision-detection/#entry3131661
However, the one above is faster yours, I think.
Why dont you have return true rather than bool = true? It is just useless cycles.

May I ask how exact your method is? The one I just linked, is always two pixels "late".
Offline SHC
« Reply #7 - Posted 2012-12-27 08:19:29 »

@P0jahn I have implemented the method by based on that thread with some changes. I didn't use return true so it checks all the pixels and so works perfectly. I too noticed the late of the original one. But mine works well and takes 1 to 5 millis to check collision by bsing on the resolutions.

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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (87 views)
2014-10-11 22:30:10
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!