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).
Location:
code/branches/physics/src/orxonox/objects/worldentities/collisionshapes
Files:
8 edited

Legend:

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

    r2306 r2374  
    3737#include "tools/BulletConversions.h"
    3838
     39#include "CompoundCollisionShape.h"
     40
    3941namespace orxonox
    4042{
    4143    CreateFactory(CollisionShape);
    4244
    43     CollisionShape::CollisionShape(BaseObject* creator) : StaticEntity(creator)
     45    CollisionShape::CollisionShape(BaseObject* creator)
     46        : BaseObject(creator)
     47        , network::Synchronisable(creator)
    4448    {
    4549        RegisterObject(CollisionShape);
    4650
    47         this->bIsCompound_ = false;
     51        this->parent_ = 0;
     52        this->parentID_ = (unsigned int)-1;
     53        this->collisionShape_ = 0;
    4854    }
    4955
     
    5561    {
    5662        SUPER(CollisionShape, XMLPort, xmlelement, mode);
     63
     64        XMLPortParamTemplate(CollisionShape, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
     65        XMLPortParamTemplate(CollisionShape, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
     66        XMLPortParamLoadOnly(CollisionShape, "yaw",   yaw,   xmlelement, mode);
     67        XMLPortParamLoadOnly(CollisionShape, "pitch", pitch, xmlelement, mode);
     68        XMLPortParamLoadOnly(CollisionShape, "roll",  roll,  xmlelement, mode);
     69        XMLPortParamTemplate(CollisionShape, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
     70        XMLPortParamLoadOnly(CollisionShape, "scale", setScale, xmlelement, mode);
    5771    }
    5872
    59     bool CollisionShape::isCollisionTypeLegal(WorldEntity::CollisionType type) const
     73    void CollisionShape::registerVariables()
    6074    {
    61         if (type != WorldEntity::None)
    62         {
    63             ThrowException(PhysicsViolation, "A CollisionShape can only have CollisionType 'None'.");
    64             return false;
    65         }
    66         else
    67             return true;
     75        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<CollisionShape>(this, &CollisionShape::updateParent));
    6876    }
    6977
    70     bool CollisionShape::hasNoTransform() const
     78    void CollisionShape::updateParent()
    7179    {
    72         return (this->getPosition().positionEquals(Vector3(0, 0, 0), 0.001) &&
    73                 this->getOrientation().equals(Quaternion(1,0,0,0), Degree(0.1)));
     80        CompoundCollisionShape* parent = dynamic_cast<CompoundCollisionShape*>(Synchronisable::getSynchronisable(this->parentID_));
     81        if (parent)
     82            parent->addChildShape(this);
    7483    }
    7584
    76     btVector3 CollisionShape::getTotalScaling()
     85    bool CollisionShape::hasTransform() const
    7786    {
    78         return omni_cast<btVector3>(this->node_->getScale());
     87        return (!this->position_.positionEquals(Vector3(0, 0, 0), 0.001) ||
     88                !this->orientation_.equals(Quaternion(1,0,0,0), Degree(0.1)));
    7989    }
    8090
     
    8494    }
    8595
    86     void CollisionShape::scale3D(const Vector3& scale)
     96    void CollisionShape::setScale(float scale)
    8797    {
    8898        ThrowException(NotImplemented, "Cannot set the scale of a collision shape: Not yet implemented.");
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CollisionShape.h

    r2306 r2374  
    3232#include "OrxonoxPrereqs.h"
    3333
    34 #include "objects/worldentities/StaticEntity.h"
     34#include "util/Math.h"
     35#include "core/BaseObject.h"
    3536
    3637namespace orxonox
    3738{
    38     class _OrxonoxExport CollisionShape : public StaticEntity
     39    class _OrxonoxExport CollisionShape : public BaseObject, public network::Synchronisable
    3940    {
    4041        public:
     
    4344
    4445            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     46            void registerVariables();
    4547
    46             btCollisionShape* getCollisionShape() const { return this->collisionShape_; }
    47             bool isCompoundShape() const { return this->bIsCompound_; }
     48            inline void setPosition(const Vector3& position)
     49                { this->position_ = position; }
     50            inline const Vector3& getPosition() const
     51                { return this->position_; }
    4852
    49             bool hasNoTransform() const;
    50             virtual btVector3 getTotalScaling();
     53            inline void setOrientation(const Quaternion& orientation)
     54                { this->orientation_ = orientation; }
     55            inline const Quaternion& getOrientation() const
     56                { return this->orientation_; }
     57
     58            void yaw(const Degree& angle)   { this->orientation_ = this->orientation_ * Quaternion(angle, Vector3::UNIT_Y); }
     59            void pitch(const Degree& angle) { this->orientation_ = this->orientation_ * Quaternion(angle, Vector3::UNIT_X); }
     60            void roll(const Degree& angle)  { this->orientation_ = this->orientation_ * Quaternion(angle, Vector3::UNIT_Z); }
     61
     62            virtual void setScale3D(const Vector3& scale);
     63            virtual void setScale(float scale);
     64            inline const Vector3& getScale3D(void) const
     65                { return this->scale_; }
     66
     67            virtual inline btCollisionShape* getCollisionShape() const
     68                { return this->collisionShape_; }
     69
     70            bool hasTransform() const;
     71
     72            inline void setParent(CompoundCollisionShape* shape, unsigned int ID)
     73                { this->parent_ = shape; this->parentID_ = ID; }
    5174
    5275        protected:
    53             bool              bIsCompound_;
    5476            btCollisionShape* collisionShape_;
    5577
    5678        private:
    57             virtual void setScale3D(const Vector3& scale);
    58             virtual void scale3D(const Vector3& scale);
     79            void updateParent();
    5980
    60             bool isCollisionTypeLegal(WorldEntity::CollisionType type) const;
     81            Vector3           position_;
     82            Quaternion        orientation_;
     83            Vector3           scale_;
     84            CompoundCollisionShape* parent_;
     85            unsigned int      parentID_;
    6186    };
    6287}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CompoundCollisionShape.cc

    r2304 r2374  
    4444        RegisterObject(CompoundCollisionShape);
    4545
    46         this->bIsCompound_    = true;
     46        this->collisionShape_ = 0;
    4747        this->compoundShape_  = new btCompoundShape();
    48         this->collisionShape_ = this->compoundShape_;
    4948    }
    5049
     
    5857    {
    5958        SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode);
     59        // Attached collision shapes
     60        XMLPortObject(CompoundCollisionShape, CollisionShape, "", addChildShape, getChildShape, xmlelement, mode);
     61    }
    6062
    61         XMLPortObject(CompoundCollisionShape, CollisionShape, "", addChildShape, getChildShape, xmlelement, mode);
     63    btCollisionShape* CompoundCollisionShape::getCollisionShape() const
     64    {
     65        // Note: Returning collisionShape_ means that it's the only one and has no transform.
     66        //       So we can get rid of the additional overhead with the compound shape.
     67        if (this->collisionShape_)
     68            return this->collisionShape_;
     69        else if (this->childShapes_.size() != 0)
     70            return this->compoundShape_;
     71        else
     72            return 0;
    6273    }
    6374
    6475    void CompoundCollisionShape::addChildShape(CollisionShape* shape)
    6576    {
    66         this->childShapes_.push_back(shape);
     77        if (!shape || !shape->getCollisionShape())
     78            return;
     79        assert(this->compoundShape_);
    6780        btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition()));
    68         shape->getCollisionShape()->setLocalScaling(shape->getTotalScaling());
    6981        this->compoundShape_->addChildShape(transf, shape->getCollisionShape());
     82
     83        if (this->childShapes_.size() == 1 && this->childShapes_[0] && !this->childShapes_[0]->hasTransform())
     84        {
     85            // --> Only shape to be added, no transform; add it directly
     86            this->collisionShape_ = shape->getCollisionShape();
     87        }
     88        else
     89        {
     90            // Make sure we use the compound shape
     91            this->collisionShape_ = 0;
     92        }
     93
     94        // network synchro
     95        shape->setParent(this, this->getObjectID());
    7096    }
    7197
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CompoundCollisionShape.h

    r2304 r2374  
    4848            CollisionShape* getChildShape(unsigned int index) const;
    4949
     50            virtual btCollisionShape* getCollisionShape() const;
     51
    5052        private:
    5153            btCompoundShape* compoundShape_;
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/PlaneCollisionShape.cc

    r2323 r2374  
    2121 *
    2222 *   Author:
    23  *      Reto Grieder
     23 *      Martin Stypinski
    2424 *   Co-authors:
    2525 *      ...
     
    3030#include "PlaneCollisionShape.h"
    3131
    32 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
    33 
    34 #include "tools/BulletConversions.h"
    3532#include "core/CoreIncludes.h"
    3633#include "core/XMLPort.h"
     
    4542        RegisterObject(PlaneCollisionShape);
    4643
    47         this->planeShape_ = new btStaticPlaneShape(btVector3(1,1,1), 0);
     44        this->planeShape_ = new btStaticPlaneShape(btVector3(1, 1, 1), 0);
    4845        this->collisionShape_ = this->planeShape_;
     46        this->planeNormal_ = Vector3(1, 1, 1);
     47        this->planeOffset_ = 0.0f;
    4948    }
    5049
     
    5251    {
    5352        if (this->isInitialized())
    54             delete this->collisionShape_;
     53            delete this->planeShape_;
    5554    }
    5655
    57     void PlaneCollisionShape::setNormal(const Vector3& normal)
     56    void PlaneCollisionShape::registerVariables()
    5857    {
    59         btScalar offset = this->planeShape_->getPlaneConstant();
    60         delete this->collisionShape_;
    61         this->planeShape_ = new btStaticPlaneShape(omni_cast<btVector3>(normal), offset);
    62         this->collisionShape_ = this->planeShape_;
    63     }
    64 
    65     void PlaneCollisionShape::setOffset(float offset)
    66     {
    67         btVector3 normal = this->planeShape_->getPlaneNormal();
    68         delete this->collisionShape_;
    69         this->planeShape_ = new btStaticPlaneShape(normal, offset);
    70         this->collisionShape_ = this ->planeShape_;
     58        REGISTERDATA(this->planeNormal_, network::direction::toclient, new network::NetworkCallback<PlaneCollisionShape>(this, &PlaneCollisionShape::planeNormalChanged));
     59        REGISTERDATA(this->planeOffset_, network::direction::toclient, new network::NetworkCallback<PlaneCollisionShape>(this, &PlaneCollisionShape::planeOffsetChanged));
    7160    }
    7261
     
    7564        SUPER(PlaneCollisionShape, XMLPort, xmlelement, mode);
    7665
    77         XMLPortParam(PlaneCollisionShape, "normal", setNormal, getNormal, xmlelement, mode);
    78         XMLPortParam(PlaneCollisionShape, "offset", setOffset, getOffset, xmlelement, mode);   
     66        XMLPortParam(PlaneCollisionShape, "planeNormal", setNormal, getNormal, xmlelement, mode);
     67        XMLPortParam(PlaneCollisionShape, "planeOffset", setOffset, getOffset, xmlelement, mode);   
    7968    }
    8069
    81     btVector3 PlaneCollisionShape::getTotalScaling()
     70    void PlaneCollisionShape::setNormal(const Vector3& normal)
    8271    {
    83         return omni_cast<btVector3>(this->node_->getScale()); //* this->radius_;
     72        delete this->planeShape_;
     73        this->planeNormal_ = normal;
     74        this->planeShape_ = new btStaticPlaneShape(omni_cast<btVector3>(normal), this->planeOffset_);
     75        this->collisionShape_ = this->planeShape_;
     76    }
     77
     78    void PlaneCollisionShape::setOffset(float offset)
     79    {
     80        delete this->planeShape_;
     81        this->planeOffset_ = offset;
     82        this->planeShape_ = new btStaticPlaneShape(omni_cast<btVector3>(this->planeNormal_), offset);
     83        this->collisionShape_ = this->planeShape_;
    8484    }
    8585}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/PlaneCollisionShape.h

    r2323 r2374  
    2121 *
    2222 *   Author:
    23  *      Reto Grieder
     23 *      Martin Stypinski
    2424 *   Co-authors:
    2525 *      ...
     
    3333
    3434#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
    35 
    3635#include "CollisionShape.h"
    3736
     
    4443            virtual ~PlaneCollisionShape();
    4544
     45            void registerVariables();
    4646            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4747
    48                
    49            
    5048            inline float getOffset()
    5149                { return this->planeShape_->getPlaneConstant();}
     50            void setOffset(float offset);
    5251
    5352            inline btVector3 getNormal()
    5453                { return this->planeShape_->getPlaneNormal();}
    55             void setOffset(float offset);
    5654            void setNormal(const Vector3& normal);
    5755
    58            private:
    59             virtual btVector3 getTotalScaling();
     56            inline void planeNormalChanged()
     57                { this->setNormal(this->planeNormal_); }
    6058
     59            inline void planeOffsetChanged()
     60                { this->setOffset(this->planeOffset_); }
     61
     62        private:
    6163            btStaticPlaneShape* planeShape_;
     64            Vector3             planeNormal_;
     65            float               planeOffset_;
    6266     };
    6367}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/SphereCollisionShape.cc

    r2304 r2374  
    4646        this->sphereShape_ = new btSphereShape(1.0f);
    4747        this->collisionShape_ = this->sphereShape_;
     48
     49        this->registerVariables();
    4850    }
    4951
     
    5254        if (this->isInitialized())
    5355            delete this->collisionShape_;
     56    }
     57
     58    void SphereCollisionShape::registerVariables()
     59    {
     60        REGISTERDATA(this->radius_, network::direction::toclient, new network::NetworkCallback<SphereCollisionShape>(this, &SphereCollisionShape::radiusChanged));
    5461    }
    5562
     
    6168    }
    6269
    63     btVector3 SphereCollisionShape::getTotalScaling()
     70    void SphereCollisionShape::setRadius(float radius)
    6471    {
    65         return omni_cast<btVector3>(this->node_->getScale()) * this->radius_;
     72        // TODO: Think about where this could be referenced already.
     73        this->radius_ = radius;
     74        delete this->sphereShape_;
     75        this->sphereShape_ = new btSphereShape(radius);
    6676    }
    6777}
  • code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/SphereCollisionShape.h

    r2304 r2374  
    4242            virtual ~SphereCollisionShape();
    4343
     44            void registerVariables();
    4445            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4546
    46             inline void setRadius(float radius)
    47                 { this->radius_ = radius; }
     47            void setRadius(float radius);
    4848            inline float getRadius() const
    4949                { return this->radius_;}
    5050
     51            inline void radiusChanged()
     52                { this->setRadius(this->radius_); }
     53
    5154        private:
    52             virtual btVector3 getTotalScaling();
    53 
    5455            btSphereShape* sphereShape_;
    5556            float          radius_;
Note: See TracChangeset for help on using the changeset viewer.