Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 2, 2008, 4:59:36 PM (16 years ago)
Author:
rgrieder
Message:
  • More dealings with not yet implemented scaling of physical objects.
  • Clearer handling of masses specified by setMass() (also taking into account mass of child WE)
  • Now calculating the inertia tensor as well according to mass and collisionShape
  • Bugfix when calling Bullet solver
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.cc

    r2304 r2306  
    7575        this->collisionShape_ = 0;
    7676        this->mass_ = 0;
    77         updateCollisionType();
     77        this->childMass_ = 0;
     78        this->collisionType_ = None;
    7879
    7980        this->registerVariables();
     
    151152            {
    152153                // static to static/kinematic/dynamic --> merge shapes
     154                this->childMass_ += object->getMass();
    153155                this->attachCollisionShape(object->getCollisionShape());
    154156                // Remove the btRigidBody from the child object.
     
    195197    }
    196198
    197     void WorldEntity::mergeCollisionShape(CollisionShape* shape)
    198     {
    199         if (!this->collisionShape_)
    200             this->collisionShape_ = new CompoundCollisionShape(this);
    201         assert(this->collisionShape_->isCompoundShape());
    202 
    203         // merge with transform
    204         CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_);
    205         assert(compoundShape);
    206         compoundShape->addChildShape(shape);
    207     }
    208 
    209199    void WorldEntity::attachCollisionShape(CollisionShape* shape)
    210200    {
     
    213203        if (!this->collisionShape_ && shape->hasNoTransform())
    214204        {
    215             // Simply add the shape as is.
     205            // Set local scaling right when adding it. It can include both scaling parameters
     206            // and shape parameters (like sphere radius)
    216207            shape->getCollisionShape()->setLocalScaling(shape->getTotalScaling());
    217208            this->collisionShape_ = shape;
     209            // recalculate inertia tensor
     210            if (this->isDynamic())
     211            {
     212                internalSetMassProps();
     213            }
    218214        }
    219215        else
     
    246242    }
    247243
    248     //BlinkingBillboard* WorldEntity::getAttachedAsdfObject(unsigned int index) const
    249     //{
    250     //    return 0;
    251     //}
     244    void WorldEntity::mergeCollisionShape(CollisionShape* shape)
     245    {
     246        if (!this->collisionShape_)
     247            this->collisionShape_ = new CompoundCollisionShape(this);
     248        assert(this->collisionShape_->isCompoundShape());
     249
     250        // merge with transform
     251        CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_);
     252        assert(compoundShape);
     253        compoundShape->addChildShape(shape);
     254
     255        // recalculate inertia tensor
     256        internalSetMassProps();
     257    }
    252258
    253259    void WorldEntity::setScale3D(const Vector3& scale)
     
    270276    {
    271277        this->mass_ = mass;
    272         if (!hasPhysics())
    273             COUT(2) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl;
     278        if (!this->hasPhysics())
     279            COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl;
    274280        else if (this->physicalBody_->isInWorld())
    275             COUT(2) << "Warning: Cannot set the physical mass at run time. Storing temporarily." << std::endl;
    276         else
    277         {
    278             if (this->collisionType_ != Dynamic)
    279                 COUT(2) << "Warning: Cannot set the physical mass of a static or kinematic object. Storing temporarily." << std::endl;
    280             else if (mass == 0.0f)
    281                 COUT(2) << "Warning: Cannot set physical mass of a dynamic object to zero. Storing temporarily." << std::endl;
    282             else
    283                 this->physicalBody_->setMassProps(mass, btVector3(0,0,0));
    284         }
     281            COUT(2) << "Warning: Cannot set the physical mass at run time. Storing new mass." << std::endl;
     282        else
     283            internalSetMassProps();
     284    }
     285
     286    void WorldEntity::internalSetMassProps()
     287    {
     288        assert(hasPhysics());
     289
     290        if ((this->isKinematic() || this->isStatic()) && (this->mass_ + this->childMass_) != 0.0f)
     291        {
     292            // Mass non zero is a bad idea for kinematic and static objects
     293            this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0));
     294        }
     295        else if (this->isDynamic() && (this->mass_ + this->childMass_) == 0.0f)
     296        {
     297            // Mass zero is not such a good idea for dynamic objects
     298            this->physicalBody_->setMassProps(1.0f, this->getLocalInertia(1.0f));
     299        }
     300        else
     301            this->physicalBody_->setMassProps(this->mass_, this->getLocalInertia(this->mass_ + this->childMass_));
     302    }
     303
     304    btVector3 WorldEntity::getLocalInertia(btScalar mass) const
     305    {
     306        btVector3 inertia(0, 0, 0);
     307        if (this->collisionShape_)
     308            this->collisionShape_->getCollisionShape()->calculateLocalInertia(mass, inertia);
     309        return inertia;
    285310    }
    286311
     
    300325        if (type != None && this->collisionType_ == None)
    301326        {
     327            // Check whether there was some scaling applied.
     328            if (!this->node_->getScale().positionEquals(Vector3(1, 1, 1), 0.001))
     329                ThrowException(NotImplemented, "Cannot create a physical body if there is scaling applied to the node: Not yet implemented.");
     330
    302331            // Create new rigid body
    303332            btCollisionShape* temp = 0;
     
    337366        // update our variable for faster checks
    338367        updateCollisionType();
    339 
    340         // Mass non zero is a bad idea for kinematic and static objects
    341         if ((type == Kinematic || type == Static) && this->mass_ != 0.0f)
    342             this->setMass(0.0f);
    343         // Mass zero is not such a good idea for dynamic objects
    344         else if (type == Dynamic && this->mass_ == 0.0f)
    345             this->setMass(1.0f);
    346         else if (hasPhysics())
    347             this->physicalBody_->setMassProps(this->mass_, btVector3(0,0,0));
    348     }
    349 
    350     void WorldEntity::updateCollisionType()
    351     {
    352         if (!this->physicalBody_)
    353             this->collisionType_ = None;
    354         else if (this->physicalBody_->isKinematicObject())
    355             this->collisionType_ = Kinematic;
    356         else if (this->physicalBody_->isStaticObject())
    357             this->collisionType_ = Static;
    358         else
    359             this->collisionType_ = Dynamic;
     368        assert(this->collisionType_ == type);
     369
     370        // update mass and inertia tensor
     371        internalSetMassProps(); // type is not None! See case None: in switch
    360372    }
    361373
     
    395407    }
    396408
     409    void WorldEntity::updateCollisionType()
     410    {
     411        if (!this->physicalBody_)
     412            this->collisionType_ = None;
     413        else if (this->physicalBody_->isKinematicObject())
     414            this->collisionType_ = Kinematic;
     415        else if (this->physicalBody_->isStaticObject())
     416            this->collisionType_ = Static;
     417        else
     418            this->collisionType_ = Dynamic;
     419    }
     420
    397421    bool WorldEntity::checkPhysics() const
    398422    {
Note: See TracChangeset for help on using the changeset viewer.