简体   繁体   English

如何快速确定用户是否触摸了许多 CGRect 之一

[英]How to determine quickly if the user touched one of many CGRects

I have an iPhone game, where there might be 30 (or more) CGRects and I need a quick way of determining if the user touched one.我有一个 iPhone 游戏,其中可能有 30 个(或更多)CGRect,我需要一种快速的方法来确定用户是否触摸了一个。 I have been previously been considering using the follow setup to detect if the user touched a CGRect in Cocos2d inside of touches began.我之前一直在考虑使用下面的设置来检测用户是否在 Cocos2d 里面触摸了一个 CGRect 。 So I have a vector (I am using Obj-C++) of CGRects:所以我有一个 CGRects 的向量(我使用的是 Obj-C++):

for (int i = 0; i < (int) vec_of_cgrects; i++) {
    if (CGRectContainsPoint(vec_of_cgrects[i], location) {
        //Do what I need to do if user touches one of the rectangles
    }
}

But this isn't that efficient and I am wondering if this the best way to detect if the user touched a specific rect of a vector of rectangles or if there is a better way.但这不是那么有效,我想知道这是否是检测用户是否触摸了矩形向量的特定矩形的最佳方法,或者是否有更好的方法。

Do you want something to happen for each rectangle that the user touches, or one event should the user touch any old rectangle?您是否希望用户触摸的每个矩形都发生一些事情,或者用户触摸任何旧矩形时应该发生一个事件?

One improvement you can make in both situations is to sort your CGRects by their x-coordinate (or y-coordinate if that suits your circumstances more), so you can break as soon as the CGRect's x-coordinate is greater than the touch's x-coordinate.您可以在这两种情况下进行的一项改进是按 CGRect 的 x 坐标(或 y 坐标,如果更适合您的情况)对它们进行排序,这样您就可以在 CGRect 的 x 坐标大于触摸的 x- 时立即中断协调。

If the latter, you can also check beforehand to see if any CGRect is fully contained by other CGRects and if so, remove them from the array.如果是后者,您还可以事先检查是否有任何 CGRect 完全包含在其他 CGRect 中,如果是,则将它们从数组中删除。

You could consider using a Spatial Hash:您可以考虑使用空间 Hash:

http://www.gamedev.net/index.php?app=core&module=search&do=search&fromMainBar=1 http://www.gamedev.net/index.php?app=core&module=search&do=search&fromMainBar=1

Here's a post on the Cocos2d forums about it:这是 Cocos2d 论坛上关于它的帖子:

http://www.cocos2d-iphone.org/forum/topic/11323#post-64132 http://www.cocos2d-iphone.org/forum/topic/11323#post-64132

Chipmunk and Box2d use it (physics libraries) I'm pretty sure for their collision detection... Chipmunk 和 Box2d 使用它(物理库)我很确定它们的碰撞检测......

Alternative inefficient approach: Store an array of width*height lists.替代低效方法:存储宽度*高度列表的数组。 Populate each list with the list of rectangles covering that pixel (or the "top" rectangle if you do some sort of overlap-handling).用覆盖该像素的矩形列表(或“顶部”矩形,如果您进行某种重叠处理)填充每个列表。 O(1) lookup! O(1) 查找!

"Hacker's" approach: Divide the screen into squares of size h*h where h ~= (width+height)/sqrt(n). “黑客”的方法:将屏幕分成大小为 h*h 的正方形,其中 h ~= (width+height)/sqrt(n)。 Populate each square as above.如上所述填充每个正方形。 O(sqrt(n))-ish lookup. O(sqrt(n))-ish 查找。

Neither is quite as good as a tree-based approach, but the tree might be painful to generate.两者都没有基于树的方法那么好,但是生成树可能会很痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM