I'm making a simple game engine using Java and have found a major issue.
To create accurate collision detection I decided to convert a transparent image into a java.awt.geom.Area object. However the method I'm doing this might be wrong or unoptimized.
To test it out, I tried loading an almost 200x200 (exactly 195x196) pixels image. I ran the program 5 times and this is the time it took
- 2900ms = 2s
- 2848ms = 2s
- 2999ms = 2s
- 2887ms = 2s
- 2871ms = 2s
Average time: 2,901ms
This isn't very good especially considering the size of the image.
Here is the code I am using:
public Entity(BufferedImage image) {
this.image = image;
entityArea = new Area();
heightmap = new int[image.getWidth()];
boolean heightFound = false;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
if((image.getRGB(x, y) & 0xFF000000) != 0) {
entityArea.add(new Area(new Rectangle(x, y, 1, 1)));
if(!heightFound) {
heightmap[x] = y;
heightFound = true;
}
}
}
}
}
heightmap is used to refer the top pixels for an entity
Add(new Area...)calls fromO(w * h)toO(w)\$\endgroup\$