Finally I got a solution, but I still have to test it properly. I also thinking about adding some of the suggestions above into my programm.
Nevertheless, here’s my solution. It is not called for each collison detection only for some cases.
Any comment would be appreciated.
`
import java.awt.;
import java.awt.image.;
public class CollisionDetector {
static Rectangle s1;
static Rectangle s2;
static Rectangle inter;
static int width;
static int height;
public static BufferedImage buf1;
public static BufferedImage buf2;
public static CollisionDetector single = new CollisionDetector();
public CollisionDetector getDetector(){
return single;
}
public static boolean checkPixelCollision(Image a, int ax, int ay, Image b, int bx, int by){
//Rectangles
//System.out.println("detector");
//System.out.println("A = " + ax + "/" + ay + "/" + a.getWidth(null) + "/" + a.getHeight(null));
//System.out.println("B = " + bx + "/" + by + "/" + b.getWidth(null) + "/" + b.getHeight(null));
//initialize Rectangles
Rectangle rec1 = new Rectangle();
Rectangle rec2 = new Rectangle();
inter = new Rectangle();
//set Bounds and get intersection
rec1.setRect(ax,ay,a.getWidth(null),a.getHeight(null));
rec2.setRect(bx,by,b.getWidth(null),b.getHeight(null));
Rectangle.intersect(rec1,rec2,inter);
//Intersection
//System.out.println("I = " + inter.x + "/" + inter.y + "/" + inter.getWidth() + "/" + inter.getHeight());
//widht and height must be at least 1 Pixel
width = (int) inter.width;
height = (int) inter.height;
if(width<1){
return false;
}
if(height<1){
return false;
}
//compute sub-images for image 1
s1 = getSubRec(rec1,inter);
//widht and height must be at least 1 Pixel
width = (int) s1.width;
height = (int) s1.height;
if(width<1){
return false;
}
if(height<1){
return false;
}
//compute sub-images for image 2
s2 = getSubRec(rec2,inter);
//widht and height must be at least 1 Pixel
width = (int) s2.width;
height = (int) s2.height;
if(width<1){
return false;
}
if(height<1){
return false;
}
//SubImages
//System.out.println("S1 = " + s1.x + "/" + s1.y + "/" + s1.width + "/" + s1.height);
//System.out.println("S2 = " + s2.x + "/" + s2.y + "/" + s2.width + "/" + s2.height);
buf1 = (BufferedImage)a;
buf2 = (BufferedImage)b;
buf1 = buf1.getSubimage((int)s1.x,(int)s1.y,(int)s1.width,(int)s1.height);
buf2 = buf2.getSubimage((int)s2.x,(int)s2.y,(int)s2.width,(int)s2.height);
//System.out.println(buf1.getWidth() + "/" + buf2.getWidth());
//System.out.println(buf1.getHeight() + "/" + buf2.getHeight());
for(int i=0;i<buf1.getWidth();i++){
for(int n=0;n<buf1.getHeight();n++){
//System.out.println(i + "/" + n);
int rgb1 = buf1.getRGB(i,n);
int rgb2 = buf2.getRGB(i,n);
if(isOpaque(rgb1)&&isOpaque(rgb2)){
return true;
}
}
}
return false;
}
private static boolean isOpaque(int pixel) {
int alpha = (pixel >> 24) & 0xff;
//int red = (pixel >> 16) & 0xff;
//int green = (pixel >> 8) & 0xff;
//int blue = (pixel ) & 0xff;
//transparent if alpha = 0
if(alpha==0){
return false;
}
return true;
}
//computing Rectangles for sub-images
private static Rectangle getSubRec(Rectangle rec1, Rectangle inter) {
//Rechtecke erzeugen
Rectangle sub = new Rectangle();
Rectangle source = rec1;
Rectangle part = inter;
int absoluteX = 0;
int absoluteY = 0;
//get X - compared to the Rectangle
if(source.x>part.x){
sub.x = 0;
absoluteX = source.x;
}else{
sub.x = part.x - source.x;
absoluteX = part.x;
}
if(source.y>part.y){
sub.y = 0;
absoluteY = source.y;
}else{
sub.y = part.y - source.y;
absoluteY = part.y;
}
//same handling with y
if((source.x+source.width)<(part.x+part.width)){
sub.width = (source.x+source.width)-absoluteX;
}else{
sub.width = (part.x+part.width)-absoluteX;
}
if((source.y+source.height)<(part.y+part.height)){
sub.height = (source.y+source.height)-absoluteY;
}else{
sub.height = (part.y+part.height)-absoluteY;
}
return sub;
}
}
`