使用 CGRectIntersectsRect 进行碰撞检测

发布于 2024-08-28 09:26:58 字数 543 浏览 5 评论 0原文

我有一个长矩形图像,它以不同的角度旋转。然而,矩形图像的框架不会随图像一起旋转,而是旋转导致框架变大以适应旋转的图像。因此,当我使用 CGRectIntersectsRect 时,碰撞检测完全关闭,因为与矩形图像碰撞的其他图像甚至在到达矩形图像的可见区域之前就会发生碰撞。 如果你不太明白我在说什么,请看一下 ascii 绘图:

普通矩形图像框,O ->像素, |, –​​ -> 帧

|----------|
|OOOOOOOOOO|
|----------|

旋转后的

|----------|
|O         |
| O        |
|  O       |
|   O      |
|    O     |
|     O    |
|      O   |
|       O  |
|        O |
|----------|

我已经阅读了一些碰撞文章,但所有这些文章都在谈论与正常直矩形的碰撞,而我真正想要的是与倾斜图像的碰撞,最好是像素碰撞检测。 TIA 提出的任何建议。

I've got a long rectangular image which is rotated at different kind of angles. However the frame of the rectangular image does not rotate along with the image and instead, the rotation causes the frame to to become larger to fit the rotated image. So when I used CGRectIntersectsRect, the collision detection is totally off because the other image colliding with the rectangular image will collide before it even reaches the visible area of the rect image.
In case you don't really know what I'm talking about, have a look at the ascii drawing:

normal rectangular image frame, O -> pixels, |, – -> frame

|----------|
|OOOOOOOOOO|
|----------|

after rotation

|----------|
|O         |
| O        |
|  O       |
|   O      |
|    O     |
|     O    |
|      O   |
|       O  |
|        O |
|----------|

I've read through some of the collision articles but all of them are talking about collision with a normal straight rectangle and what I really want is collision with a slanted image, preferably pixel collision detection. TIA for any suggestions made.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-09-04 09:26:58

如果您使用的是 Mac,则可以使用 -[NSImage hitTestRect:withImageDestinationRect:context:hints:flipped:] 来确定特定矩形是否与图像中的非透明像素相交。使用方法请参见标题注释。

If you're on the mac, you can use -[NSImage hitTestRect:withImageDestinationRect:context:hints:flipped:] to decide if a particular rectangle intersects non-transparent pixels in your image. See the header comment for usage.

别低头,皇冠会掉 2024-09-04 09:26:58

您不能使用 UIViewframe 属性对旋转对象进行碰撞,因为一旦开始,frame 就不再有效改变观点。您需要想出一些定制的解决方案。具体是什么取决于很多因素,尤其是性能。

您可以为图像创建几何碰撞包络,即可以随图像一起旋转的多边形。或者,您可以使用像素完美的碰撞检测与简单的边界检查相结合,以尽可能避免昂贵的像素检查。而且,如果您真的想变得更奇特,您可以求助于一些功能齐全的物理模拟器,例如 Box2D

如果您需要更好的建议,请向我们提供更多详细信息。您需要多久检查一次碰撞?一次?每秒40次?

You can’t use the frame property of UIView to do collisions on rotated objects, as the frame is no longer valid as soon as you start transforming the view. You need to come up with some custom solution. What exactly that will be depends on many things, especially performance.

You can create a geometric collision envelope for the image, a polygon you can rotate along with the image. Or you can use pixel-perfect collision detection combined with simple bounds-checking to avoid the expensive pixel-check when possible. And, if you really wanted to get fancy, you can resort to some full-featured physics simulator such as Box2D.

If you want better advice, give us more detail. How often do you need to check for the collisions? Once? 40 times per second?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文