Box2D Help

Please don't post Bullet support questions here, use the above forums instead.
iwinties
Posts: 3
Joined: Mon Jul 02, 2007 8:09 pm

Box2D Help

Post by iwinties »

I've just picked up Box2D, and I'm having a little trouble figuring out how I should do this... I'm trying to make a game similar to Klooni Game's "The Amazing Flying Brothers". I don't plan for the game to be much except get me closer to Box2D.

So far, I've modified one of the demo's in B2D, the simple pendulum. I've duplicated it, and now I have two pendulums. Spaced apart evenly. The first problem I have, is that if I try to access Body->position at all, my game crashes. I'm not sure why that's happening... The second problem I'm having is logical, I guess. I can't determine how I should switch a weight with the player, depending on which trapeze he's on.

The joints both have a "weight" on the end, because as far as I know, the joint has to be connected to two bodies. Can you detach one body and have the joint still working, can you detach a body from a joint at ALL?

I think that's all the questions I have right now, but I hope someone can help. I'd really like to become familiar with Box2D.

BTW, I've pasted the code to initialize the joints and bodies. Kinda long, hope this is ok. :)

Code: Select all

void Demo1()
{
    int length = 175;

    // FLOOR
    Body* b1 = new Body();
    b1->Set( hgeVector( SCREEN_WIDTH - 1, 30 ), FLT_MAX );
    b1->position = hgeVector( SCREEN_WIDTH / 2, SCREEN_HEIGHT - 15 );
    b1->friction = 0.2f;
    world.Add( b1 );
    bodies.push_back( b1 );

    // FIRST BASE
    Body* b2 = new Body();
    b2->Set( hgeVector( 50, 10 ), FLT_MAX );
    b2->position = hgeVector( 200, SCREEN_HEIGHT - 35 );
    //b2->friction = 0.2f;
    world.Add( b2 );
    bodies.push_back( b2 );

    // SECOND BASE
    Body* b3 = new Body();
    b3->Set( hgeVector( 50, 10 ), FLT_MAX );
    b3->position = hgeVector( SCREEN_WIDTH - 200, SCREEN_HEIGHT - 35 );
    //b3->friction = 0.2f;
    world.Add( b3 );
    bodies.push_back( b3 );

    // FIRST WEIGHT
    Body* b4 = new Body();
    b4->Set( hgeVector( 20, 20 ), 20 );
    b4->friction = 0.5f;
    b4->position = hgeVector( 200 + length, 200 );
    b4->rotation = 0.0f;
    world.Add( b4 );
    bodies.push_back( b4 );

    // SECOND WEIGHT
    Body* b5 = new Body();
    b5->Set( hgeVector( 20, 20 ), 20 );
    b5->friction = 0.5f;
    b5->position = hgeVector( SCREEN_WIDTH - 200 + -length, 200 );
    b5->rotation = 0.0f;
    world.Add( b5 );
    bodies.push_back( b5 );

    // PLAYER
    Body* player = new Body();
    player->Set( hgeVector( 30, 75 ), 100 );
    player->position = hgeVector( SCREEN_WIDTH / 2, 0 );
    player->friction = 0.2f;
    player->rotation = 0.0f;
    world.Add( player );
    bodies.push_back( player );

    // FIRST TRAPEZE
    Joint *j1 = new Joint();
    j1->Set( b2, b4, hgeVector( 200, 200 ) );
    world.Add( j1 );
    joints.push_back( j1 );

    // SECOND TRAPEZE
    Joint *j2 = new Joint();
    j2->Set( b3, b5, hgeVector( SCREEN_WIDTH - 200, 200 ) );
    world.Add( j2 );
    joints.push_back( j2 );
}
Erin Catto
Posts: 316
Joined: Fri Jul 01, 2005 5:29 am
Location: Irvine

Post by Erin Catto »

The body->position crash sounds like you may have a null or orphaned pointer.

If you want to disable a joint, you should just write a function like:

void World::Remove(Joint* joint)
{
joints.erase(joint); // or whatever std::vector supports
}

To release the memory, you would still need to have something like:

delete joint;
iwinties
Posts: 3
Joined: Mon Jul 02, 2007 8:09 pm

Post by iwinties »

Oh, ok. I thought the joints were more tied in with the engine, I wasn't sure if that would cause some catastrophic meltdown. :-P

And the body->position bug was an bad typo. :(

Thanks for the help!
iwinties
Posts: 3
Joined: Mon Jul 02, 2007 8:09 pm

Post by iwinties »

Ok, so it works better... Mostly. Hehe, I have a few more problems.

But I'm having a problem deciding how I should replace a weight with a player, or if I should ever do that. I have the player which is a bigger box with a bigger mass, on the left joint. The right joint has a weight on it.

Because if the use left clicks and release, I want the player body to be released from the joint and continue free falling based on it's velocity. But while the player is in the air, I want the joint he leaped from to keep moving.

Any ideas?