使用 C++ 查找 Box2D 中发生碰撞的物体

发布于 2024-10-16 02:07:30 字数 679 浏览 3 评论 0原文

我有一个用于检测碰撞的基本类,但我不知道如何查看哪些物体正在碰撞以触发适当的事件。假设我有一个乒乓球游戏,其中有 ballBody 和 topwallBody。我如何判断这些是否发生碰撞。这是我用来给你一个想法的课程。

class MyListener : public b2ContactListener
{
    void BeginContact(b2Contact* contact)
    {
        b2Fixture* fixtureA = contact->GetFixtureA();
        b2Fixture* fixtureB = contact->GetFixtureB();
        b2Body* body1 = fixtureA->GetBody();
        b2Body* body2 = fixtureB->GetBody();
        cout << "started";
    }
    void EndContact(b2Contact* contact)
    {
        cout << "ended\n";
    }
};
MyListener listener;
world.SetContactListener(&listener);

看起来我可以在指针中获取主体,但我不知道如何将它们与其他主体进行比较。

I have a basic class for detecting collisions but I can't figure out how to see what bodies are colliding to trigger the appropriate event. Lets say I have a pong game and in it a ballBody and topwallBody. How would I figure out if these are colliding or not. Here is the class I'm using just to give you an idea.

class MyListener : public b2ContactListener
{
    void BeginContact(b2Contact* contact)
    {
        b2Fixture* fixtureA = contact->GetFixtureA();
        b2Fixture* fixtureB = contact->GetFixtureB();
        b2Body* body1 = fixtureA->GetBody();
        b2Body* body2 = fixtureB->GetBody();
        cout << "started";
    }
    void EndContact(b2Contact* contact)
    {
        cout << "ended\n";
    }
};
MyListener listener;
world.SetContactListener(&listener);

It looks like I can get the bodies in the pointers but I have no idea how to compare them to other bodies.

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

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

发布评论

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

评论(2

自控 2024-10-23 02:07:30

创建主体时,将用户数据设置为有意义的内容,但要保持一致:) 一个好的提示是始终在其中包含相同种类和类型的数据、实体 ID 或对参与者的引用。

直接来自文档:

b2BodyDef bodyDef;

bodyDef.userData = &myActor;

因此,如果您走这条路,您将从 b2Body 获取 actor 并通知它碰撞,或者执行其他操作。

同样来自 docs

b2Fixture* fixtureA = myContact->GetFixtureA();

b2Body* bodyA = fixtureA->GetBody();

MyActor* actorA = (MyActor*)bodyA->GetUserData();

在上面的代码中,您将拥有参与者/实体并且可以执行以下操作无论你想要什么...actorA.explode()

保持一致可能会让你免于疯狂。如果一个人把各种数据都塞在那里,那么就很难知道什么数据在哪个身体里。另外,您无法真正以任何通用方式处理联系人。

When you create the bodies, set the userdata to something meaningful, but be consistent :) A good tip is to always have the same kind and type of data in there, an entity id or reference to an actor.

Straight from the documentation:

b2BodyDef bodyDef;

bodyDef.userData = &myActor;

So if you went this road you would get the actor from the b2Body and inform it of the collision, or do something else.

Also from the docs:

b2Fixture* fixtureA = myContact->GetFixtureA();

b2Body* bodyA = fixtureA->GetBody();

MyActor* actorA = (MyActor*)bodyA->GetUserData();

In the code above you would have the actor/entity and could do whatever you would like... actorA.explode().

Being consistent will likely save you from insanity. If one sticks all kinds of data in there it'll become really hard knowing what data is in what body. Plus you can't really handle the contacts in any generic way.

西瓜 2024-10-23 02:07:30

Skurmedel 给出的答案在这方面对我帮助很大。我想我应该添加一些我正在做的事情来解决这个问题的信息。

我和OP一样,想检查什么击中了什么。我的心在游戏屏幕的墙壁内跳来跳去,想知道它们是否撞击了其他心或墙壁。

我使用此代码来查看联系人

        world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {

            Gdx.app.log("GameScreen", "Contact Made! Fixture A: " + contact.getFixtureA().getBody().getUserData().toString());
            Gdx.app.log("GameScreen", "Contact Made! Fixture B: " + contact.getFixtureB().getBody().getUserData().toString());
        }

,在我的 heart 对象中,我覆盖了 toString 方法,暂时仅返回“Hear”。我将主体的 userData 设置为整个精灵对象,因此我可以灵活地处理对象本身的主体。

由于没有地板、墙壁和天花板的实际类引用,我只需将 userData 设置为“Floor”等。

GameScreen: Contact Made! Fixture A: Ground
GameScreen: Contact Made! Fixture B: Heart

使用此方法并在稍后对其进行增强,我将能够根据对象的身份更改对象的反应方式接触。

The answer Skurmedel gave helped me immensely on this. I thought I would add a little bit of information from what I was doing to solve this.

I, like the OP, wanted to check what was hitting what. I have hearts bouncing around inside the walls of the game screen and wanted to know if they are hitting other hearts, or the walls.

I used this code to view the contact

        world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {

            Gdx.app.log("GameScreen", "Contact Made! Fixture A: " + contact.getFixtureA().getBody().getUserData().toString());
            Gdx.app.log("GameScreen", "Contact Made! Fixture B: " + contact.getFixtureB().getBody().getUserData().toString());
        }

And within my heart object I overrode the toString method to simply return 'Hear' for now. I am setting the userData for the body as the whole sprite object, so I have flexibility with the body in the object itself.

Not having my actual class references for the floor, walls, and ceiling I simply set the userData as 'Floor' etc.

GameScreen: Contact Made! Fixture A: Ground
GameScreen: Contact Made! Fixture B: Heart

Using this method, and beefing it up later, I will be able to change how the objects react based on who they are contacting.

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