Cocos2d 碰撞检测模型 - 最适合我的情况?

发布于 2024-12-10 00:18:02 字数 948 浏览 0 评论 0原文

我正在构建一个 cocos2d iPhone 游戏,其中有大量子弹和移动的敌人,并且我正在检测它们之间的碰撞。为了碰撞目的,每个精灵都可以用一个圆圈来表示。我正在考虑以下选项:

1)简单球体检测

我定期进行这样的检测:

-(BOOL) isCollidingSphere:(CCSpriteExt*) obj1 WithSphere:(CCSprite *) obj2
{
    float minDistance = obj1.radius + obj2.radius;
    float dx = obj2.position.x - obj1.position.x;
    float dy = obj2.position.y - obj1.position.y;
    if (! (dx > minDistance || dy > minDistance) )
    {
        float actualDistance = sqrt( dx * dx + dy * dy );
        return (actualDistance <= minDistance);
    }
    return NO;
}

2)Box2d仅用于碰撞检测

我为所有精灵创建一个Box2d主体如本教程所示: http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

我的问题很简单:如果我优先考虑的是优化,哪种方法更快?

谢谢!

I'm building a cocos2d iPhone game with lots of bullets and moving enemies and I'm detecting collisions between them. Every sprite can be represented by a circle for collision purposes. I'm considering the following options:

1) Simple Sphere Detection

I detect like this at regular intervals:

-(BOOL) isCollidingSphere:(CCSpriteExt*) obj1 WithSphere:(CCSprite *) obj2
{
    float minDistance = obj1.radius + obj2.radius;
    float dx = obj2.position.x - obj1.position.x;
    float dy = obj2.position.y - obj1.position.y;
    if (! (dx > minDistance || dy > minDistance) )
    {
        float actualDistance = sqrt( dx * dx + dy * dy );
        return (actualDistance <= minDistance);
    }
    return NO;
}

2) Box2d for collision detection only

I create a Box2d body for all sprites as shown in this tutorial: http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

My question is simple: If my priority is optimisation, which approach is faster?

Thanks!

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

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

发布评论

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

评论(1

北方的巷 2024-12-17 00:18:02

如果您需要的只是基于距离/半径的碰撞检查,则不需要物理引擎。

不过,您应该摆脱 sqrt 。首先,您使用的是适用于双精度数的平方根函数。对于浮动版本,请使用sqrtf

要完全消除平方根,请确保您的对象存储其半径平方(radiusSquared = radius * radius)。这样你就不必再求平方根了:

-(BOOL) isCollidingSphere:(CCSpriteExt*) obj1 WithSphere:(CCSprite *) obj2
{
    float r1 = obj1.radius;
    float r2 = obj2.radius;
    float minDistanceSquared = r1 * r1 + r2 * r2 + 2 * r1 * r2;
    float dx = obj2.position.x - obj1.position.x;
    float dy = obj2.position.y - obj1.position.y;
    float actualDistanceSquared = dx * dx + dy * dy;
    return (actualDistanceSquared <= minDistanceSquared);
}

If all you need is distance/radius based collision checks, you don't need a physics engine.

You should get rid of the sqrt though. First of all, you're using the square root function that works on doubles. For the float version use sqrtf.

To get rid entirely of the square root, make sure your objects store their radius squared (radiusSquared = radius * radius). That way you don't have to take the square root anymore:

-(BOOL) isCollidingSphere:(CCSpriteExt*) obj1 WithSphere:(CCSprite *) obj2
{
    float r1 = obj1.radius;
    float r2 = obj2.radius;
    float minDistanceSquared = r1 * r1 + r2 * r2 + 2 * r1 * r2;
    float dx = obj2.position.x - obj1.position.x;
    float dy = obj2.position.y - obj1.position.y;
    float actualDistanceSquared = dx * dx + dy * dy;
    return (actualDistanceSquared <= minDistanceSquared);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文