像素完美碰撞检测的Andr​​oid [英] Pixel-Perfect Collision Detection Android

查看:122
本文介绍了像素完美碰撞检测的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我工作的一个游戏在Android上。我需要实现像素完美碰撞检测。我已经有周围设置各图像的边界框,每个边界框被转换以匹配图像的当前旋转。这一切的伟大工程。我还从存储在数组中的每个位图具有的像素数据。有人可以帮我找出最有效的方法去检测,如果像素的重叠?在此先感谢您的帮助!

Ok so I am working on a game on Android. I need to implement pixel perfect collision detection. I already have the bounding boxes set up around each of the images, each bounding box is transformed to match the current rotation of the image. That all works great. I also have the pixel data from each bitmap stored in an array. Can someone help me figure out the most efficient way to go about detecting if the pixels overlap? Thanks in advance for any help!

推荐答案

基本的想法是创建一个位掩码,你在每个像素指示该对象实际上是有或没有的每个对象。然后,你的两个对象比较位掩码的每一个像素。

The basic idea is to create a bitmask for each object where you indicate in each pixel if the object is actually there or not. Then you compare each pixel of the bitmasks for the two objects.

您可以尽量减少需要检查通过计算矩形区域,其中两个边界框重叠像素的数目。在此区域内的像素是什么,你需要检查一下。

You could minimize the number of pixels you need to check by calculating the rectangular area in which the two bounding boxes overlap. The pixels within this area are what you need to check.

遍历所有这些像素的,并且如果像素被填充在两个对象进行检查。如果其中的任何一样,那么你有一个碰撞。

Iterate through all of those pixels, and check if the pixel is filled in both objects. If any of them are, then you have a collision.

如果您的矩形与x / y轴一致,以找到重叠,查找重叠的左,右,顶部和底部。它会是这个样子(我可能搞砸了边缘的情况下,没有试过):

If your rectangles are aligned with the x/y axis, to find the overlap, find the left, right, top and bottom of the overlap. It would look something like this (I could have screwed up the edge cases, haven't tried this):

int left = max(obj1.left, obj2.left)
int right = min(obj1.right, obj2.right)
int top = min(obj1.top, obj2.top)
int bottom = max(obj1.bottom, obj2.bottom)

for (int x = left; x < right; x++) {
  for (int y = top; y < bottom; y++) {
     if (obj1.isFilled(x,y) && obj2.isFilled(x,y)) {
        return true;
     }
  }
}

这篇关于像素完美碰撞检测的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆