Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 22, 2008, 10:23:29 PM (15 years ago)
Author:
rgrieder
Message:
  • Forgot to account for collision shape position and orientation when attaching WEs
  • addChildShape —> attach in CompoundCollisionShape
  • Fixed an issue which allowed WE's to detach themselves from non-parents.
  • Fixed an issue that occurred when physics was not active before attaching
  • Added some forgotten const in WE (physics)
  • When attaching WE's the child doesn't get modified anymore. Alternative: notifyDetached() and notifyBeingAttached(this) —> the child does the work
Location:
code/branches/presentation/src/orxonox/objects/worldentities
Files:
3 edited

Legend:

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

    r2500 r2527  
    109109            float coeff = min(1.0f, 15.0f * dt);
    110110
    111             Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition();
     111            Vector3 offset = this->getWorldPosition() - this->cameraNode_->getWorldPosition();
    112112            this->cameraNode_->translate(coeff * offset);
    113113
  • code/branches/presentation/src/orxonox/objects/worldentities/WorldEntity.cc

    r2501 r2527  
    7171        this->physicalBody_ = 0;
    7272        this->bPhysicsActive_ = false;
     73        this->bPhysicsActiveSynchronised_ = false;
     74        this->bPhysicsActiveBeforeAttaching_ = false;
    7375        this->collisionShape_ = new CompoundCollisionShape(this);
    7476        // Note: CompoundCollisionShape is a Synchronisable, but must not be synchronised.
     
    251253    void WorldEntity::attach(WorldEntity* object)
    252254    {
    253         // check first whether attaching is even allowed
    254         if (object->hasPhysics())
    255         {
    256             if (!this->hasPhysics())
    257             {
    258                 COUT(2) << "Warning: Cannot attach a physical object to a non physical one." << std::endl;
    259                 return;
    260             }
    261             else if (object->isDynamic())
    262             {
    263                 COUT(2) << "Warning: Cannot attach a dynamic object to a WorldEntity." << std::endl;
    264                 return;
    265             }
    266             else if (object->isKinematic() && this->isDynamic())
    267             {
    268                 COUT(2) << "Warning: Cannot attach a kinematic object to a dynamic one." << std::endl;
    269                 return;
    270             }
    271             else if (object->isKinematic())
    272             {
    273                 COUT(2) << "Warning: Cannot attach a kinematic object to a static or kinematic one: Not yet implemented." << std::endl;
    274                 return;
    275             }
    276             else
    277             {
    278                 object->deactivatePhysics();
    279             }
    280         }
    281 
    282255        if (object == this)
    283256        {
     
    286259        }
    287260
    288         if (object->getParent())
    289             object->detachFromParent();
     261        if (!object->notifyBeingAttached(this))
     262            return;
    290263
    291264        this->attachNode(object->node_);
    292 
    293265        this->children_.insert(object);
    294         object->parent_ = this;
    295         object->parentID_ = this->getObjectID();
    296 
    297         // collision shapes
     266
    298267        this->attachCollisionShape(object->getCollisionShape());
    299268        // mass
     
    302271    }
    303272
     273    bool WorldEntity::notifyBeingAttached(WorldEntity* newParent)
     274    {
     275        // check first whether attaching is even allowed
     276        if (this->hasPhysics())
     277        {
     278            if (!newParent->hasPhysics())
     279            {
     280                COUT(2) << "Warning: Cannot attach a physical object to a non physical one." << std::endl;
     281                return false;
     282            }
     283            else if (this->isDynamic())
     284            {
     285                COUT(2) << "Warning: Cannot attach a dynamic object to a WorldEntity." << std::endl;
     286                return false;
     287            }
     288            else if (this->isKinematic() && newParent->isDynamic())
     289            {
     290                COUT(2) << "Warning: Cannot attach a kinematic object to a dynamic one." << std::endl;
     291                return false;
     292            }
     293            else if (this->isKinematic())
     294            {
     295                COUT(2) << "Warning: Cannot attach a kinematic object to a static or kinematic one: Not yet implemented." << std::endl;
     296                return false;
     297            }
     298        }
     299
     300        if (this->isPhysicsActive())
     301            this->bPhysicsActiveBeforeAttaching_ = true;
     302        this->deactivatePhysics();
     303
     304        if (this->parent_)
     305            this->detachFromParent();
     306
     307        this->parent_ = newParent;
     308        this->parentID_ = newParent->getObjectID();
     309
     310        // apply transform to collision shape
     311        this->collisionShape_->setPosition(this->getPosition());
     312        this->collisionShape_->setOrientation(this->getOrientation());
     313        // TODO: Scale
     314       
     315        return true;
     316    }
     317
    304318    void WorldEntity::detach(WorldEntity* object)
    305319    {
     320        if (this->children_.find(object) == this->children_.end())
     321        {
     322            CCOUT(2) << "Warning: Cannot detach an object that is not a child." << std::endl;
     323            return;
     324        }
     325
    306326        // collision shapes
    307327        this->detachCollisionShape(object->getCollisionShape());
     328
    308329        // mass
    309330        if (object->getMass() > 0.0f)
     
    315336        this->detachNode(object->node_);
    316337        this->children_.erase(object);
    317         object->parent_ = 0;
    318         object->parentID_ = OBJECTID_UNKNOWN;
    319 
    320         // Note: It is possible that the object has physics but was disabled when attaching
    321         object->activatePhysics();
     338
     339        object->notifyDetached();
     340    }
     341
     342    void WorldEntity::notifyDetached()
     343    {
     344        this->parent_ = 0;
     345        this->parentID_ = OBJECTID_UNKNOWN;
     346
     347        // reset orientation of the collisionShape (cannot be set within a WE usually)
     348        this->collisionShape_->setPosition(Vector3::ZERO);
     349        this->collisionShape_->setOrientation(Quaternion::IDENTITY);
     350        // TODO: Scale
     351
     352        if (this->bPhysicsActiveBeforeAttaching_)
     353        {
     354            this->activatePhysics();
     355            this->bPhysicsActiveBeforeAttaching_ = false;
     356        }
    322357    }
    323358
     
    351386    void WorldEntity::attachCollisionShape(CollisionShape* shape)
    352387    {
    353         this->collisionShape_->addChildShape(shape);
     388        this->collisionShape_->attach(shape);
    354389        // Note: this->collisionShape_ already notifies us of any changes.
    355390    }
     
    357392    void WorldEntity::detachCollisionShape(CollisionShape* shape)
    358393    {
    359         this->collisionShape_->removeChildShape(shape);
     394        this->collisionShape_->detach(shape);
    360395        // Note: this->collisionShape_ already notifies us of any changes.
    361396    }
     
    363398    CollisionShape* WorldEntity::getAttachedCollisionShape(unsigned int index) const
    364399    {
    365         return this->collisionShape_->getChildShape(index);
     400        return this->collisionShape_->getAttachedShape(index);
    366401    }
    367402
  • code/branches/presentation/src/orxonox/objects/worldentities/WorldEntity.h

    r2485 r2527  
    244244            CollisionShape* getAttachedCollisionShape(unsigned int index) const;
    245245
    246             inline CompoundCollisionShape* getCollisionShape()
     246            inline CompoundCollisionShape* getCollisionShape() const
    247247                { return this->collisionShape_; }
    248             inline btRigidBody* getPhysicalBody()
     248            inline btRigidBody* getPhysicalBody() const
    249249                { return this->physicalBody_; }
    250250
     
    259259            inline void disableCollisionCallback()
    260260                { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); }
    261             inline bool isCollisionCallbackActive()
     261            inline bool isCollisionCallbackActive() const
    262262                { return this->bCollisionCallbackActive_; }
    263263
     
    271271            void recalculateMassProps();
    272272            void resetPhysicsProps();
     273
     274            bool notifyBeingAttached(WorldEntity* newParent);
     275            void notifyDetached();
    273276
    274277            // network callbacks
     
    293296            bool                         bPhysicsActive_;
    294297            bool                         bPhysicsActiveSynchronised_;
     298            bool                         bPhysicsActiveBeforeAttaching_;
    295299            CompoundCollisionShape*      collisionShape_;
    296300            btScalar                     mass_;
Note: See TracChangeset for help on using the changeset viewer.