对许多对象进行命中测试 Flash AS3
我正在进行一个模拟,其中涉及动态添加到舞台上的粒子(很多圆圈)。当这些圆圈碰撞(实际上是影片剪辑)时,它们需要反弹(动量切换)。我对物理没有任何问题,但不知道如何有效地检查碰撞。归结为能够 1) 检查粒子是否发生碰撞,以及 2) 获取哪个其他粒子(对象)参与了碰撞?
理想情况下,我希望粒子能够检查自己的碰撞并调整自己的动量,而不仅仅是在每帧上运行一些丰富的 Render() 函数。这怎么可能做到呢?
提前致谢。
I am making a simulation that involves particles dynamically added to the stage (lots of circles). When these circles collide (which are actually movieclips) they need to rebound (momentum switched). I have no problem with the physics but do not know how to efficiently check for collisions. It boils down to being able to 1) Check if a particle is colliding and 2) Getting which other particle (object) is involved in the collision?
I would ideally like particles to be able to check for their own collisions and adjust their own momentum, not just some meaty Render()
function running on every frame. How could this be done?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了有效地检测碰撞,您将需要使用称为二叉树的东西。本质上,您首先检查两个圆接触是否合理,而不是对照每个圆的每个像素检查每个圆的每个像素。这是通过反复将地图分成四等份来完成的。如果两个圆圈不在屏幕的同一象限,它们就不会接触。
然而,实施起来很困难,除非您有目的地给自己一个挑战,否则请使用库,这样您就不会重新发明轮子。您可以使用物理引擎 Box2D 或游戏引擎 Flixel。
For efficient collision detection, you will want to use something called a binary tree. Essentially, instead of checking every single pixel of every single circle against every single pixel of every single circle, you first check if it is even reasonable that two circles are touching. This is done by repeatedly dividing the map into quarters. If two circles aren't even in the same quadrant of the screen, they won't be touching.
Implementation, however, is difficult, and unless you're purposefully giving yourself a challenge, use a library so you don't reinvent the wheel. You can use Box2D, a physics engine, or Flixel, a game engine.
http://troygilbert.com/2009/08/pixel-perfect- Collision-Detection-revisited/
这是提供像素完美碰撞检测的 as3 类的链接。网站上提供的下载链接。希望这有帮助。
作者还链接到另一个解决方案:
http://coreyoneil.com/portfolio/index.php ?项目=5
http://troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/
This is a link to an as3 class providing Pixel-Perfect Collision Detection. Download link provided on the site. Hope this helps.
The author also links to another solution:
http://coreyoneil.com/portfolio/index.php?project=5
如果您关心速度和优化,我建议您选择 Box2D。有很多示例可供您入门,并且按照您的建议进行操作实际上非常简单。
If you care about speed and optimization, I would suggest you go with Box2D. There are many examples for you to get started and doing what you're suggesting on it is actually quite simple.
如果您不想使用 Box2D,您仍然可以使用递归函数进行二分搜索,并将搜索重复分成象限,直到您在数组元素中找到碰撞,或者您没有找到碰撞
if you don't want to use the Box2D, you can still use a recursive function to make a binary search and split the search into quadrants repeatedly until you either find a collision in the element of the array or you don't