Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 15, 2008, 4:33:50 PM (15 years ago)
Author:
rgrieder
Message:

Added callback for collisions. Every WorldEntity that collides against another will be called with the following function:
virtual bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint);
btManifoldPoint is from Bullet and tells you more about the contact point, like position.

Per default, the callback is disabled. Enable it with this→enableCollisionCallback(), for instance in your derived class constructor.
Note that if you activate the callback for e.g. SpaceShips, but not for MovableEntities, then collidesAgainst will only be called in the SpaceShip. This could be changed however, just ask me.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation/src/orxonox/objects/Scene.cc

    r2463 r2466  
    206206            this->physicalWorld_   = new btDiscreteDynamicsWorld(this->dispatcher_, this->broadphase_, this->solver_, this->collisionConfig_);
    207207            this->physicalWorld_->setGravity(omni_cast<btVector3>(this->gravity_));
     208
     209            // also set the collision callback variable.
     210            // Note: This is a global variable which we assign a static function.
     211            // TODO: Check whether this (or anything about Bullet) works with multiple physics engine instances.
     212            gContactAddedCallback = &Scene::collisionCallback;
    208213        }
    209214        else if (!wantPhysics && hasPhysics())
     
    337342            this->physicalWorld_->removeRigidBody(object->getPhysicalBody());
    338343    }
     344
     345    /*static*/ bool Scene::collisionCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,
     346                                             int index0, const btCollisionObject* colObj1, int partId1, int index1)
     347    {
     348        // get the WorldEntity pointers
     349        WorldEntity* object0 = (WorldEntity*)colObj0->getUserPointer();
     350        assert(dynamic_cast<WorldEntity*>(object0));
     351        WorldEntity* object1 = (WorldEntity*)colObj1->getUserPointer();
     352        assert(dynamic_cast<WorldEntity*>(object1));
     353
     354        // false means that bullet will assume we didn't modify the contact
     355        bool modified = false;
     356        if (object0->isCollisionCallbackActive())
     357        {
     358            modified |= object0->collidesAgainst(object1, cp);
     359            if (object1->isCollisionCallbackActive())
     360                modified |= object1->collidesAgainst(object0, cp);
     361        }
     362        else
     363            modified |= object1->collidesAgainst(object0, cp);
     364
     365        return modified;
     366    }
    339367}
Note: See TracChangeset for help on using the changeset viewer.