Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 13, 2008, 11:18:44 PM (16 years ago)
Author:
rgrieder
Message:

Added XML Parameters for restitution, angular factor, linear damping, angular damping and friction to WorldEntity.

Location:
code/branches/physics/src/orxonox/objects/worldentities
Files:
2 edited

Legend:

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

    r2426 r2429  
    7272        this->bPhysicsActive_ = false;
    7373        this->collisionShape_ = new CompoundCollisionShape(this);
    74         this->mass_ = 0;
    75         this->childrenMass_ = 0;
    7674        this->collisionType_ = None;
    7775        this->collisionTypeSynchronised_ = None;
     76        this->mass_           = 0;
     77        this->childrenMass_   = 0;
     78        // Use bullet default values
     79        this->restitution_    = 0;
     80        this->angularFactor_  = 1;
     81        this->linearDamping_  = 0;
     82        this->angularDamping_ = 0;
     83        this->friction_       = 0.5;
    7884
    7985        this->registerVariables();
     
    103109        SUPER(WorldEntity, XMLPort, xmlelement, mode);
    104110
    105         XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
     111        XMLPortParamTemplate(WorldEntity, "position",    setPosition,    getPosition,    xmlelement, mode, const Vector3&);
    106112        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
    107         XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode);
    108         XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode);
    109         XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode);
    110         XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode);
    111         XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode);
    112         XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
    113         XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
     113        XMLPortParamTemplate(WorldEntity, "scale3D",     setScale3D,     getScale3D,     xmlelement, mode, const Vector3&);
     114        XMLPortParam        (WorldEntity, "scale",       setScale,       getScale,      xmlelement, mode);
     115        XMLPortParamLoadOnly(WorldEntity, "lookat",      lookAt_xmlport,      xmlelement, mode);
     116        XMLPortParamLoadOnly(WorldEntity, "direction",   setDirection_xmlport, xmlelement, mode);
     117        XMLPortParamLoadOnly(WorldEntity, "yaw",         yaw_xmlport,          xmlelement, mode);
     118        XMLPortParamLoadOnly(WorldEntity, "pitch",       pitch_xmlport,        xmlelement, mode);
     119        XMLPortParamLoadOnly(WorldEntity, "roll",        roll_xmlport,        xmlelement, mode);
    114120
    115121        // Physics
    116         XMLPortParam(WorldEntity, "collisionType", setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode);
    117         XMLPortParam(WorldEntity, "mass", setMass, getMass, xmlelement, mode);
     122        XMLPortParam(WorldEntity, "collisionType",  setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode);
     123        XMLPortParam(WorldEntity, "mass",           setMass,             getMass,             xmlelement, mode);
     124        XMLPortParam(WorldEntity, "restitution",    setRestitution,      getRestitution,      xmlelement, mode);
     125        XMLPortParam(WorldEntity, "angularFactor",  setAngularFactor,    getAngularFactor,    xmlelement, mode);
     126        XMLPortParam(WorldEntity, "linearDamping",  setLinearDamping,    getLinearDamping,    xmlelement, mode);
     127        XMLPortParam(WorldEntity, "angularDamping", setAngularDamping,   getAngularDamping,   xmlelement, mode);
     128        XMLPortParam(WorldEntity, "friction",       setFriction,         getFriction,         xmlelement, mode);
    118129
    119130        // Other attached WorldEntities
     
    125136    void WorldEntity::registerVariables()
    126137    {
    127         REGISTERDATA(this->bActive_,     network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
    128         REGISTERDATA(this->bVisible_,    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
    129 
    130         REGISTERDATA(this->getScale3D(), network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged));
     138        REGISTERDATA(this->bActive_,        network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
     139        REGISTERDATA(this->bVisible_,       network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
     140
     141        REGISTERDATA(this->getScale3D(),    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged));
    131142
    132143        REGISTERDATA((int&)this->collisionTypeSynchronised_,
    133                                          network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
    134         REGISTERDATA(this->mass_,        network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged));
     144                                            network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
     145        REGISTERDATA(this->mass_,           network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged));
     146        REGISTERDATA(this->restitution_,    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::restitutionChanged));
     147        REGISTERDATA(this->angularFactor_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::angularFactorChanged));
     148        REGISTERDATA(this->linearDamping_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::linearDampingChanged));
     149        REGISTERDATA(this->angularDamping_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::angularDampingChanged));
     150        REGISTERDATA(this->friction_,       network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::frictionChanged));
    135151        REGISTERDATA(this->bPhysicsActiveSynchronised_,
    136                                          network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::physicsActivityChanged));
    137 
    138         REGISTERDATA(this->parentID_,    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
    139     }
    140 
    141     void WorldEntity::updateParent()
     152                                            network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::physicsActivityChanged));
     153
     154        REGISTERDATA(this->parentID_,       network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::parentChanged));
     155    }
     156
     157    void WorldEntity::parentChanged()
    142158    {
    143159        WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
     
    162178                this->setCollisionType(this->collisionTypeSynchronised_);
    163179        }
    164     }
    165 
    166     void WorldEntity::massChanged()
    167     {
    168         this->setMass(this->mass_);
    169180    }
    170181
     
    214225        // mass
    215226        this->childrenMass_ += object->getMass();
    216         recalculatePhysicsProps();
     227        recalculateMassProps();
    217228    }
    218229
     
    225236        {
    226237            this->childrenMass_ -= object->getMass();
    227             recalculatePhysicsProps();
     238            recalculateMassProps();
    228239        }
    229240
     
    418429    }
    419430
    420     void WorldEntity::setMass(float mass)
    421     {
    422         this->mass_ = mass;
    423         recalculatePhysicsProps();
    424     }
    425 
    426431    void WorldEntity::setCollisionType(CollisionType type)
    427432    {
     
    430435            ThrowException(PhysicsViolation, "Cannot set the collision type of a WorldEntity with a parent");
    431436        else if (this->addedToPhysicalWorld())
    432             ThrowException(PhysicsViolation, "Warning: Cannot set the collision type at run time.");
     437            ThrowException(PhysicsViolation, "Cannot set the collision type at run time.");
    433438
    434439        // Check for type legality. Could be StaticEntity or MobileEntity
    435440        if (!this->isCollisionTypeLegal(type))
    436441            return; // exception gets issued anyway
     442        if (type != None && !this->getScene()->hasPhysics())
     443            ThrowException(PhysicsViolation, "Cannot have physical bodies in a non physical scene");
    437444
    438445        // Check whether we have to create or destroy.
     
    444451
    445452            // Create new rigid body
    446             btCollisionShape* temp = 0;
    447453            btRigidBody::btRigidBodyConstructionInfo bodyConstructionInfo(0, this, this->collisionShape_->getCollisionShape());
    448             bodyConstructionInfo.m_restitution = 1;
    449454            this->physicalBody_ = new btRigidBody(bodyConstructionInfo);
    450455            this->physicalBody_->setUserPointer(this);
     
    485490
    486491        // update mass and inertia tensor
    487         recalculatePhysicsProps();
     492        recalculateMassProps();
     493        resetPhysicsProps();
    488494        activatePhysics();
    489495    }
     
    544550        for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it)
    545551            this->childrenMass_ += (*it)->getMass();
    546         recalculatePhysicsProps();
     552        recalculateMassProps();
    547553        // Notify parent WE
    548554        if (this->parent_)
     
    564570                this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape());
    565571        }
    566         recalculatePhysicsProps();
    567     }
    568 
    569     void WorldEntity::recalculatePhysicsProps()
     572        recalculateMassProps();
     573    }
     574
     575    void WorldEntity::recalculateMassProps()
    570576    {
    571577        if (this->hasPhysics())
     
    589595        }
    590596    }
     597
     598    void WorldEntity::resetPhysicsProps()
     599    {
     600        if (this->hasPhysics())
     601        {
     602            this->physicalBody_->setRestitution(this->restitution_);
     603            this->physicalBody_->setAngularFactor(this->angularFactor_);
     604            this->physicalBody_->setDamping(this->linearDamping_, this->angularDamping_);
     605            this->physicalBody_->setFriction(this->friction_);
     606        }
     607    }
    591608}
  • code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.h

    r2426 r2429  
    141141
    142142        private:
    143             void updateParent();
    144 
    145143            inline void lookAt_xmlport(const Vector3& target)
    146144                { this->lookAt(target); }
     
    154152                { this->roll(angle); }
    155153
     154            // network callbacks
     155            void parentChanged();
    156156            inline void scaleChanged()
    157157                { this->setScale3D(this->getScale3D()); }
     
    192192            std::string getCollisionTypeStr() const;
    193193
    194             void setMass(float mass);
     194            inline void setMass(float mass)
     195                { this->mass_ = mass; recalculateMassProps(); }
    195196            inline float getMass() const
    196197                { return this->mass_; }
     198
    197199            inline float getTotalMass() const
    198200                { return this->mass_ + this->childrenMass_; }
     201
     202            inline void setRestitution(float restitution)
     203                { this->restitution_ = restitution; resetPhysicsProps(); }
     204            inline float getRestitution() const
     205                { return this->restitution_; }
     206
     207            inline void setAngularFactor(float angularFactor)
     208                { this->angularFactor_ = angularFactor; resetPhysicsProps(); }
     209            inline float getAngularFactor() const
     210                { return this->angularFactor_; }
     211
     212            inline void setLinearDamping(float linearDamping)
     213                { this->linearDamping_ = linearDamping; resetPhysicsProps(); }
     214            inline float getLinearDamping() const
     215                { return this->linearDamping_; }
     216
     217            inline void setAngularDamping(float angularDamping)
     218                { this->angularDamping_ = angularDamping; resetPhysicsProps(); }
     219            inline float getAngularDamping() const
     220                { return this->angularDamping_; }
     221
     222            inline void setFriction(float friction)
     223                { this->friction_ = friction; resetPhysicsProps(); }
     224            inline float getFriction() const
     225                { return this->friction_; }
    199226
    200227            void attachCollisionShape(CollisionShape* shape);
     
    217244        private:
    218245            void updateCollisionType();
    219             void recalculatePhysicsProps();
     246            void recalculateMassProps();
     247            void resetPhysicsProps();
    220248
    221249            // network callbacks
    222250            void collisionTypeChanged();
    223             void massChanged();
    224251            void physicsActivityChanged();
     252            inline void massChanged()
     253                { this->setMass(this->mass_); }
     254            inline void restitutionChanged()
     255                { this->setRestitution(this->restitution_); }
     256            inline void angularFactorChanged()
     257                { this->setAngularFactor(this->angularFactor_); }
     258            inline void linearDampingChanged()
     259                { this->setLinearDamping(this->linearDamping_); }
     260            inline void angularDampingChanged()
     261                { this->setAngularDamping(this->angularDamping_); }
     262            inline void frictionChanged()
     263                { this->setFriction(this->friction_); }
    225264
    226265            CollisionType                collisionType_;
     
    230269            CompoundCollisionShape*      collisionShape_;
    231270            btScalar                     mass_;
     271            btScalar                     restitution_;
     272            btScalar                     angularFactor_;
     273            btScalar                     linearDamping_;
     274            btScalar                     angularDamping_;
     275            btScalar                     friction_;
    232276            btScalar                     childrenMass_;
    233277    };
Note: See TracChangeset for help on using the changeset viewer.