Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2306


Ignore:
Timestamp:
Dec 2, 2008, 4:59:36 PM (15 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
Location:
code/branches/physics/src/orxonox/objects
Files:
6 edited

Legend:

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

    r2304 r2306  
    172172    void Scene::tick(float dt)
    173173    {
     174        // TODO: This is not stable! If physics cannot be calculated real time anymore,
     175        //       framerate will drop exponentially.
    174176        if (physicalWorld_)
    175             physicalWorld_->stepSimulation(dt,(int)(dt/0.0166666f));
     177            physicalWorld_->stepSimulation(dt,(int)(dt/0.0166666f + 1.0f));
    176178    }
    177179
  • code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc

    r2304 r2306  
    3232#include "BulletDynamics/Dynamics/btRigidBody.h"
    3333
     34#include "util/Debug.h"
    3435#include "util/Exception.h"
    3536#include "core/CoreIncludes.h"
     
    220221        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
    221222        const btVector3& velocity = this->physicalBody_->getLinearVelocity();
    222         this->velocity_ = Vector3(velocity.x(), velocity.y(), velocity.z());
     223        this->velocity_.x = velocity.x();
     224        this->velocity_.y = velocity.y();
     225        this->velocity_.z = velocity.z();
    223226        velocityChanged();
    224227        positionChanged();
  • 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    {
  • code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.h

    r2304 r2306  
    108108                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
    109109
    110             void setScale3D(const Vector3& scale);
     110            virtual void setScale3D(const Vector3& scale);
    111111            inline void setScale3D(float x, float y, float z)
    112112                { this->setScale3D(Vector3(x, y, z)); }
     
    119119                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
    120120
    121             void scale3D(const Vector3& scale);
     121            virtual void scale3D(const Vector3& scale);
    122122            inline void scale3D(float x, float y, float z)
    123123                { this->scale3D(Vector3(x, y, z)); }
     
    182182            };
    183183
    184             bool hasPhysics() const { return getCollisionType() != None; }
    185 
    186             CollisionType getCollisionType() const { return this->collisionType_; }
    187             void setCollisionType(CollisionType type);
    188 
    189             void setCollisionTypeStr(const std::string& type);
    190             std::string getCollisionTypeStr() const;
    191 
     184            bool hasPhysics()  const { return getCollisionType() != None     ; }
    192185            bool isStatic()    const { return getCollisionType() == Static   ; }
    193186            bool isKinematic() const { return getCollisionType() == Kinematic; }
    194187            bool isDynamic()   const { return getCollisionType() == Dynamic  ; }
    195188
     189            inline CollisionType getCollisionType() const
     190                { return this->collisionType_; }
     191            void setCollisionType(CollisionType type);
     192
     193            void setCollisionTypeStr(const std::string& type);
     194            std::string getCollisionTypeStr() const;
     195
    196196            void setMass(float mass);
    197197            inline float getMass() const
     
    201201            CollisionShape* getAttachedCollisionShape(unsigned int index) const;
    202202
    203             CollisionShape* getCollisionShape() { return this->collisionShape_; }
    204             btRigidBody* getPhysicalBody() { return this->physicalBody_; }
     203            inline CollisionShape* getCollisionShape()
     204                { return this->collisionShape_; }
     205            inline btRigidBody* getPhysicalBody()
     206                { return this->physicalBody_; }
    205207
    206208        protected:
    207             //virtual btCollisionShape* getCollisionShape() = 0;
    208             //virtual void attachPhysicalObject(WorldEntity* object);
    209 
    210209            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
     210
     211            btRigidBody*  physicalBody_;
     212
     213        private:
     214            void updateCollisionType();
     215            void mergeCollisionShape(CollisionShape* shape);
     216            void internalSetMassProps();
     217            btVector3 getLocalInertia(btScalar mass) const;
    211218            bool checkPhysics() const;
    212             void updateCollisionType();
    213 
    214             btRigidBody*  physicalBody_;
    215 
    216         private:
    217             void mergeCollisionShape(CollisionShape* shape);
    218219
    219220            CollisionType                collisionType_;
     
    221222            CollisionShape*              collisionShape_;
    222223            btScalar                     mass_;
     224            btScalar                     childMass_;
    223225    };
    224226}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CollisionShape.cc

    r2304 r2306  
    7878        return omni_cast<btVector3>(this->node_->getScale());
    7979    }
     80
     81    void CollisionShape::setScale3D(const Vector3& scale)
     82    {
     83        ThrowException(NotImplemented, "Cannot set the scale of a collision shape: Not yet implemented.");
     84    }
     85
     86    void CollisionShape::scale3D(const Vector3& scale)
     87    {
     88        ThrowException(NotImplemented, "Cannot set the scale of a collision shape: Not yet implemented.");
     89    }
    8090}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CollisionShape.h

    r2304 r2306  
    5555
    5656        private:
     57            virtual void setScale3D(const Vector3& scale);
     58            virtual void scale3D(const Vector3& scale);
     59
    5760            bool isCollisionTypeLegal(WorldEntity::CollisionType type) const;
    5861    };
Note: See TracChangeset for help on using the changeset viewer.