How could you detect collision with a colour in Java?

I am making a top-down game. I’ve got a Player and I’ve drawn a level made up of black and white sections as a .png file. How could I implement a collision detection system where the player can move on the white section but not on the black parts.

Sorry if this is a simple question but I’m pretty new to game programming.

Thanks for any help!

Since reading the pixels of the image would be pretty slow… I would write an algorithm that would determine all rectangles in that image and then do collision with those rectangles. It shouldn’t be hard to write a greedy algorithm that iterates through all pixels… and when a black one is found it starts to grow that area in all directions until it hits a white pixel. Do this until all black pixels are taken.

Wow! Thanks for replying so quickly! Seeing as I’m very new to Java Game programming, could you give me a few pointers on how to make such an algorithm. Please don’t do it all for me, that would be bad for both of us but just a few indications on how I could write such an algorithm would be awesome!

EDIT: I found these questions of StackOverflow. The answers suggest a lot of ways in which I could implement this. Are these systems the kind of algorithm you were talking about?



The most common form of collision detection that happens to be one of the quickest methods is called when the play attempts to move in a certain direction. It checks to see if there is a pixel or rectangle at said position along the side of a shape, this is easy with simple polygons as they can have easy to use bounding boxes.

Unless you want to dynamically generate colored boundaries ingame, I wouldn’t do collision by color value. Instead, just define bounding boxes like lcass said (java.awt.Rectangle is good for this) and do logical collision checks that restrict where the player can move in relation to where the player is contacting the bounding box.