Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find what bodies are colliding in Box2D using C++

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.

like image 981
recharge330 Avatar asked Sep 06 '25 11:09

recharge330


4 Answers

When new custom menu is added, its entry is added into several tables.

  1. table: _term_taxonomy with a column taxonomy and value nav_menu.
  2. from 1) term_id is also in table:_terms with the title of menu which is shown in admin panel
  3. table:_term_relationship will contain term_texonomy_id from 1) and connection to all object_id which will be linked to all _post table having sub_menu and below level menu.
  4. table:_postmeta will contain all the details related to menu and submenu for each object_id from _posts table.
like image 199
Hiren Rathod Avatar answered Sep 09 '25 04:09

Hiren Rathod


Navigation menu items are stored in the wp_posts table with post_type = 'nav_menu_item'.

like image 24
Todd Moses Avatar answered Sep 09 '25 04:09

Todd Moses


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.

like image 24
Skurmedel Avatar answered Sep 09 '25 05:09

Skurmedel


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.

like image 34
Cynic Wild Avatar answered Sep 09 '25 04:09

Cynic Wild