开发者

Find what bodies are colliding in Box2D using C++

开发者 https://www.devze.com 2023-02-08 22:06 出处:网络
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 topwal

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();
        b2Bo开发者_Go百科dy* 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消