且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于在AS3碰撞检测alghorithms

更新时间:2022-06-24 03:39:14

如果你只有说,十敌人和一百发子弹还是让我怀疑任何复杂的空间结构将大大有利于你这一切。检查对每一个敌人达一千检查,涨幅在性能上每一个子弹相比,保持一个更复杂的结构,在我看来是太小了。

If you only have say, ten enemies and a hundred bullets or so I doubt any complex spatial structure will benefit you all that much. Checking every bullet against every enemy amounts to a thousand checks, and the gains in performance compared to maintaining a more complex structure is in my opinion too small.

根据你的游戏速度有多快,你可以​​得到的,而不是每一帧逃脱检查的碰撞每隔一帧(或较少偶数)。这会给你在你的碰撞检测一个漂亮的100%的性能提升。

Depending on how fast your game is, you could get away with checking for collisions every other frame (or even less often) instead of every frame. That'll give you a nice 100% performance boost on your collision detection.

我建议简单地做这样的:

I recommend simply doing this:

for each(var enemy:Enemy in _enemies){
    for each(var bullet:Bullet in _bullets){
         if(bullet.x > enemy.x - enemy.width / 2 && 
            bullet.x < enemy.x + enemy.width / 2 &&
            bullet.y > enemy.y - enemy.height / 2 && 
            bullet.y < enemy.y + enemy.height / 2){
                trace("collision!");
         }
     }
}