Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 10, 2008, 1:38:17 PM (16 years ago)
Author:
rgrieder
Message:

Trying to synchronise phyiscs over the network.

  • Removed derivation of CollisionShape from WorldEntity (BaseObject instead).
File:
1 edited

Legend:

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

    r2313 r2374  
    1414 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1515 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  *   GNU General Public License for more details.
     16 *   GNU General Public License for more details. 
    1717 *
    1818 *   You should have received a copy of the GNU General Public License
     
    7373        // Default behaviour does not include physics
    7474        this->physicalBody_ = 0;
    75         this->collisionShape_ = 0;
     75        this->collisionShape_ = new CompoundCollisionShape(this);
    7676        this->mass_ = 0;
    7777        this->childMass_ = 0;
    7878        this->collisionType_ = None;
     79        this->collisionTypeSynchronised_ = None;
    7980
    8081        this->registerVariables();
     
    113114        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
    114115
     116        // Physics
    115117        XMLPortParam(WorldEntity, "collisionType", setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode);
    116         //XMLPortParam(WorldEntity, "collisionRadius", setCollisionRadius, getCollisionRadius, xmlelement, mode);
    117118        XMLPortParam(WorldEntity, "mass", setMass, getMass, xmlelement, mode);
    118119
     120        // Other attached WorldEntities
    119121        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
     122        // Attached collision shapes
    120123        XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode);
    121124    }
     
    123126    void WorldEntity::registerVariables()
    124127    {
    125         REGISTERDATA(this->bActive_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
    126         REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
    127 
    128         REGISTERDATA(this->getScale3D().x, network::direction::toclient);
    129         REGISTERDATA(this->getScale3D().y, network::direction::toclient);
    130         REGISTERDATA(this->getScale3D().z, network::direction::toclient);
     128        REGISTERDATA(this->bActive_,     network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
     129        REGISTERDATA(this->bVisible_,    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
     130
     131        // HACK: Removed the call because it gets called the first time as well
     132        REGISTERDATA(this->getScale3D(), network::direction::toclient);//, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged));
     133
     134        int* collisionType = (int*)&this->collisionTypeSynchronised_;
     135        REGISTERDATA(*collisionType, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
     136        REGISTERDATA(this->mass_,    network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged));
    131137
    132138        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
     
    138144        if (parent)
    139145            this->attachToParent(parent);
     146    }
     147
     148    void WorldEntity::collisionTypeChanged()
     149    {
     150        if (this->collisionTypeSynchronised_ != Dynamic &&
     151            this->collisionTypeSynchronised_ != Kinematic &&
     152            this->collisionTypeSynchronised_ != Static &&
     153            this->collisionTypeSynchronised_ != None)
     154        {
     155            CCOUT(1) << "Error when collsion Type was received over network. Unknown enum value:" << this->collisionTypeSynchronised_ << std::endl;
     156        }
     157        else if (this->collisionTypeSynchronised_ != collisionType_)
     158        {
     159            if (this->parent_)
     160                CCOUT(2) << "Warning: Network connection tried to set the collision type of an attached WE. Ignoring." << std::endl;
     161            else
     162                this->setCollisionType(this->collisionTypeSynchronised_);
     163        }
     164    }
     165
     166    void WorldEntity::massChanged()
     167    {
     168        this->setMass(this->mass_);
    140169    }
    141170
     
    210239    void WorldEntity::attachCollisionShape(CollisionShape* shape)
    211240    {
    212         this->attachedShapes_.push_back(shape);
    213 
    214         if (!this->collisionShape_ && shape->hasNoTransform())
    215         {
    216             // Set local scaling right when adding it. It can include both scaling parameters
    217             // and shape parameters (like sphere radius)
    218             shape->getCollisionShape()->setLocalScaling(shape->getTotalScaling());
    219             this->collisionShape_ = shape;
    220             // recalculate inertia tensor
    221             if (this->isDynamic())
    222                 internalSetMassProps();
    223         }
    224         else
    225         {
    226             if (this->collisionShape_ && !this->collisionShape_->isCompoundShape())
    227             {
    228                 // We have to create a new compound shape and add the old one first.
    229                 CollisionShape* thisShape = this->collisionShape_;
    230                 this->collisionShape_ = 0;
    231                 this->mergeCollisionShape(thisShape);
    232             }
    233             this->mergeCollisionShape(shape);
    234         }
     241        this->collisionShape_->addChildShape(shape);
    235242
    236243        if (this->physicalBody_)
     
    241248                removeFromPhysicalWorld();
    242249            }
    243             this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape());
    244         }
    245 
    246         addToPhysicalWorld();
     250            if (this->collisionShape_->getCollisionShape())
     251                this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape());
     252            // recalculate inertia tensor
     253            internalSetMassProps();
     254            addToPhysicalWorld();
     255        }
    247256    }
    248257
    249258    CollisionShape* WorldEntity::getAttachedCollisionShape(unsigned int index) const
    250259    {
    251         if (index < this->attachedShapes_.size())
    252             return attachedShapes_[index];
    253         else
    254             return 0;
    255     }
    256 
    257     void WorldEntity::mergeCollisionShape(CollisionShape* shape)
    258     {
    259         if (!this->collisionShape_)
    260             this->collisionShape_ = new CompoundCollisionShape(this);
    261         assert(this->collisionShape_->isCompoundShape());
    262 
    263         // merge with transform
    264         CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_);
    265         assert(compoundShape);
    266         compoundShape->addChildShape(shape);
    267 
    268         // recalculate inertia tensor
    269         internalSetMassProps();
     260        return this->collisionShape_->getChildShape(index);
    270261    }
    271262
     
    302293        this->mass_ = mass;
    303294        if (!this->hasPhysics())
    304             COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl;
     295            return;
     296        // TODO: Add this again when new network callbacks work properly
     297            //COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl;
    305298        else
    306299        {
     
    320313        assert(hasPhysics());
    321314
    322         if ((this->isKinematic() || this->isStatic()) && (this->mass_ + this->childMass_) != 0.0f)
    323         {
    324             // Mass non zero is a bad idea for kinematic and static objects
     315        if (this->isStatic())
     316        {
     317            // Just set everything to zero
    325318            this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0));
    326319        }
    327         else if (this->isDynamic() && (this->mass_ + this->childMass_) == 0.0f)
    328         {
    329             // Mass zero is not such a good idea for dynamic objects
     320        else if ((this->mass_ + this->childMass_) == 0.0f)
     321        {
     322            // Use default values to avoid very large or very small values
     323            CCOUT(2) << "Warning: Setting the internal physical mass to 1.0 because mass_ is 0.0." << std::endl;
    330324            this->physicalBody_->setMassProps(1.0f, this->getLocalInertia(1.0f));
    331325        }
     
    337331    {
    338332        btVector3 inertia(0, 0, 0);
    339         if (this->collisionShape_)
     333        if (this->collisionShape_->getCollisionShape())
    340334            this->collisionShape_->getCollisionShape()->calculateLocalInertia(mass, inertia);
    341335        return inertia;
     
    368362            this->physicalBody_ = new btRigidBody(bodyConstructionInfo);
    369363            this->physicalBody_->setUserPointer(this);
     364            this->physicalBody_->setActivationState(DISABLE_DEACTIVATION);
    370365        }
    371366        else if (type == None && this->collisionType_ != None)
     
    377372            this->physicalBody_ = 0;
    378373            this->collisionType_ = None;
     374            this->collisionTypeSynchronised_ = None;
    379375            return;
    380376        }
     
    401397
    402398        // update mass and inertia tensor
    403         internalSetMassProps(); // type is not None! See case None: in switch
     399        internalSetMassProps(); // type is not None! See case None in switch
    404400
    405401        addToPhysicalWorld();
     
    451447        else
    452448            this->collisionType_ = Dynamic;
     449        this->collisionTypeSynchronised_ = this->collisionType_;
     450    }
     451
     452    bool WorldEntity::isPhysicsRunning() const
     453    {
     454        return this->physicalBody_ && this->physicalBody_->isInWorld();
    453455    }
    454456
Note: See TracChangeset for help on using the changeset viewer.