Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  Collision Maps  (Read 4497 times)
0 Members and 1 Guest are viewing this topic.
Offline hgouveia

Senior Newbie





« Posted 2011-01-29 22:59:24 »

 Hello, i need a help to figer out how to do a Collision detectition using collision maps,

  i want to do something like

1  
2  
3  
 if ( collisionMaps( collisionWorldMap, actor ) == COLOR_RED ) {
   //do whatever
}


i have this maps to test , but i can't get it
http://img201.imageshack.us/i/mascara2.png/ , the collision map
http://img88.imageshack.us/i/mascara1b.png/ // the original



Offline JL235

JGO Coder


Medals: 10



« Reply #1 - Posted 2011-01-30 01:13:46 »

If you load your collision map as a BufferedImage (using the ImageIO class) you can then call 'getRGB( x, y )' on it to return the colour value at that given location. This is in the form of 4 bytes packed into an int in the format of alpha, red, green and blue. To get these out you can bit-shift and then bit-and the value with 0xff; but if your not used to using bit-shifting then you can simply wrap the int inside a new Color object (which can take a single int in it's constructor). You can then use the Color object to get out the components.

I'd imagine getRGB is simply performing a direct array lookup after jumping through a couple of functions, which should be cheap. However incase it is slow you can also get out all pixels in a giant array using the other 'getRGB' method and then just throw away your image. That should be something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
    BufferedImage img = ImageIO.read( new File("my_collisions.png") );
    int[] pixels img.getRGB( 0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth() );
   
    // some code later...
   
    // find the pixel at the collision x,y
   int pixel = pixels[x + y*img.getWidth()];
    // get out the red
   int red = (pixel >> 16) & 0xff;
    if ( red > 200 ) {
        // handle collision
   } else {
        // no collision
   }

Note that the above is untested.

Once you have the components you can state if there is a collision if red is equal to 255, otherwise there isn't. Two tips; make sure you don't use jpegs (as they will alter the values stored) and don't check for exact values. For example rather then checking if red is 255, check if red is greater then 250 or 200. This makes the maps a bit easier to work with. Your already using a PNG and this is very good because it's a format that is excellent at compressing a large blocks of the same colour.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #2 - Posted 2011-01-30 01:58:37 »

You can also just read the image in and then throw all the data into a big boolean array. I've done that and it works fine, it's also just a bit faster.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hgouveia

Senior Newbie





« Reply #3 - Posted 2011-01-30 09:40:34 »

Thanks guys , it works like a charm! Cheesy
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-02-10 06:47:09 »

You can also just read the image in and then throw all the data into a big boolean array. I've done that and it works fine, it's also just a bit faster.
What do you mean by throwing all the data into a big boolean array?

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #5 - Posted 2011-02-10 18:57:49 »

You can also just read the image in and then throw all the data into a big boolean array. I've done that and it works fine, it's also just a bit faster.
What do you mean by throwing all the data into a big boolean array?
You don't need to hold onto the BufferedImage or the raster data if you just scan through in the first place, find what pixel is considered solid, and set those to true in a corresponding boolean array. This is what I've done when I didn't want to allow logic parts of the game to access the BufferedImage.

See my work:
OTC Software
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-02-12 01:19:15 »

You can also just read the image in and then throw all the data into a big boolean array. I've done that and it works fine, it's also just a bit faster.
What do you mean by throwing all the data into a big boolean array?
You don't need to hold onto the BufferedImage or the raster data if you just scan through in the first place, find what pixel is considered solid, and set those to true in a corresponding boolean array. This is what I've done when I didn't want to allow logic parts of the game to access the BufferedImage.
Oh I understand. But under what circumstances would you not want other parts of the game to have the actual pixel values?

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #7 - Posted 2011-04-23 17:53:48 »

You can also just read the image in and then throw all the data into a big boolean array. I've done that and it works fine, it's also just a bit faster.
What do you mean by throwing all the data into a big boolean array?
You don't need to hold onto the BufferedImage or the raster data if you just scan through in the first place, find what pixel is considered solid, and set those to true in a corresponding boolean array. This is what I've done when I didn't want to allow logic parts of the game to access the BufferedImage.
Oh I understand. But under what circumstances would you not want other parts of the game to have the actual pixel values?

The collisionmap should be in it's own bitmap or something shouldn't it? Load the pixels you need into a faster form (a nice boolean array for example), and free the CPU of the actual image.  Cheesy

Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #8 - Posted 2011-04-23 18:05:43 »

If you ever want to modify the collision map, having it as an Image is very useful - as it gives you access to the flexibility of the Java2D api with which to perform the modifications.

So IMO you should leave it as a BufferedImage.
Ofcourse from a design perspective you might want to limit precisely what operations the game code can perform on the collision map, but that can be achieved through simple encapsulation.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #9 - Posted 2011-04-25 19:24:38 »

If you ever want to modify the collision map, having it as an Image is very useful - as it gives you access to the flexibility of the Java2D api with which to perform the modifications.

So IMO you should leave it as a BufferedImage.
Ofcourse from a design perspective you might want to limit precisely what operations the game code can perform on the collision map, but that can be achieved through simple encapsulation.
Yeah I'd say it's really a matter of what you want to accomplish. I was basically passing the image data into OpenGL and then using the constructed bool arrays just for collision in my immutable level.

See my work:
OTC Software
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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (62 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!