Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2466


Ignore:
Timestamp:
Dec 15, 2008, 4:33:50 PM (16 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.

Location:
code/branches/presentation/src/orxonox
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation/src/orxonox/OrxonoxPrereqs.h

    r2459 r2466  
    225225class btCollisionObject;
    226226class btGhostObject;
     227class btManifoldPoint;
    227228
    228229class btCollisionShape;
  • 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}
  • code/branches/presentation/src/orxonox/objects/Scene.h

    r2459 r2466  
    120120                { this->setGravity(this->gravity_); }
    121121
     122            // collision callback from bullet
     123            static bool collisionCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,
     124                                          int index0, const btCollisionObject* colObj1, int partId1, int index1);
     125
    122126            // Bullet objects
    123127            btDiscreteDynamicsWorld*             physicalWorld_;
  • code/branches/presentation/src/orxonox/objects/weaponSystem/projectiles/Projectile.cc

    r2100 r2466  
    5656        this->setStatic(false);
    5757        this->translate(Vector3(55, 0, 0), Ogre::Node::TS_LOCAL);
     58
     59        // Get notification about collisions
     60        this->enableCollisionCallback();
    5861
    5962        if (this->owner_)
  • code/branches/presentation/src/orxonox/objects/worldentities/WorldEntity.cc

    r2465 r2466  
    8585        this->angularDamping_ = 0;
    8686        this->friction_       = 0.5;
     87        this->bCollisionCallbackActive_ = false;
    8788
    8889        this->registerVariables();
     
    144145        registerVariable(this->getScale3D(),    variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged));
    145146
    146         registerVariable((int&)this->collisionTypeSynchronised_,
    147                                                 variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
     147        // Physics stuff
    148148        registerVariable(this->mass_,           variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged));
    149149        registerVariable(this->restitution_,    variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::restitutionChanged));
     
    152152        registerVariable(this->angularDamping_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::angularDampingChanged));
    153153        registerVariable(this->friction_,       variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::frictionChanged));
     154        registerVariable(this->bCollisionCallbackActive_,
     155                                                variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionCallbackActivityChanged));
     156        registerVariable((int&)this->collisionTypeSynchronised_,
     157                                                variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
    154158        registerVariable(this->bPhysicsActiveSynchronised_,
    155159                                                variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::physicsActivityChanged));
    156160
     161        // Attach to parent if necessary
    157162        registerVariable(this->parentID_,       variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::parentChanged));
    158163    }
     
    192197        else
    193198            this->deactivatePhysics();
     199    }
     200
     201    void WorldEntity::collisionCallbackActivityChanged()
     202    {
     203        if (this->hasPhysics())
     204        {
     205            if (bCollisionCallbackActive_)
     206                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() |
     207                    btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
     208            else
     209                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() &
     210                    !btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
     211        }
    194212    }
    195213
     
    525543        recalculateMassProps();
    526544        resetPhysicsProps();
     545        collisionCallbackActivityChanged();
    527546        activatePhysics();
    528547    }
  • code/branches/presentation/src/orxonox/objects/worldentities/WorldEntity.h

    r2459 r2466  
    240240            void notifyChildMassChanged();
    241241
     242            virtual inline bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
     243                { return false; } /* With false, Bullet assumes no modification to the collision objects. */
     244
     245            inline void enableCollisionCallback()
     246                { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); }
     247            inline void disableCollisionCallback()
     248                { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); }
     249            inline bool isCollisionCallbackActive()
     250                { return this->bCollisionCallbackActive_; }
     251
    242252        protected:
    243253            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
     
    253263            void collisionTypeChanged();
    254264            void physicsActivityChanged();
     265            void collisionCallbackActivityChanged();
    255266            inline void massChanged()
    256267                { this->setMass(this->mass_); }
     
    279290            btScalar                     friction_;
    280291            btScalar                     childrenMass_;
     292            bool                         bCollisionCallbackActive_;
    281293    };
    282294
  • code/branches/presentation/src/orxonox/objects/worldentities/pawns/SpaceShip.cc

    r2463 r2466  
    6161        // Be aware of this call: The collision type legality check will not reach derived classes!
    6262        this->setCollisionType(WorldEntity::Dynamic);
     63        // Get notification about collisions
     64        this->enableCollisionCallback();
    6365
    6466        this->setConfigValues();
Note: See TracChangeset for help on using the changeset viewer.