Box2D 中的高级碰撞检测 - 碰撞点、力等

发布于 2024-12-08 19:37:54 字数 745 浏览 0 评论 0原文

我正在使用 Cocos2d 和 Box2d 为 iOS 编写简单的游戏。我已经开始进行碰撞检测,但不是以我想要的方式进行。我扩展了 b2ContactListener,并以这种方式检测对象碰撞:

void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body *aBody = contact->GetFixtureA()->GetBody();
    b2Body *bBody = contact->GetFixtureB()->GetBody();

    // collision between aBody and bBody occurred

}

我不知道如何获取碰撞点,以及如果可能的话,碰撞冲击力。有几个教程如何获取碰撞点,但没有一个对我有用。我发现了一些使用 b2CollisionPoint 的示例,但看起来这在当前版本的 Box2d 中不起作用。其他示例实现了 b2CollisionPoint 的“Result”方法,但它也不起作用 - 看起来它适用于 Box2d 的先前版本或其他平台(Java 或 Flash)的版本。

为了更具体地说明我的问题,这是我想要实现的目标的示例: 我需要检测游戏对象之间的碰撞类型。我已经能够检测玩家固定装置是否与地面固定装置碰撞,但我需要知道玩家是否只是“站立”在地面上或从底部与地面碰撞(想象一下平台游戏和击中平台的玩家)跳跃后)或另一侧(玩家跳入垂直墙)。如果我能够找到碰撞点,我会更容易做到这一点。这在许多其他情况下也很有用。

I'm writing simple game for iOS using Cocos2d with Box2d. I've already collision detection working, but not in the way I want to. I've extended b2ContactListener, and detecting objects collisions this way:

void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body *aBody = contact->GetFixtureA()->GetBody();
    b2Body *bBody = contact->GetFixtureB()->GetBody();

    // collision between aBody and bBody occurred

}

I can't find out how to get the point of collision, and if possible, the collision impact force. There are several tutorials how to get collision points, but none of them is working for me. I've found some examples that used b2CollisionPoint, but It looks like this is not working in the current version of Box2d. Other examples implements "Result" method of b2CollisionPoint, but it doesn't work neither - looks like it's applies to the previous version of Box2d or versions for other platform (Java or Flash).

To be more specific about my problem, here is an example of what I want to achieve:
I need to detect kind of collision between game objects. I'm already able to detect if player fixture is colliding with ground fixture, but I need to know if the player is just "standing" on the ground or colliding with it from the bottom (imagine platform game and a player that hits a platform after jump) or other side (player jumps into the vertical wall). I'll be much easier for me to do this if I would be able to get the point of collision. This would be useful in many other cases too.

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

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

发布评论

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

评论(1

落花浅忆 2024-12-15 19:37:54

这对我有用:
http://www.cocos2d-iphone.org/forum/topic/6024

void ContactListener::PostSolve(b2Contact* contact, 
                                  const b2ContactImpulse* impulse) {

    // find the strength of the impulse..
    int32 count = contact->GetManifold()->pointCount;
    float32 maxImpulse = 0.0f;
    for (int32 i = 0; i < count; ++i) {
        maxImpulse = b2Max(maxImpulse, impulse->normalImpulses[i]);
    }

    NSLog(@"maxImpulse: %f", maxImpulse);
}

this worked for me:
http://www.cocos2d-iphone.org/forum/topic/6024

void ContactListener::PostSolve(b2Contact* contact, 
                                  const b2ContactImpulse* impulse) {

    // find the strength of the impulse..
    int32 count = contact->GetManifold()->pointCount;
    float32 maxImpulse = 0.0f;
    for (int32 i = 0; i < count; ++i) {
        maxImpulse = b2Max(maxImpulse, impulse->normalImpulses[i]);
    }

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