Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 21, 2008, 2:44:00 PM (16 years ago)
Author:
rgrieder
Message:

Resolved four issues with the collision shapes:

  • NetworkCallback will of course not call functions virtually
  • CompoundCollisionShapes with a WorldEntity parent don't have to attach themselves when synchronised
  • Just in case: When changing a CollisionShape, the old gets destroyed AFTER everything has been updated
  • CompoundCollisionShapes with a WorldEntity parent now update both the WE and the CompoundCollisionShape when something changes

Other changes

  • Also replaced some redundant code while at it (updating the shapes was done individually)
  • Like in WE, the CompoundCollisionShape deletes all its children when being destroyed itself. This requires in WE that the children get detached before the CompoundCollisionShape gets deleted.
Location:
code/branches/presentation/src/orxonox/objects/collisionshapes
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation/src/orxonox/objects/collisionshapes/BoxCollisionShape.cc

    r2486 r2514  
    4545
    4646        this->halfExtents_ = Vector3(1, 1, 1);
    47         updateBox();
     47        updateShape();
    4848
    4949        this->registerVariables();
     
    5858    void BoxCollisionShape::registerVariables()
    5959    {
    60         registerVariable(this->halfExtents_, variableDirection::toclient, new NetworkCallback<BoxCollisionShape>(this, &BoxCollisionShape::updateBox));
     60        registerVariable(this->halfExtents_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
    6161    }
    6262
     
    7171    }
    7272
    73     void BoxCollisionShape::updateBox()
     73    btCollisionShape* BoxCollisionShape::createNewShape() const
    7474    {
    75         if (this->collisionShape_)
    76             delete this->collisionShape_;
    77         this->collisionShape_ = new btBoxShape(omni_cast<btVector3>(this->halfExtents_));
    78         this->updateParent();
     75        return new btBoxShape(omni_cast<btVector3>(this->halfExtents_));
    7976    }
    8077}
  • code/branches/presentation/src/orxonox/objects/collisionshapes/BoxCollisionShape.h

    r2486 r2514  
    4646
    4747            inline void setHalfExtents(const Vector3& extents)
    48                 { this->halfExtents_ = extents; updateBox(); }
     48                { this->halfExtents_ = extents; updateShape(); }
    4949            inline const Vector3& getHalfExtents() const
    5050                { return halfExtents_;}
    5151
    5252            inline void setWidth(float value)
    53                 { this->halfExtents_.z = value / 2; updateBox(); }
     53                { this->halfExtents_.z = value / 2; updateShape(); }
    5454            inline float getWidth() const
    5555                { return this->halfExtents_.z * 2; }
    5656
    5757            inline void setHeight(float value)
    58                 { this->halfExtents_.y = value / 2; updateBox(); }
     58                { this->halfExtents_.y = value / 2; updateShape(); }
    5959            inline float getHeight() const
    6060                { return this->halfExtents_.y * 2; }
    6161
    6262            inline void setLength(float value)
    63                 { this->halfExtents_.x = value / 2; updateBox(); }
     63                { this->halfExtents_.x = value / 2; updateShape(); }
    6464            inline float getLength() const
    6565                { return this->halfExtents_.x * 2; }
    6666
    67             void updateBox();
     67        private:
     68            btCollisionShape* createNewShape() const;
    6869
    69         private:
    7070            Vector3 halfExtents_;
    7171     };
  • code/branches/presentation/src/orxonox/objects/collisionshapes/CollisionShape.cc

    r2469 r2514  
    4242namespace orxonox
    4343{
    44     CreateFactory(CollisionShape);
    45 
    4644    CollisionShape::CollisionShape(BaseObject* creator)
    4745        : BaseObject(creator)
     
    6260    CollisionShape::~CollisionShape()
    6361    {
     62        // Detach from parent
     63        if (this->isInitialized() && this->parent_)
     64            this->parent_->removeChildShape(this);
    6465    }
    6566
     
    7980    void CollisionShape::registerVariables()
    8081    {
    81         registerVariable(this->parentID_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChanged));
     82        registerVariable(this->parentID_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChangedCallback));
    8283    }
    8384
    8485    void CollisionShape::parentChanged()
    8586    {
    86         Synchronisable* synchronisable = Synchronisable::getSynchronisable(this->parentID_);
    87         CompoundCollisionShape* CCSparent = dynamic_cast<CompoundCollisionShape*>(synchronisable);
    88         if (CCSparent)
    89             CCSparent->addChildShape(this);
    90         else
    91         {
    92             WorldEntity* WEparent = dynamic_cast<WorldEntity*>(synchronisable);
    93             if (WEparent)
    94                 WEparent->attachCollisionShape(this);
    95         }
     87        CompoundCollisionShape* parent = dynamic_cast<CompoundCollisionShape*>(Synchronisable::getSynchronisable(this->parentID_));
     88        if (parent)
     89            parent->addChildShape(this);
    9690    }
    9791
     
    118112    }
    119113
     114    void CollisionShape::updateShape()
     115    {
     116        btCollisionShape* oldShape = this->collisionShape_;
     117        this->collisionShape_ = this->createNewShape();
     118        // When we recreate the shape, we have to inform the parent about this to update the shape
     119        this->updateParent();
     120        if (oldShape)
     121            delete oldShape;
     122    }
     123
    120124    void CollisionShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
    121125    {
  • code/branches/presentation/src/orxonox/objects/collisionshapes/CollisionShape.h

    r2459 r2514  
    6666                { return this->scale_; }
    6767
     68            void updateShape();
     69
    6870            void calculateLocalInertia(float mass, btVector3& inertia) const;
    6971
     
    7880        protected:
    7981            virtual void updateParent();
     82            virtual void parentChanged();
     83            // Note: This is required because the NetworkCallback will not call functions virtually
     84            void parentChangedCallback() { this->parentChanged(); }
     85            virtual btCollisionShape* createNewShape() const = 0;
    8086
    8187            btCollisionShape*       collisionShape_;
     
    8389
    8490        private:
    85             void parentChanged();
    86 
    8791            Vector3                 position_;
    8892            Quaternion              orientation_;
  • code/branches/presentation/src/orxonox/objects/collisionshapes/CompoundCollisionShape.cc

    r2484 r2514  
    5454        if (this->isInitialized())
    5555        {
    56             // Notify children about removal
     56            // Delete all children
    5757            for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->childShapes_.begin();
    5858                it != this->childShapes_.end(); ++it)
    5959            {
     60                // make sure that the child doesn't want to detach itself --> speedup because of the missing update
    6061                it->first->setParent(0, OBJECTID_UNKNOWN);
     62                delete it->first;
    6163            }
    6264
     
    197199        if (this->parent_)
    198200            this->parent_->updateChildShape(this);
    199         else if (this->worldEntityParent_)
     201        if (this->worldEntityParent_)
    200202            this->worldEntityParent_->notifyCollisionShapeChanged();
     203    }
     204
     205    void CompoundCollisionShape::parentChanged()
     206    {
     207        if (!this->worldEntityParent_)
     208            CollisionShape::parentChanged();
    201209    }
    202210
  • code/branches/presentation/src/orxonox/objects/collisionshapes/CompoundCollisionShape.h

    r2469 r2514  
    3333
    3434#include <vector>
     35#include <cassert>
    3536#include "CollisionShape.h"
    3637
     
    5960        private:
    6061            void updatePublicShape();
     62            void parentChanged();
     63            inline virtual btCollisionShape* createNewShape() const
     64                { assert(false); return 0; }
    6165
    6266            btCompoundShape* compoundShape_;
  • code/branches/presentation/src/orxonox/objects/collisionshapes/ConeCollisionShape.cc

    r2486 r2514  
    4646        this->radius_ = 1.0f;
    4747        this->height_ = 1.0f;
    48         updateCone();
     48        updateShape();
    4949
    5050        this->registerVariables();
     
    5959    void ConeCollisionShape::registerVariables()
    6060    {
    61         registerVariable(this->radius_, variableDirection::toclient, new NetworkCallback<ConeCollisionShape>(this, &ConeCollisionShape::updateCone));
    62         registerVariable(this->height_, variableDirection::toclient, new NetworkCallback<ConeCollisionShape>(this, &ConeCollisionShape::updateCone));
     61        registerVariable(this->radius_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
     62        registerVariable(this->height_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
    6363    }
    6464
     
    7171    }
    7272
    73     void ConeCollisionShape::updateCone()
     73    btCollisionShape* ConeCollisionShape::createNewShape() const
    7474    {
    75         if (this->collisionShape_)
    76             delete this->collisionShape_;
    77         this->collisionShape_ = new btConeShape(this->radius_, this->height_);
    78         this->updateParent();
     75        return  new btConeShape(this->radius_, this->height_);
    7976    }
    8077}
  • code/branches/presentation/src/orxonox/objects/collisionshapes/ConeCollisionShape.h

    r2486 r2514  
    4646
    4747            inline void setRadius(float value)
    48                 { this->radius_ = value; updateCone(); }
     48                { this->radius_ = value; updateShape(); }
    4949            inline float getRadius() const
    5050                { return radius_; }
    5151
    5252            inline void setHeight(float value)
    53                 { this->height_ = value; updateCone(); }
     53                { this->height_ = value; updateShape(); }
    5454            inline float getHeight() const
    5555                { return this->height_; }
    5656
    57             void updateCone();
     57        private:
     58            btCollisionShape* createNewShape() const;
    5859
    59         private:
    6060            float radius_;
    6161            float height_;
  • code/branches/presentation/src/orxonox/objects/collisionshapes/PlaneCollisionShape.cc

    r2459 r2514  
    4646        this->normal_ = Vector3(0, 1, 0);
    4747        this->offset_ = 0.0f;
    48         updatePlane();
     48        updateShape();
    4949
    5050        this->registerVariables();
     
    5959    void PlaneCollisionShape::registerVariables()
    6060    {
    61         registerVariable(this->normal_, variableDirection::toclient, new NetworkCallback<PlaneCollisionShape>(this, &PlaneCollisionShape::updatePlane));
    62         registerVariable(this->offset_, variableDirection::toclient, new NetworkCallback<PlaneCollisionShape>(this, &PlaneCollisionShape::updatePlane));
     61        registerVariable(this->normal_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
     62        registerVariable(this->offset_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
    6363    }
    6464
     
    7171    }
    7272
    73     void PlaneCollisionShape::updatePlane()
     73    btCollisionShape* PlaneCollisionShape::createNewShape() const
    7474    {
    75         if (this->collisionShape_)
    76             delete this->collisionShape_;
    77         this->collisionShape_ = new btStaticPlaneShape(omni_cast<btVector3>(this->normal_), this->offset_);
    78         this->updateParent();
     75        return new btStaticPlaneShape(omni_cast<btVector3>(this->normal_), this->offset_);
    7976    }
    8077}
  • code/branches/presentation/src/orxonox/objects/collisionshapes/PlaneCollisionShape.h

    r2459 r2514  
    4646
    4747            inline void setNormal(const Vector3& normal)
    48                 { this->normal_ = normal; updatePlane(); }
     48                { this->normal_ = normal; updateShape(); }
    4949            inline const Vector3& getNormal()
    5050                { return normal_;}
    5151
    5252            inline void setOffset(float offset)
    53                 { this->offset_ = offset; updatePlane(); }
     53                { this->offset_ = offset; updateShape(); }
    5454            inline float getOffset()
    5555                { return this->offset_;}
    5656
    57             void updatePlane();
     57        private:
     58            btCollisionShape* createNewShape()const;
    5859
    59         private:
    6060            Vector3 normal_;
    6161            float   offset_;
  • code/branches/presentation/src/orxonox/objects/collisionshapes/SphereCollisionShape.cc

    r2459 r2514  
    4545
    4646        this->radius_ = 1.0f;
    47         updateSphere();
     47        updateShape();
    4848
    4949        this->registerVariables();
     
    5858    void SphereCollisionShape::registerVariables()
    5959    {
    60         registerVariable(this->radius_, variableDirection::toclient, new NetworkCallback<SphereCollisionShape>(this, &SphereCollisionShape::updateSphere));
     60        registerVariable(this->radius_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::updateShape));
    6161    }
    6262
     
    6868    }
    6969
    70     void SphereCollisionShape::updateSphere()
     70    btCollisionShape* SphereCollisionShape::createNewShape() const
    7171    {
    72         if (this->collisionShape_)
    73             delete this->collisionShape_;
    74         // When we recreate the shape, we have to inform the parent about this to update the shape
    75         this->collisionShape_ = new btSphereShape(this->radius_);
    76         this->updateParent();
     72        return new btSphereShape(this->radius_);
    7773    }
    7874}
  • code/branches/presentation/src/orxonox/objects/collisionshapes/SphereCollisionShape.h

    r2486 r2514  
    4646
    4747            inline void setRadius(float radius)
    48                 { this->radius_ = radius; updateSphere(); }
     48                { this->radius_ = radius; updateShape(); }
    4949            inline float getRadius() const
    5050                { return this->radius_; }
    5151
    52             void updateSphere();
     52        private:
     53            btCollisionShape* createNewShape() const;
    5354
    54         private:
    5555            float radius_;
    5656    };
Note: See TracChangeset for help on using the changeset viewer.