高效的碰撞检测AS3

高效的碰撞检测AS3

问题描述:

我有一个游戏,我做的一个问题。我基本上是在一个地图对象,我要检查每个人,如果他们与墙壁碰撞(然后做一些事情)。由于正与AS2,我想过做同样的方式:我画了一幅画,只有墙壁,所以只用矩形和一切在两者之间是透明的(不存在,那么地板为例)。在AS2我把图像在屏幕上,让我们把它叫做墙上,然后我做了的hitTest到墙上的每个对象。也就是说,例如,在对象实际上在图像上,由于该透明部分是它的一部分,但功能只对可见零件与壁测试,等等。因此,它的工作。

I have a problem with a game that I'm doing. I basically have objects that are in a map and I have to check for each of them if they collide with the walls (and then do something). Since was working with AS2, I thought about doing the same way: I drew a picture with only the walls, so with only rectangles and everything else in between is transparent (does not exist, then the floor for example). In AS2 I put the image to the screen, let's call it wall, and then I did a hitTest to wall with every object. That is for instance, the object was actually on the image, since that the transparent parts were part of it, but the function was testing only on the visible parts, and so with the walls. So it worked.

现在在AS3中没有的HitTest ,但 hitTestObject ,这是我用的,我做的,例如 wall.hitTestObject(对象)。的问题是,这种功能是,就好像它好好尝试一下看到胶片和在不接触壁的物体碰撞它们!

Now in AS3 there is no HitTest but hitTestObject, which I used, and I do for example wall.hitTestObject(object). The problem is that this function is as if it doens't see the transparencies, and the objects while not touching the walls collide with them!

我找到了 PixelPerfectCollisionDetection ,实际上解决了这个问题,但它是巨大而沉重因此,在我的情况下,有这么多的物体进行测试(至少60)在每帧,游戏速度变慢了很多!

I found the PixelPerfectCollisionDetection that actually solves the problem but it is huge and heavy so in my case, with so many objects to be tested (at least 60) at each frame, the game slows down a lot!

我需要的是像 hitTestObject 函数(我并不需要大量的准确性!)的拍摄图像的透明部分的照顾。

What I need is a function like hitTestObject (i don't need a lot of accuracy!) that take care of the transparent parts of an image.

我该怎么办?

正如评论,物理/游戏库将拥有内置的这个code为你工作,并应开箱。

As mentioned in the comments, physics/game libraries will have this code built-in for you and should work out of the box.

但是,如果你想建立它自己,甚至推出自己的优化,(这是很便宜的)第一步是使用检查边界冲突完全内置的 DisplayObject.getBounds 和 Rectangle.intersects (尽管你必须这样做以一致的坐标空间,也就是舞台):

But if you want to build it yourself, or even introduce your own optimizations, the first step (which is very inexpensive) is checking for bounds collision using entirely built-in functionality of DisplayObject.getBounds and Rectangle.intersects (though you must do so in a consistent coordinate space, i.e. the stage):

if (obj1.getBounds(stage).intersects(obj2.getBounds(stage)) {
  // Cheap bounds intersection is true, now do pixel-perfect detection...
}

这时如果边界检查是真实的,执行像素完美碰撞检测。

Then if the bounds check is true, perform the pixel-perfect collision detection.

看来, BitmapData.hitTest 是你最好的选择 - 看的迈克钱伯斯的博客文章。

It seems that BitmapData.hitTest is your best bet - see a blog post by Mike Chambers.

在此之前的方法,如果你有兴趣在纯技术的,还有格兰特斯金纳中的他的博客。这是一个相当聪明的算法,利用内置的位图例程(即,比较快),创建的BitmapData只有一样大的重叠区域(甚至缩放下来),并绘制了两个对象到BitmapData的专用通道,然后用 BitmapData.getColorBoundsRect()来确定是否有任何像素的触摸。我猜BitmapData.hitTest速度更快,但它会是有趣的比较。

Prior to this method, if you're interested in neat techniques, there was a method outlined by Grant Skinner in his blog. It's quite a clever algorithm using built-in bitmap routines (aka, fairly fast), creating a BitmapData only as large as the overlapping region (or even scaling that down), and drawing the two objects into specific channels of the bitmapdata, then using BitmapData.getColorBoundsRect() to determine if there are any pixels touch. I'm guessing BitmapData.hitTest is faster, but it'd be fun to compare.