Java-通过数组存储的冲突检测冲突
我目前正在为我的2D游戏制作碰撞。我做了一些研究,并推断我应该使用将alpha值像素存储到实体图像的“遮罩”中的方法,对其他实体也是如此。然后,我取两个实体的x&y坐标,以及高度和宽度,制作一个矩形对象,并使用方法Java-通过数组存储的冲突检测冲突,java,arrays,collision,Java,Arrays,Collision,我目前正在为我的2D游戏制作碰撞。我做了一些研究,并推断我应该使用将alpha值像素存储到实体图像的“遮罩”中的方法,对其他实体也是如此。然后,我取两个实体的x&y坐标,以及高度和宽度,制作一个矩形对象,并使用方法Rectangle.intersects(Rectangle r),检查它们是否发生碰撞,以使其更有效,而不是通过2个循环 如果它们相交,我将创建一个具有以下维度的新数组: int maxLengthY = Math.max(thisEntity.getMask().length,
Rectangle.intersects(Rectangle r)
,检查它们是否发生碰撞,以使其更有效,而不是通过2个循环
如果它们相交,我将创建一个具有以下维度的新数组:
int maxLengthY = Math.max(thisEntity.getMask().length, e.getMask().length);
int maxLengthX = Math.max(thisEntity.getMask()[0].length, thisEntity.getMask()[0].length);
int minX = Math.min(thisEntity.getX(), e.getX());
int minY = Math.min(thisEntity.getY(), e.getY());
int[][] map = new int[maxLengthX + minX][maxLengthY + minY];
然后将另外两个遮罩与相应的y&x“边界”相加,如下所示:
for(int curX = 0; curX < maxLengthX + minX; curX++) { //only loop through the co-ords that area affected
for(int curY = 0; curY < maxLengthY + minY; curY++) {
int this_x = thisEntity.getX();
int this_width = thisEntity.getImage().getWidth();
int this_y = thisEntity.getY();
int this_height = thisEntity.getImage().getHeight();
int[][] this_mask = thisEntity.getMask();
if(curX < (this_x + this_width) && curX < this_x) {//check that the co-ords used are relevant for thisEntity's mask
if(curY < (this_y + this_height) && curY < this_y) {
map[curX][curY] = this_mask[Math.abs(curX - this_x)][Math.abs(curY - this_y)]; // store data from mask to map
}
}
int other_x = e.getX();
int other_width = e.getImage().getWidth();
int other_y = e.getY();
int other_height = e.getImage().getHeight();
int[][] other_mask = e.getMask();
if(curX < (other_x + other_width) && curX > other_x) { //check that the co-ords used are relevant for e's mask
if(curY < (other_y + other_height) && curY > other_y) {
if(map[curX][curY] == 1) { //check if this segment is already written by thisEntity
map[curX][curY] = 2; //if yes, set to 2 instead of e's value to show collision
} else {
map[curX][curY] = other_mask[curX][curY]; // the minus to nullify minX and minY "requirements"
}
}
}
}
}
另外,下面是方法getMask()
:
public int[]getMask(){
返回掩码;
}
...
私有void createMask(BuffereImage图像){
最终int[]像素=((DataBufferInt)image.getRaster().getDataBuffer()).getData();
final int width=image.getWidth();
最终整数高度=image.getHeight();
最终布尔值hasAlphaChannel=image.getAlphaRaster()!=null;
int[][]结果=新的int[高度][宽度];
中频(信道){
用于(整数像素=0,行=0,列=0;像素
然而。。。这段代码并没有按预期的那样工作,在某些情况下,根本就不起作用因为在向地图添加单个遮罩时,我会得到IndexOutOfBounds,即使它应该工作,所以可能只是我忽略了一些东西
因此,总而言之,我需要代码方面的帮助:
- 怎么了
- 我怎样才能修好它
- 有没有更有效的方法来进行这种类型的碰撞
- 你推荐其他类型的碰撞吗?如果是,它们是什么
创建实体时,是否从大小完全相同的图像创建它们的遮罩?因为否则,实体的遮罩和图像贴图将使用不同的坐标系(当贴图[0][0]位于“世界”的一角时,实体的遮罩[0][0]可能位于其一角),并且您将在该行比较相同的索引:
map[curX][curY] = other_mask[curX][curY];
(在上面的代码中,您实际上是在使用Math.abs(a-b)
将它们放在同一坐标系中)
至于更有效的检测碰撞的方法,你可以在维基百科上查看更多信息。我用它来创建世界地图:`int[][]map=new int[minX][minY]`因此它需要图像x和y的值,并且需要最小的值来为两个实体的遮罩创建一个与其在级别中的区域相关的矩形。然而,我同意你的观点,即所有代码都有缺陷,你知道他们怎么说的,如果不值得修复船上的漏洞,那就换一个新的吧。可能只是我太懒了:P。谢谢你的链接,我可能会造一艘新船:-)。
public int[][] getMask() {
return mask;
}
...
private void createMask(BufferedImage image) {
final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[][] result = new int[height][width];
if (hasAlphaChannel) {
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += 4) {
int alpha = pixels[pixel];
if(alpha != 0) {
result[row][col] = 1;
} else {
result[row][col] = 0;
}
if (col == width) {
col = 0;
row++;
}
}
}
mask = result;
}
map[curX][curY] = other_mask[curX][curY];