Newbie Question

Post Reply
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia
Contact:

Newbie Question

Post by hiker »

Hi,

I am very new to bullet, and I want to use bullet for a kart game (see http://supertuxkart.berlios.de). So first an apology for asking stupid questions.

I've started by using a simple boxe for each kart, and without any collision detection (e.g. track) am just letting them fall down (a very simple first test :) ). I expected that only the z (gravity) coordinate would change. But I see that the other coordinates are changing as well, e.g. :
(format body 0: <x> <y> <z> dt <delta t>)

Code: Select all

body 0: 1.500000 -1.500000 -0.018948 dt 0.000000
body 0: 1.500000 -1.500000 -0.018948 dt 0.014394
body 0: 1.554279 -1.449197 -0.238790 dt 0.006532
body 0: 1.554279 -1.449197 -0.238790 dt 0.006405
body 0: 1.608559 -1.398393 -0.461356 dt 0.006611
body 0: 1.608559 -1.398393 -0.461356 dt 0.006349
1.5/-1.5/-0.018948 are the start coordinates, and the falling appears to be working as expected (quadratic behaviour). But the x/y coordinates change linearly as well.

If I set the kart start coordinates to 0/0/0, everything works as expected - x/y remain (nearly) 0 (rounding errors only), and the box is falling. So I guess I must be forgetting to reset the velocity(?) somewhere, or some functions are not doing what I expect them to do.

Secondly, if I use a 2nd kart (starting at -1.500000 -3.000000 -0.018948 ), this kart will first raise before starting to fall, and it is showing the same linear behaviour in the x/y coordinates but in the opposite direction compared with the kart above, e.g.:

Code: Select all

body 1: -1.500000 -3.000000 -0.018948 dt 0.000000
body 1: -1.500000 -3.000000 -0.018948 dt 0.014394
body 1: -1.554279 -3.050803 0.195446 dt 0.006532
body 1: -1.554279 -3.050803 0.195446 dt 0.006405
body 1: -1.608559 -3.101606 0.407115 dt 0.006611
Any hints appreciated - I am sure it is something stupid :) (I will of course be using a more complex kart model, but I first wanted to get my head around how to get started - btw, thanks to Erwin for updateing the vehicle demo!!!):

Cheers,
Joerg

This is all the code using the bullet physics, nothing else (bullet related) happens in between.

World creation:

Code: Select all

    m_dynamics_world = new btDiscreteDynamicsWorld();
    m_dynamics_world->setGravity(btVector3(0.0f, 0.0f, -gravity));
Adding a kart:

Code: Select all

    //x_* and y* are the minimum/maximum coordinates of the kart model
    // resuling in a size of approx 3.5 x 2.2 (x/y direction)
    btCollisionShape *box = new btBoxShape(btVector3(x_max-x_min, 
                                                     y_max-y_min, 
                                                     1.0f)       );
    sgCoord *pos=kart->getCoord();
    btTransform trans;
    trans.setIdentity();
    trans.setOrigin(btVector3(pos->xyz[0], pos->xyz[1], pos->xyz[2]));
    // it works as expected if I use:    
    // trans.setOrigin(btVector3(0.0f,0.0f,0.0f));

    float mass=kart->getMass();
    btVector3 inertia;
    box->calculateLocalInertia(mass, inertia);
    btRigidBody* kart_body = new btRigidBody(mass, trans, box, inertia);
    kart_body->m_worldTransform.setRotation(btQuaternion(0,0,0,1));
    kart_body->setLinearVelocity(btVector3(0.0f,0.0f,0.0f));
    kart_body->setAngularVelocity(btVector3(0,0,0));
    m_dynamics_world->addRigidBody(kart_body);
and during a time step:

Code: Select all

    int max_sim_sub_steps = 1;
    int numSimSteps = m_dynamics_world->stepSimulation(dt,max_sim_sub_steps);

    int numObjects = m_dynamics_world->getNumCollisionObjects();
    for(int i=0; i<numObjects; i++) 
    {
        btCollisionObject *obj = m_dynamics_world->getCollisionObjectArray()[i];
        btRigidBody* body = btRigidBody::upcast(obj);
        if(!body) continue;
        const btVector3 &pos=body->getCenterOfMassPosition();
        printf("body %d: %f %f %f dt %f\n",i, pos.x(), pos.y(), pos.z(),dt);
        
    }

User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Post by Erwin Coumans »

Hi,

Can you try this with only 1 single object added to Bullet? What is the distance between the objects? It looks like the objects are overlapping initially. One good help would be to hookup the debugDrawer. The btBoxShape takes half extents, not the full length of the sides.

Also, it is recommended to use the btMotionState to synchronize the graphics/physics transforms. The other constructor of btRigidBody will probably disappear soon/made obsolete.

Thanks,
Erwin
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia
Contact:

Post by hiker »

Erwin Coumans wrote: The btBoxShape takes half extents, not the full length of the sides.
Ah - I checked for overlapping before posting, but wasn't aware that half extents are used. A quick test shows that this solves the problem :) Thanks!
Erwin Coumans wrote: Also, it is recommended to use the btMotionState to synchronize the graphics/physics transforms. The other constructor of btRigidBody will probably disappear soon/made obsolete.
btDefault MotionState was not declared in bullet-2.-18a, but it works now with the latest snapshot - great, thanks.

BTW, there is a 'no newline at end of file' warning for btDefaultMotionState.h :)

Thanks a lot!
Joerg
User avatar
SteveBaker
Posts: 127
Joined: Sun Aug 13, 2006 4:41 pm
Location: Cedar Hill, Texas
Contact:

Post by SteveBaker »

BTW, there is a 'no newline at end of file' warning for btDefaultMotionState.h :)
Erwin has a real problem 'habit' with creating these! I fix 'em up as fast as I can - but it's an uphill battle!

:-)
User avatar
SteveBaker
Posts: 127
Joined: Sun Aug 13, 2006 4:41 pm
Location: Cedar Hill, Texas
Contact:

Post by SteveBaker »

Woah! I just noticed!

TuxKart with Bullet physics! Now that's a weird experience...I wrote the original TuxKart (upon which SuperTuxKart is built) - it's very strange to see one's own ancient offspring popping up in places like this!

TuxKart with physics....WOW!
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia
Contact:

Post by hiker »

SteveBaker wrote: TuxKart with Bullet physics! Now that's a weird experience...
*grin* You shouldn't be surprised, you were the one who recommended bullet to me :)
SteveBaker wrote: I wrote the original TuxKart (upon which SuperTuxKart is built) - it's very strange to see one's own ancient offspring popping up in places like this!
TuxKart with physics....WOW!
Well, I hope that we get (what's now) supertuxkart to a really nice state - using a ready-to-go physics engine (hopefully) simplifies the task.

Cheers,
Joerg
Post Reply