[SOLVED] Collision callback and sphereshape

Post Reply
mnunesvsc
Posts: 4
Joined: Thu Mar 15, 2012 8:02 pm
Contact:

[SOLVED] Collision callback and sphereshape

Post by mnunesvsc » Wed May 15, 2019 2:36 pm

I am using bullet for physiscs and collision detection for a game, and on my callback code, i can capture all colisions, except sphere vs sphere, is something i am doing wrong

the collision is ocurring because the objects are deflected but the bodies are not coming to the callback

here follow some code i use on creation of the collision objects

the shot sphere

Code: Select all

    colShape = new btSphereShape(2); // 2
    
    btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
    btVector3 localInertia(0, 0, 0);
    if (isDynamic) colShape->calculateLocalInertia(mass, localInertia);

    //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
    btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
    btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, colShape, localInertia);
    btRigidBody* body = new btRigidBody(rbInfo);
   dynamicsWorld->addRigidBody(body, COL_PRFR, COL_GRND | COL_BUILD | COL_DEBRIS | COL_ENSP );
the other sphere

Code: Select all

    colShape = new btSphereShape( 2 * 1. );
    btScalar mass(16.f);
    startTransform.setOrigin(pos);
    btRigidBody* body = createRigidBody(dynamicsWorld, mass, startTransform, colShape, COL_ENSP, COL_GRND | COL_BUILD | COL_DEBRIS | COL_SHIP | COL_PRFR | COL_ENSP );

Can anyone can help ?

Peace for all
Last edited by mnunesvsc on Wed Jun 19, 2019 2:08 pm, edited 2 times in total.

User avatar
xissburg
Posts: 44
Joined: Sat May 19, 2007 9:28 pm

Re: Collision callback and sphereshape

Post by xissburg » Tue May 28, 2019 7:51 pm

Where's your contact callback code? Are you using gContactAddedCallback? If so, you have to set a collision flag like this

Code: Select all

newBody->setCollisionFlags(newBody->getCollisionFlags()|btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
You should look into btManifoldResult::addContactPoint to see the logic and debug it.

mnunesvsc
Posts: 4
Joined: Thu Mar 15, 2012 8:02 pm
Contact:

Re: Collision callback and sphereshape

Post by mnunesvsc » Mon Jun 03, 2019 2:17 pm

I use;

gContactProcessedCallback

like this

Code: Select all

    // retrieve objects colided.
	btCollisionObject* o1 = static_cast<btCollisionObject*>(body0);
	btCollisionObject* o2 = static_cast<btCollisionObject*>(body1);

	int o1idx = o1->getUserIndex();
	int o2idx = o2->getUserIndex();

    // detect the object hited
	switch(o1idx)
	{
        // Se o tiro da nave colidiu...
        case OBJ_PLYFIR: // && (o2->getUserIndex() == 19) )
        {
            // Add to the array of objects to be deleted
            // acolObjects.insert(o1);
            /// add to the map....
            colObjectsMap[o1->getUserIndex2()] = o1;

            printf("1 -  %s 2# %d 1# %d\n", "Col IDXs =", o2idx, o1idx);

            break;
        }

        // Se o tiro da nave colidiu...
        case OBJ_ENMSHP: // && (o2->getUserIndex() == 19) )

	...
and have a type for every object as;

Code: Select all

btRigidBody* body = createRigidBody(dynamicsWorld, mass, startTransform, colShape, COL_ENSP, COL_GRND | COL_BUILD | COL_DEBRIS | COL_SHIP | COL_PRFR | COL_ENSP );

User avatar
xissburg
Posts: 44
Joined: Sat May 19, 2007 9:28 pm

Re: Collision callback and sphereshape

Post by xissburg » Mon Jun 03, 2019 4:07 pm

Is the callback invoked at least once or never? Does it happen in single collision and also in resting contact? You have to look at the point where the callback is called by Bullet which is in btPersistentManifold.cpp btPersistentManifold::refreshContactPoints. It only gets called if the contact is persisted so I don't think it will be called for short lived contacts in general (that is a single 'bounce' type of contact).

I believe you should be using gContactStartedCallback instead.

mnunesvsc
Posts: 4
Joined: Thu Mar 15, 2012 8:02 pm
Contact:

Re: Collision callback and sphereshape

Post by mnunesvsc » Wed Jun 19, 2019 1:52 pm

THANK YOU very much.

that was exactly what i was in need, just the first touch of collision worked like a charm, many, many thanks for the help

and to help someone else who can be on the same problem.

I was retrieving objects colided by the body pointer, but i can extract the same information from manifold, and that was what i had done, cost me 2 minuts to change the procedures

Code: Select all

    // retrieve objects colided.
	const btCollisionObject* o1 = static_cast<const btCollisionObject*>(manifold->getBody0());
	const btCollisionObject* o2 = static_cast<const btCollisionObject*>(manifold->getBody1());

Post Reply