Java 2d碰撞检测-尝试获取两个精灵的所有不透明像素

Java 2d碰撞检测-尝试获取两个精灵的所有不透明像素,java,sprite,collision,detection,Java,Sprite,Collision,Detection,我正在构建一个2d游戏,我正在尝试实现像素级/完美碰撞检测 我的问题是,我试图通过使用缓冲图像类getRGB()方法获取精灵的所有不透明像素,但是我只能在缓冲图像上使用此方法 我希望你能为我指明正确的方向,告诉我我要做什么。以下是我正在学习的游戏课程的方法: 此方法应获取我的精灵中的所有不透明像素 public HashSet<String> getMask(Sprite character){ HashSet <String> mask = new HashSet&l

我正在构建一个2d游戏,我正在尝试实现像素级/完美碰撞检测

我的问题是,我试图通过使用缓冲图像类getRGB()方法获取精灵的所有不透明像素,但是我只能在缓冲图像上使用此方法

我希望你能为我指明正确的方向,告诉我我要做什么。以下是我正在学习的游戏课程的方法:

此方法应获取我的精灵中的所有不透明像素

public HashSet<String> getMask(Sprite character){

HashSet <String> mask = new HashSet<String>();
    int pixel;
    int alpha;

    for(int i = 0; i < character.getWidth(); i++){
        for(int j = 0; j < character.getHeight(); i++){   

            pixel = character.getRGB(i,j);
            alpah = (pixel >> 24) & 0xff;    

            if(alpha != 0){
                mask.add((character.getX + i) + "," + (character.getY - j));
            }
        }
    }
    return mask;
}
public HashSet getMask(精灵字符){
HashSet mask=新HashSet();
整数像素;
int-alpha;
对于(int i=0;i>24)&0xff;
如果(alpha!=0){
add((character.getX+i)+“,”+(character.getY-j));
}
}
}
返回掩码;
}
检查碰撞的方法

public boolean checkCollision(Sprite a, Sprite b){      
    // This method detects to see if the images overlap at all. If they do,     collision is possible
    int ax1 = a.getX();
    int ay1 = a.getY();
    int ax2 = ax1 + a.getWidth();
    int ay2 = ay1 + a.getHeight();
    int bx1 = b.getX();
    int by1 = b.getY();
    int bx2 = bx1 + b.getWidth();
    int by2 = by1 + b.getHeight();

    if(by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1){
          return false; // Collision is impossible.
    }
    else {// Collision is possible.
        // get the masks for both images
        HashSet<String> maskPlayer1 = getMask(shark);
        HashSet<String> maskPlayer2 = getMask(torpedo);

        maskPlayer1.retainAll(maskPlayer2);  // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1

        if(maskPlayer1.size() > 0){  // if so, than there exists at least one pixel that is the same in both images, thus
            System.out.println("Collision" + count);//  collision has occurred.
            count++;
            return true;

        }
    }
    return false;   
}
公共布尔校验冲突(精灵a,精灵b){
//此方法检测图像是否重叠。如果重叠,则可能发生碰撞
int ax1=a.getX();
int ay1=a.getY();
int ax2=ax1+a.getWidth();
int ay2=ay1+a.getHeight();
int bx1=b.getX();
int by1=b.getY();
int bx2=bx1+b.getWidth();
int by2=by1+b.getHeight();
if(by20){//如果是,则两个图像中至少存在一个相同的像素,因此
System.out.println(“碰撞”+计数);//发生了碰撞。
计数++;
返回true;
}
}
返回false;
}
在上面的getMask()方法中,您可以看到我说的是:character.getRGB(),但是因为character是Sprite类型,所以我得到了一个错误,因为我只能对缓冲图像使用getRGB()

因此,据我所知,getRGB()很乐意获取游戏中缓冲图像移动的当前像素,但不愿意获取精灵的当前像素。我可能误解了这种方法的工作原理

所以我想知道是否有任何方法可以避免这个错误,或者如果没有,你能给我指出正确的方向吗


谢谢大家

Sprite是一个扩展矩形的类或具有此类属性的类

您可以向其添加BuffereImage成员:无论何时从角色获取RGB,都可以从该BuffereImage获取RGB

int getRGB(int i, int j) {
  return myBufferedImage.getRGB(i, j);
}
你在雪碧的什么地方

class Sprite {

  BufferedImage myBufferedImage;


  public int getRGB(int i, int j) {

    ......... as shown above

  }

  ....


}

Sprite是一个扩展矩形的类,或者具有这样的属性

您可以向其添加BuffereImage成员:无论何时从角色获取RGB,都可以从该BuffereImage获取RGB

int getRGB(int i, int j) {
  return myBufferedImage.getRGB(i, j);
}
你在雪碧的什么地方

class Sprite {

  BufferedImage myBufferedImage;


  public int getRGB(int i, int j) {

    ......... as shown above

  }

  ....


}

我不知道你是什么意思?您的意思是将MyBuffereImage设置为全局变量吗?如果我要发布我的整个游戏课程,你能帮我理解吗?当然,给我5分钟时间。如果我能给你发一封电子邮件,告诉你所涉及的课程,可以吗?因为我的动画课程也在使用,所有3个课程加在一起都非常大。我觉得在这里发布可能太大了我不知道你的意思是什么?您的意思是将MyBuffereImage设置为全局变量吗?如果我要发布我的整个游戏课程,你能帮我理解吗?当然,给我5分钟时间。如果我能给你发一封电子邮件,告诉你所涉及的课程,可以吗?因为我的动画课程也在使用,所有3个课程加在一起都非常大。我觉得在这里发布可能太大了